xref: /llvm-project/clang/test/SemaCXX/tautological-negation-compare.cpp (revision b3469ce6f80bce2b0a86026fd6867c4b04853466)
1 // RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-negation-compare -Wno-constant-logical-operand %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -Wtautological-compare -Wno-constant-logical-operand %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -Wall -Wno-unused -Wno-loop-analysis -Wno-constant-logical-operand %s
4 
5 #define COPY(x) x
6 
test_int(int x)7 void test_int(int x) {
8   if (x || !x) {} // expected-warning {{'||' of a value and its negation always evaluates to true}}
9   if (!x || x) {} // expected-warning {{'||' of a value and its negation always evaluates to true}}
10   if (x && !x) {} // expected-warning {{'&&' of a value and its negation always evaluates to false}}
11   if (!x && x) {} // expected-warning {{'&&' of a value and its negation always evaluates to false}}
12 
13   // parentheses are ignored
14   if (x || (!x)) {} // expected-warning {{'||' of a value and its negation always evaluates to true}}
15   if (!(x) || x) {} // expected-warning {{'||' of a value and its negation always evaluates to true}}
16 
17   // don't warn on macros
18   if (COPY(x) || !x) {}
19   if (!x || COPY(x)) {}
20   if (x && COPY(!x)) {}
21   if (COPY(!x && x)) {}
22 
23   // dont' warn on literals
24   if (1 || !1) {}
25   if (!42 && 42) {}
26 
27 
28   // don't warn on overloads
29   struct Foo{
30     int val;
31     Foo operator!() const { return Foo{!val}; }
32     bool operator||(const Foo other) const { return val || other.val; }
33     bool operator&&(const Foo other) const { return val && other.val; }
34   };
35 
36   Foo f{3};
37   if (f || !f) {}
38   if (!f || f) {}
39   if (f.val || !f.val) {} // expected-warning {{'||' of a value and its negation always evaluates to true}}
40   if (!f.val && f.val) {} // expected-warning {{'&&' of a value and its negation always evaluates to false}}
41 }
42