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