blob: 27d1eb3d40bc4f0ffd289aace6dfef6469654507 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Ted Kremenek3ca0bf22007-10-29 16:58:49 +00002
3int foo(int x) {
Douglas Gregord64fdd02010-06-08 19:50:34 +00004 return x == x; // expected-warning {{self-comparison always evaluates to true}}
Ted Kremenek3ca0bf22007-10-29 16:58:49 +00005}
6
7int foo2(int x) {
Douglas Gregord64fdd02010-06-08 19:50:34 +00008 return (x) != (((x))); // expected-warning {{self-comparison always evaluates to false}}
9}
10
11void foo3(short s, short t) {
12 if (s == s) {} // expected-warning {{self-comparison always evaluates to true}}
13 if (s == t) {} // no-warning
14}
15
16void foo4(void* v, void* w) {
17 if (v == v) {} // expected-warning {{self-comparison always evaluates to true}}
18 if (v == w) {} // no-warning
Ted Kremenek3ca0bf22007-10-29 16:58:49 +000019}
20
Ted Kremeneka8335a92007-10-29 17:02:56 +000021int qux(int x) {
22 return x < x; // expected-warning {{self-comparison}}
23}
24
25int qux2(int x) {
26 return x > x; // expected-warning {{self-comparison}}
27}
28
Ted Kremenek3ca0bf22007-10-29 16:58:49 +000029int bar(float x) {
30 return x == x; // no-warning
31}
32
33int bar2(float x) {
34 return x != x; // no-warning
Ted Kremeneka8335a92007-10-29 17:02:56 +000035}
Ted Kremenekb82dcd82009-03-20 18:35:45 +000036
37// Motivated by <rdar://problem/6703892>, self-comparisons of enum constants
38// should not be warned about. These can be expanded from macros, and thus
39// are usually deliberate.
40int compare_enum() {
41 enum { A };
42 return A == A; // no-warning
43}
Douglas Gregord1e4d9b2010-01-12 23:18:54 +000044
45// Don't complain in unevaluated contexts.
46int compare_sizeof(int x) {
47 return sizeof(x == x); // no-warning
48}
Douglas Gregord64fdd02010-06-08 19:50:34 +000049
50int array_comparisons() {
51 int array1[2];
52 int array2[2];
53
54 //
55 // compare same array
56 //
57 return array1 == array1; // expected-warning{{self-comparison always evaluates to true}}
58 return array1 != array1; // expected-warning{{self-comparison always evaluates to false}}
59 return array1 < array1; // expected-warning{{self-comparison always evaluates to false}}
60 return array1 <= array1; // expected-warning{{self-comparison always evaluates to true}}
61 return array1 > array1; // expected-warning{{self-comparison always evaluates to false}}
62 return array1 >= array1; // expected-warning{{self-comparison always evaluates to true}}
63
64 //
65 // compare differrent arrays
66 //
67 return array1 == array2; // expected-warning{{array comparison always evaluates to false}}
68 return array1 != array2; // expected-warning{{array comparison always evaluates to true}}
69
70 //
71 // we don't know what these are going to be
72 //
73 return array1 < array2; // expected-warning{{array comparison always evaluates to a constant}}
74 return array1 <= array2; // expected-warning{{array comparison always evaluates to a constant}}
75 return array1 > array2; // expected-warning{{array comparison always evaluates to a constant}}
76 return array1 >= array2; // expected-warning{{array comparison always evaluates to a constant}}
77
78}
79