Skip to content

Commit ebccc95

Browse files
committed
[clang-tidy] Add check for assignment or comparision operators' operand in readability-math-missing-parentheses
1 parent e573ffe commit ebccc95

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-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
@@ -187,6 +187,11 @@ Changes in existing checks
187187
<clang-tidy/checks/concurrency/mt-unsafe>` check by fixing a false positive
188188
where ``strerror`` was flagged as MT-unsafe.
189189

190+
- Improved :doc:`readability-math-missing-parentheses
191+
<clang-tidy/checks/readability/math-missing-parentheses>` check by fixing
192+
false negatives where math expressions are the operand of assignment operators
193+
or comparison operators.
194+
190195
- Improved :doc:`misc-const-correctness
191196
<clang-tidy/checks/misc/const-correctness>` check by adding the option
192197
`AllowedTypes`, that excludes specified types from const-correctness

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,17 @@ 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+1]]:30: warning: '-' has higher precedence than '^'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
164+
netChange[i] = nums[i] ^ k - nums[i];
165+
}
166+
}
167+
168+
void CompareAsParentBinOp(int b) {
169+
//CHECK-MESSAGES: :[[@LINE+1]]:12: warning: '*' has higher precedence than '-'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
170+
if (b == 1 * 2 - 3) {
171+
172+
}
173+
}

0 commit comments

Comments
 (0)