Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ using namespace clang::ast_matchers;
namespace clang::tidy::readability {

void MathMissingParenthesesCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(binaryOperator(unless(hasParent(binaryOperator())),
unless(isAssignmentOperator()),
unless(isComparisonOperator()),
unless(hasAnyOperatorName("&&", "||")),
hasDescendant(binaryOperator()))
.bind("binOp"),
this);
Finder->addMatcher(
binaryOperator(
unless(hasParent(binaryOperator(unless(isAssignmentOperator()),
unless(isComparisonOperator())))),
unless(isAssignmentOperator()), unless(isComparisonOperator()),
unless(hasAnyOperatorName("&&", "||")),
hasDescendant(binaryOperator()))
.bind("binOp"),
this);
}

static int getPrecedence(const BinaryOperator *BinOp) {
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ Changes in existing checks
tolerating fix-it breaking compilation when functions is used as pointers
to avoid matching usage of functions within the current compilation unit.

- Improved :doc:`readability-math-missing-parentheses
<clang-tidy/checks/readability/math-missing-parentheses>` check by fixing
false negatives where math expressions are the operand of assignment operators
or comparison operators.

- Improved :doc:`readability-qualified-auto
<clang-tidy/checks/readability/qualified-auto>` check by adding the option
`AllowedTypes`, that excludes specified types from adding qualifiers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,19 @@ namespace PR92516 {
for (j = i + 1, 2; j < 1; ++j) {}
}
}

namespace PR141249 {
void AssignAsParentBinOp(int* netChange, int* nums, int k, int i) {
//CHECK-MESSAGES: :[[@LINE+2]]:30: warning: '-' has higher precedence than '^'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
//CHECK-FIXES: netChange[i] = nums[i] ^ (k - nums[i]);
netChange[i] = nums[i] ^ k - nums[i];
}
}

void CompareAsParentBinOp(int b) {
//CHECK-MESSAGES: :[[@LINE+2]]:12: warning: '*' has higher precedence than '-'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
//CHECK-FIXES: if (b == (1 * 2) - 3) {
if (b == 1 * 2 - 3) {

}
}