1// RUN: %check_clang_tidy -std=c++11 -check-suffixes=,CXX11 %s misc-static-assert %t
2// RUN: %check_clang_tidy -std=c++17-or-later -check-suffixes=,CXX17 %s misc-static-assert %t
3
4void abort() {}
5#ifdef NDEBUG
6#define assert(x) 1
7#else
8#define assert(x) \
9 if (!(x)) \
10 abort()
11#endif
12
13void print(...);
14
15#define ZERO_MACRO 0
16
17#define False false
18#define FALSE 0
19
20#define my_macro() assert(0 == 1)
21// CHECK-FIXES: #define my_macro() assert(0 == 1)
22
23namespace PR24066 {
24
25void referenceMember() {
26 struct {
27 int A;
28 int B;
29 } S;
30 assert(&S.B - &S.A == 1);
31}
32
33const int X = 1;
34void referenceVariable() {
35 assert(X > 0);
36}
37
38
39constexpr int Y = 1;
40void referenceConstexprVariable() {
41 assert(Y > 0);
42 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be replaced by static_assert() [misc-static-assert]
43 // CHECK-FIXES-CXX11: {{^ }}static_assert(Y > 0, "");
44 // CHECK-FIXES-CXX17: {{^ }}static_assert(Y > 0);
45}
46
47void useInSizeOf() {
48 char a = 0;
49 assert(sizeof(a) == 1U);
50 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be replaced by static_assert() [misc-static-assert]
51 // CHECK-FIXES-CXX11: {{^ }}static_assert(sizeof(a) == 1U, "");
52 // CHECK-FIXES-CXX17: {{^ }}static_assert(sizeof(a) == 1U);
53}
54
55void useInDecltype() {
56 char a = 0;
57 assert(static_cast<decltype(a)>(256) == 0);
58 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be replaced by static_assert() [misc-static-assert]
59 // CHECK-FIXES-CXX11: {{^ }}static_assert(static_cast<decltype(a)>(256) == 0, "");
60 // CHECK-FIXES-CXX17: {{^ }}static_assert(static_cast<decltype(a)>(256) == 0);
61}
62
63}
64
65constexpr bool myfunc(int a, int b) { return a * b == 0; }
66
67typedef __SIZE_TYPE__ size_t;
68extern "C" size_t strlen(const char *s);
69
70class A {
71public:
72 bool method() { return true; }
73};
74
75class B {
76public:
77 constexpr bool method() { return true; }
78};
79
80template <class T> void doSomething(T t) {
81 assert(myfunc(1, 2));
82 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be replaced by static_assert() [misc-static-assert]
83 // CHECK-FIXES-CXX11: {{^ }}static_assert(myfunc(1, 2), "");
84 // CHECK-FIXES-CXX17: {{^ }}static_assert(myfunc(1, 2));
85
86 assert(t.method());
87 // CHECK-FIXES: {{^ }}assert(t.method());
88
89 assert(sizeof(T) == 123);
90}
91
92int main() {
93 my_macro();
94 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
95 // CHECK-FIXES: {{^ }}my_macro();
96
97 assert(myfunc(1, 2) && (3 == 4));
98 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
99 // CHECK-FIXES-CXX11: {{^ }}static_assert(myfunc(1, 2) && (3 == 4), "");
100 // CHECK-FIXES-CXX17: {{^ }}static_assert(myfunc(1, 2) && (3 == 4));
101
102 int x = 1;
103 assert(x == 0);
104 // CHECK-FIXES: {{^ }}assert(x == 0);
105
106 A a;
107 B b;
108
109 doSomething<A>(t: a);
110 doSomething<B>(t: b);
111
112 assert(false);
113 // CHECK-FIXES: {{^ }}assert(false);
114
115 assert(False);
116 // CHECK-FIXES: {{^ }}assert(False);
117 assert(FALSE);
118 // CHECK-FIXES: {{^ }}assert(FALSE);
119
120 assert(ZERO_MACRO);
121 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
122 // CHECK-FIXES-CXX11: {{^ }}static_assert(ZERO_MACRO, "");
123 // CHECK-FIXES-CXX17: {{^ }}static_assert(ZERO_MACRO);
124
125 assert(!"Don't report me!");
126 // CHECK-FIXES: {{^ }}assert(!"Don't report me!");
127
128 assert(0 && "Don't report me!");
129 // CHECK-FIXES: {{^ }}assert(0 && "Don't report me!");
130
131 assert(false && "Don't report me!");
132 // CHECK-FIXES: {{^ }}assert(false && "Don't report me!");
133
134#define NULL ((void*)0)
135 assert(NULL && "Don't report me!");
136 // CHECK-FIXES: {{^ }}assert(NULL && "Don't report me!");
137
138 assert(NULL == "Don't report me!");
139 // CHECK-FIXES: {{^ }}assert(NULL == "Don't report me!");
140
141 assert("Don't report me!" == NULL);
142 // CHECK-FIXES: {{^ }}assert("Don't report me!" == NULL);
143
144 assert(0 == "Don't report me!");
145 // CHECK-FIXES: {{^ }}assert(0 == "Don't report me!");
146
147#define NULL ((unsigned int)0)
148 assert(NULL && "Report me!");
149 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
150 // CHECK-FIXES: {{^ }}static_assert(NULL , "Report me!");
151
152#define NULL __null
153 assert(__null == "Don't report me!");
154 // CHECK-FIXES: {{^ }}assert(__null == "Don't report me!");
155 assert(NULL == "Don't report me!");
156 // CHECK-FIXES: {{^ }}assert(NULL == "Don't report me!");
157#undef NULL
158
159 assert(ZERO_MACRO && "Report me!");
160 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
161 // CHECK-FIXES: {{^ }}static_assert(ZERO_MACRO , "Report me!");
162
163 assert(0);
164
165#define false false
166 assert(false);
167
168#define false 0
169 assert(false);
170#undef false
171
172 assert(10==5 && "Report me!");
173 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
174 // CHECK-FIXES: {{^ }}static_assert(10==5 , "Report me!");
175
176 assert(strlen("12345") == 5);
177 // CHECK-FIXES: {{^ }}assert(strlen("12345") == 5);
178
179#define assert(e) (__builtin_expect(!(e), 0) ? print (#e, __FILE__, __LINE__) : (void)0)
180 assert(false);
181 // CHECK-FIXES: {{^ }}assert(false);
182
183 assert(10 == 5 + 5);
184 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
185 // CHECK-FIXES-CXX11: {{^ }}static_assert(10 == 5 + 5, "");
186 // CHECK-FIXES-CXX17: {{^ }}static_assert(10 == 5 + 5);
187#undef assert
188
189 return 0;
190}
191

source code of clang-tools-extra/test/clang-tidy/checkers/misc/static-assert.cpp