[clang] [Clang] Improve ``-Wtautological-overlap-compare`` diagnostics flag (PR #133653)
Yutong Zhu via cfe-commits
cfe-commits at lists.llvm.org
Fri May 9 06:38:22 PDT 2025
================
@@ -169,7 +169,84 @@ int struct_test(struct A a) {
return a.x > 5 && a.y < 1; // no warning, different variables
return a.x > 5 && a.x < 1;
- // expected-warning at -1{{overlapping comparisons always evaluate to false}}
+ // expected-warning at -1{{non-overlapping comparisons always evaluate to false}}
return a.y == 1 || a.y != 1;
// expected-warning at -1{{overlapping comparisons always evaluate to true}}
}
+
+void char_tests(char c) {
+ if (c > 'a' || c < 'z') {}
+ // expected-warning at -1{{overlapping comparisons always evaluate to true}}
+ if (c > 'z' && c < 'a') {}
+ // expected-warning at -1{{non-overlapping comparisons always evaluate to false}}
+ if (c == 'a' && c == 'z') {}
+ // expected-warning at -1{{non-overlapping comparisons always evaluate to false}}
+ if (c != 'a' || c != 'z') {}
+ // expected-warning at -1{{overlapping comparisons always evaluate to true}}
+}
+
+void float_tests(float f) {
+ if (f > 1.0 || f < 2.0) {}
+ // expected-warning at -1{{overlapping comparisons always evaluate to true}}
----------------
YutongZhuu wrote:
Sorry for the late reply. If we consider ``nan`` as a possible value for ``f``, this flag would be wrong in the first place since what you said is also applicable for integer comparisons. I wouldn't worry too much about the ``nan`` case too much because I really can't imagine any code written like this was intended.
``__builtin_nan()/__builtin_inf()`` wouldn't trigger this flag at all since this flag only deals with literal comparands, but I can still add them.
https://github.com/llvm/llvm-project/pull/133653
More information about the cfe-commits
mailing list