[clang-tools-extra] Add clang-tidy check readability-math-missing-parentheses (PR #84481)

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 30 21:17:18 PDT 2024


================
@@ -0,0 +1,81 @@
+// RUN: %check_clang_tidy %s readability-math-missing-parentheses %t
+
+int foo(){
+    return 5;
+}
+
+int bar(){
+    return 4;
+}
+
+class fun{
+public:  
+    int A;
+    double B;
+    fun(){
+        A = 5;
+        B = 5.4;
+    }
+};
+
+void f(){
+    //CHECK-MESSAGES: :[[@LINE+2]]:17: warning: '*' has higher precedence than '+'; add parentheses to make the precedence of operations explicit [readability-math-missing-parentheses]
+    //CHECK-FIXES: int a = 1 + (2 * 3);
+    int a = 1 + 2 * 3; 
+
+    int b = 1 + 2 + 3; // No warning
+
+    int c = 1 * 2 * 3; // No warning
+
+    //CHECK-MESSAGES: :[[@LINE+3]]:17: warning: '*' has higher precedence than '+'; add parentheses to make the precedence of operations explicit [readability-math-missing-parentheses]
+    //CHECK-MESSAGES: :[[@LINE+2]]:25: warning: '/' has higher precedence than '-'; add parentheses to make the precedence of operations explicit [readability-math-missing-parentheses]
+    //CHECK-FIXES: int d = 1 + (2 * 3) - (4 / 5);
+    int d = 1 + 2 * 3 - 4 / 5;
----------------
PiotrZSL wrote:

Add more negative tests, like this:
`int d_negative = 1 + (2 * 3) - (4 / 5);`
Just to check that `()` won't be added if they already exists.

https://github.com/llvm/llvm-project/pull/84481


More information about the cfe-commits mailing list