Skip to content

Commit a12f4f0

Browse files
authored
[clang-tidy] Add check for assignment or comparision operators' operand in readability-math-missing-parentheses (#141345)
Fixes false negative in #141249. Add check for math binary operators which are operands of assignment or comparision operators. Closes #141249.
1 parent 60808a4 commit a12f4f0

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ using namespace clang::ast_matchers;
1616
namespace clang::tidy::readability {
1717

1818
void MathMissingParenthesesCheck::registerMatchers(MatchFinder *Finder) {
19-
Finder->addMatcher(binaryOperator(unless(hasParent(binaryOperator())),
20-
unless(isAssignmentOperator()),
21-
unless(isComparisonOperator()),
22-
unless(hasAnyOperatorName("&&", "||")),
23-
hasDescendant(binaryOperator()))
24-
.bind("binOp"),
25-
this);
19+
Finder->addMatcher(
20+
binaryOperator(
21+
unless(hasParent(binaryOperator(unless(isAssignmentOperator()),
22+
unless(isComparisonOperator())))),
23+
unless(isAssignmentOperator()), unless(isComparisonOperator()),
24+
unless(hasAnyOperatorName("&&", "||")),
25+
hasDescendant(binaryOperator()))
26+
.bind("binOp"),
27+
this);
2628
}
2729

2830
static int getPrecedence(const BinaryOperator *BinOp) {

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ Changes in existing checks
245245
tolerating fix-it breaking compilation when functions is used as pointers
246246
to avoid matching usage of functions within the current compilation unit.
247247

248+
- Improved :doc:`readability-math-missing-parentheses
249+
<clang-tidy/checks/readability/math-missing-parentheses>` check by fixing
250+
false negatives where math expressions are the operand of assignment operators
251+
or comparison operators.
252+
248253
- Improved :doc:`readability-qualified-auto
249254
<clang-tidy/checks/readability/qualified-auto>` check by adding the option
250255
`AllowedTypes`, that excludes specified types from adding qualifiers.

clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,19 @@ namespace PR92516 {
157157
for (j = i + 1, 2; j < 1; ++j) {}
158158
}
159159
}
160+
161+
namespace PR141249 {
162+
void AssignAsParentBinOp(int* netChange, int* nums, int k, int i) {
163+
//CHECK-MESSAGES: :[[@LINE+2]]:30: warning: '-' has higher precedence than '^'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
164+
//CHECK-FIXES: netChange[i] = nums[i] ^ (k - nums[i]);
165+
netChange[i] = nums[i] ^ k - nums[i];
166+
}
167+
}
168+
169+
void CompareAsParentBinOp(int b) {
170+
//CHECK-MESSAGES: :[[@LINE+2]]:12: warning: '*' has higher precedence than '-'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
171+
//CHECK-FIXES: if (b == (1 * 2) - 3) {
172+
if (b == 1 * 2 - 3) {
173+
174+
}
175+
}

0 commit comments

Comments
 (0)