modernize-use-auto-cast.cpp
9.65 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// RUN: %check_clang_tidy %s modernize-use-auto %t -- \
// RUN: -config="{CheckOptions: [{key: modernize-use-auto.MinTypeNameLength, value: '0'}]}" \
// RUN: -- -I %S/Inputs/modernize-use-auto -frtti
struct A {
virtual ~A() {}
};
struct B : public A {};
struct C {};
void f_static_cast() {
long l = 1;
int i1 = static_cast<int>(l);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: auto i1 = static_cast<int>(l);
const int i2 = static_cast<int>(l);
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: const auto i2 = static_cast<int>(l);
long long ll = static_cast<long long>(l);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: auto ll = static_cast<long long>(l);
unsigned long long ull = static_cast<unsigned long long>(l);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: auto ull = static_cast<unsigned long long>(l);
unsigned int ui = static_cast<unsigned int>(l);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: auto ui = static_cast<unsigned int>(l);
long double ld = static_cast<long double>(l);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: auto ld = static_cast<long double>(l);
A *a = new B();
B *b1 = static_cast<B *>(a);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: auto *b1 = static_cast<B *>(a);
B *const b2 = static_cast<B *>(a);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: auto *const b2 = static_cast<B *>(a);
const B *b3 = static_cast<const B *>(a);
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: const auto *b3 = static_cast<const B *>(a);
B &b4 = static_cast<B &>(*a);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: auto &b4 = static_cast<B &>(*a);
const B &b5 = static_cast<const B &>(*a);
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: const auto &b5 = static_cast<const B &>(*a);
B &b6 = static_cast<B &>(*a), &b7 = static_cast<B &>(*a);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: auto &b6 = static_cast<B &>(*a), &b7 = static_cast<B &>(*a);
// Don't warn when non-cast involved
long double cast = static_cast<long double>(l), noncast = 5;
// Don't warn when auto is already being used.
auto i3 = static_cast<int>(l);
auto *b8 = static_cast<B *>(a);
auto &b9 = static_cast<B &>(*a);
}
void f_dynamic_cast() {
A *a = new B();
B *b1 = dynamic_cast<B *>(a);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: auto *b1 = dynamic_cast<B *>(a);
B &b2 = dynamic_cast<B &>(*a);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: auto &b2 = dynamic_cast<B &>(*a);
}
void f_reinterpret_cast() {
auto *a = new A();
C *c1 = reinterpret_cast<C *>(a);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: auto *c1 = reinterpret_cast<C *>(a);
C &c2 = reinterpret_cast<C &>(*a);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: auto &c2 = reinterpret_cast<C &>(*a);
}
void f_const_cast() {
const A *a1 = new A();
A *a2 = const_cast<A *>(a1);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: auto *a2 = const_cast<A *>(a1);
A &a3 = const_cast<A &>(*a1);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: auto &a3 = const_cast<A &>(*a1);
}
typedef unsigned char xmlChar;
#define BAD_CAST (xmlChar *)
#define XMLCHAR_CAST(x) (xmlChar *)(x)
#define CAST_IN_MACRO(x) \
do { \
xmlChar *s = (xmlChar *)(x); \
} while (false);
// CHECK-FIXES: xmlChar *s = (xmlChar *)(x);
void f_cstyle_cast() {
auto *a = new A();
C *c1 = (C *)a;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: auto *c1 = (C *)a;
C &c2 = (C &)*a;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: auto &c2 = (C &)*a;
xmlChar *s = BAD_CAST "xml";
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: auto *s = BAD_CAST "xml";
xmlChar *t = XMLCHAR_CAST("xml");
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: auto *t = XMLCHAR_CAST("xml");
CAST_IN_MACRO("xml");
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name
}
void f_functional_cast() {
long l = 1;
int i1 = int(l);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a cast to avoid duplicating the type name
// CHECK-FIXES: auto i1 = int(l);
B b;
A a = A(b);
}
class StringRef
{
public:
StringRef(const char *);
const char *begin() const;
const char *end() const;
};
template <typename T, typename U>
T template_value_cast(const U &u);
template <typename T, typename U>
T *template_pointer_cast(U *u);
template <typename T, typename U>
T &template_reference_cast(U &u);
template <typename T, typename U>
const T *template_const_pointer_cast(const U *u);
template <typename T, typename U>
const T &template_const_reference_cast(const U &u);
template <typename T>
T template_value_get(StringRef s);
struct S {
template <typename T>
const T *template_member_get();
};
template <typename T>
T max(T t1, T t2);
void f_template_cast()
{
double d = 0;
int i1 = template_value_cast<int>(d);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a template cast to avoid duplicating the type name
// CHECK-FIXES: auto i1 = template_value_cast<int>(d);
A *a = new B();
B *b1 = template_value_cast<B *>(a);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a template cast to avoid duplicating the type name
// CHECK-FIXES: auto *b1 = template_value_cast<B *>(a);
B &b2 = template_value_cast<B &>(*a);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a template cast to avoid duplicating the type name
// CHECK-FIXES: auto &b2 = template_value_cast<B &>(*a);
B *b3 = template_pointer_cast<B>(a);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a template cast to avoid duplicating the type name
// CHECK-FIXES: auto *b3 = template_pointer_cast<B>(a);
B &b4 = template_reference_cast<B>(*a);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a template cast to avoid duplicating the type name
// CHECK-FIXES: auto &b4 = template_reference_cast<B>(*a);
const B *b5 = template_const_pointer_cast<B>(a);
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a template cast to avoid duplicating the type name
// CHECK-FIXES: const auto *b5 = template_const_pointer_cast<B>(a);
const B &b6 = template_const_reference_cast<B>(*a);
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a template cast to avoid duplicating the type name
// CHECK-FIXES: const auto &b6 = template_const_reference_cast<B>(*a);
B *b7 = template_value_get<B *>("foo");
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a template cast to avoid duplicating the type name
// CHECK-FIXES: auto *b7 = template_value_get<B *>("foo");
B *b8 = template_value_get<B *>("foo"), *b9 = template_value_get<B *>("bar");
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with a template cast to avoid duplicating the type name
// CHECK-FIXES: auto *b8 = template_value_get<B *>("foo"), *b9 = template_value_get<B *>("bar");
S s;
const B *b10 = s.template_member_get<B>();
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use auto when initializing with a template cast to avoid duplicating the type name
// CHECK-FIXES: const auto *b10 = s.template_member_get<B>();
// Don't warn when auto is already being used.
auto i2 = template_value_cast<int>(d);
auto *i3 = template_value_cast<int *>(d);
auto **i4 = template_value_cast<int **>(d);
auto &i5 = template_reference_cast<int>(d);
// Don't warn for implicit template arguments.
int i6 = max(i1, i2);
// Don't warn for mismatched var and initializer types.
A *a1 = template_value_cast<B *>(a);
// Don't warn for mismatched var types.
B *b11 = template_value_get<B *>("foo"), b12 = template_value_get<B>("bar");
// Don't warn for implicit variables.
for (auto &c : template_reference_cast<StringRef>(*a)) {
}
}