[clang] [clang][Sema] Warn consecutive builtin comparisons in an expression (PR #92200)

Youngsuk Kim via cfe-commits cfe-commits at lists.llvm.org
Thu May 16 15:07:07 PDT 2024


================
@@ -215,3 +215,10 @@ namespace PR20735 {
     // fix-it:"{{.*}}":{[[@LINE-9]]:20-[[@LINE-9]]:20}:")"
   }
 }
+
+void consecutive_builtin_compare(int x, int y, int z) {
+  (void)(x < y < z);  // expected-warning {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
+  (void)(x < y > z);  // expected-warning {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
+  (void)(x < y <= z); // expected-warning {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
+  (void)(x <= y > z); // expected-warning {{comparisons like 'X<=Y<=Z' don't have their mathematical meaning}}
----------------
JOE1994 wrote:

(I'll refer to each of the 3 cases mentioned in the above comment as case1, case2, and case 3)

I pushed a commit that adds 2 `no-warning` test lines for case1.
It's unlikely for one to write `((x < y) >= z)` and believe it to have mathematical meaning of `(x < y) AND (y >= z)`.

I limited the scope of this PR to diagnosing consecutive "built-in" comparisons (as per the PR title).
Thus, the current implementation does not diagnose case 2.

I don't have the capacity to further work on making clang diagnose case 2 but not case 3.
I added a TODO note at the bottom of the test lines for now.

FYI, gcc doesn't diagnose any of case1, case2, or case 3.

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


More information about the cfe-commits mailing list