Author: flovent
Date: 2025-06-05T15:18:01+03:00
New Revision: a12f4f0031c5448df1563a499d6295fd06b979ff

URL: 
https://github.com/llvm/llvm-project/commit/a12f4f0031c5448df1563a499d6295fd06b979ff
DIFF: 
https://github.com/llvm/llvm-project/commit/a12f4f0031c5448df1563a499d6295fd06b979ff.diff

LOG: [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.

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    
clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp

Removed: 
    


################################################################################
diff  --git 
a/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
index cf4214a059682..1d5c6cca5a82d 100644
--- a/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/MathMissingParenthesesCheck.cpp
@@ -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) {

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index e0f81a032c38d..b7d0a223ce5bb 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -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.

diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp
index 80d2bc304bb5b..947eda07a7675 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp
@@ -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)   {
+
+  }
+}


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to