1 // RUN: %check_clang_tidy %s misc-static-assert %t
2 
abort()3 void abort() {}
4 #ifdef NDEBUG
5 #define assert(x) 1
6 #else
7 #define assert(x)                                                              \
8   if (!(x))                                                                    \
9   abort()
10 #endif
11 
12 void print(...);
13 
14 #define ZERO_MACRO 0
15 
16 #define False false
17 #define FALSE 0
18 
19 #define my_macro() assert(0 == 1)
20 // CHECK-FIXES: #define my_macro() assert(0 == 1)
21 
myfunc(int a,int b)22 constexpr bool myfunc(int a, int b) { return a * b == 0; }
23 
24 typedef __SIZE_TYPE__ size_t;
25 extern "C" size_t strlen(const char *s);
26 
27 class A {
28 public:
method()29   bool method() { return true; }
30 };
31 
32 class B {
33 public:
method()34   constexpr bool method() { return true; }
35 };
36 
doSomething(T t)37 template <class T> void doSomething(T t) {
38   assert(myfunc(1, 2));
39   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be replaced by static_assert() [misc-static-assert]
40   // CHECK-FIXES: {{^  }}static_assert(myfunc(1, 2), "");
41 
42   assert(t.method());
43   // CHECK-FIXES: {{^  }}assert(t.method());
44 
45   assert(sizeof(T) == 123);
46 }
47 
main()48 int main() {
49   my_macro();
50   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
51   // CHECK-FIXES: {{^  }}my_macro();
52 
53   assert(myfunc(1, 2) && (3 == 4));
54   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
55   // CHECK-FIXES: {{^  }}static_assert(myfunc(1, 2) && (3 == 4), "");
56 
57   int x = 1;
58   assert(x == 0);
59   // CHECK-FIXES: {{^  }}assert(x == 0);
60 
61   A a;
62   B b;
63 
64   doSomething<A>(a);
65   doSomething<B>(b);
66 
67   assert(false);
68   // CHECK-FIXES: {{^  }}assert(false);
69 
70   assert(False);
71   // CHECK-FIXES: {{^  }}assert(False);
72   assert(FALSE);
73   // CHECK-FIXES: {{^  }}assert(FALSE);
74 
75   assert(ZERO_MACRO);
76   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
77   // CHECK-FIXES: {{^  }}static_assert(ZERO_MACRO, "");
78 
79   assert(!"Don't report me!");
80   // CHECK-FIXES: {{^  }}assert(!"Don't report me!");
81 
82   assert(0 && "Don't report me!");
83   // CHECK-FIXES: {{^  }}assert(0 && "Don't report me!");
84 
85   assert(false && "Don't report me!");
86   // CHECK-FIXES: {{^  }}assert(false && "Don't report me!");
87 
88 #define NULL ((void*)0)
89   assert(NULL && "Don't report me!");
90   // CHECK-FIXES: {{^  }}assert(NULL && "Don't report me!");
91 
92   assert(NULL == "Don't report me!");
93   // CHECK-FIXES: {{^  }}assert(NULL == "Don't report me!");
94 
95   assert("Don't report me!" == NULL);
96   // CHECK-FIXES: {{^  }}assert("Don't report me!" == NULL);
97 
98   assert(0 == "Don't report me!");
99   // CHECK-FIXES: {{^  }}assert(0 == "Don't report me!");
100 
101 #define NULL ((unsigned int)0)
102   assert(NULL && "Report me!");
103   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
104   // CHECK-FIXES: {{^  }}static_assert(NULL , "Report me!");
105 
106 #define NULL __null
107   assert(__null == "Don't report me!");
108   // CHECK-FIXES: {{^  }}assert(__null == "Don't report me!");
109   assert(NULL == "Don't report me!");
110   // CHECK-FIXES: {{^  }}assert(NULL == "Don't report me!");
111 #undef NULL
112 
113   assert(ZERO_MACRO && "Report me!");
114   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
115   // CHECK-FIXES: {{^  }}static_assert(ZERO_MACRO , "Report me!");
116 
117   assert(0);
118 
119 #define false false
120   assert(false);
121 
122 #define false 0
123   assert(false);
124 #undef false
125 
126   assert(10==5 && "Report me!");
127   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
128   // CHECK-FIXES: {{^  }}static_assert(10==5 , "Report me!");
129 
130   assert(strlen("12345") == 5);
131   // CHECK-FIXES: {{^  }}assert(strlen("12345") == 5);
132 
133 #define assert(e) (__builtin_expect(!(e), 0) ? print (#e, __FILE__, __LINE__) : (void)0)
134   assert(false);
135   // CHECK-FIXES: {{^  }}assert(false);
136 
137   assert(10 == 5 + 5);
138   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
139   // CHECK-FIXES: {{^  }}static_assert(10 == 5 + 5, "");
140 #undef assert
141 
142   return 0;
143 }
144