1 // RUN: %check_clang_tidy %s modernize-use-auto %t -- \
2 // RUN: -config="{CheckOptions: [{key: modernize-use-auto.MinTypeNameLength, value: '0'}]}" \
3 // RUN: -- -std=c++11 -frtti
4
5 class MyType {};
6
7 class MyDerivedType : public MyType {};
8
9 // FIXME: the replacement sometimes results in two consecutive spaces after
10 // the word 'auto' (due to the presence of spaces at both sides of '*').
auto_new()11 void auto_new() {
12 MyType *a_new = new MyType();
13 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new
14 // CHECK-FIXES: auto *a_new = new MyType();
15
16 static MyType *a_static = new MyType();
17 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use auto when initializing with new
18 // CHECK-FIXES: static auto *a_static = new MyType();
19
20 long long *ll = new long long();
21 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new
22 // CHECK-FIXES: auto *ll = new long long();
23
24 MyType *derived = new MyDerivedType();
25
26 void *vd = new MyType();
27
28 // CV-qualifier tests.
29 //
30 // NOTE : the form "type const" is expected here because of a deficiency in
31 // TypeLoc where CV qualifiers are not considered part of the type location
32 // info. That is, all that is being replaced in each case is "MyType *" and
33 // not "MyType * const".
34 static MyType * const d_static = new MyType();
35 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use auto when initializing with new
36 // CHECK-FIXES: static auto * const d_static = new MyType();
37
38 MyType * const a_const = new MyType();
39 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new
40 // CHECK-FIXES: auto * const a_const = new MyType();
41
42 MyType * volatile vol = new MyType();
43 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new
44 // CHECK-FIXES: auto * volatile vol = new MyType();
45
46 struct SType {} *stype = new SType;
47
48 int (**func)(int, int) = new (int(*[5])(int,int));
49
50 int *array = new int[5];
51 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new
52 // CHECK-FIXES: auto *array = new int[5];
53
54 MyType *ptr(new MyType);
55 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new
56 // CHECK-FIXES: auto *ptr(new MyType);
57
58 MyType *ptr2{new MyType};
59
60 {
61 // Test for declaration lists.
62 MyType *a = new MyType(), *b = new MyType(), *c = new MyType();
63 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
64 // CHECK-FIXES: auto *a = new MyType(), *b = new MyType(), *c = new MyType();
65
66 // Non-initialized declaration should not be transformed.
67 MyType *d = new MyType(), *e;
68
69 MyType **f = new MyType*(), **g = new MyType*();
70 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
71 // CHECK-FIXES: auto **f = new MyType*(), **g = new MyType*();
72
73 // Mismatching types in declaration lists should not be transformed.
74 MyType *h = new MyType(), **i = new MyType*();
75
76 // '*' shouldn't be removed in case of mismatching types with multiple
77 // declarations.
78 MyType *j = new MyType(), *k = new MyType(), **l = new MyType*();
79 }
80
81 {
82 // Test for typedefs.
83 typedef int * int_p;
84 // CHECK-FIXES: typedef int * int_p;
85
86 int_p a = new int;
87 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
88 // CHECK-FIXES: auto a = new int;
89 int_p *b = new int*;
90 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
91 // CHECK-FIXES: auto *b = new int*;
92
93 // Test for typedefs in declarations lists.
94 int_p c = new int, d = new int;
95 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
96 // CHECK-FIXES: auto c = new int, d = new int;
97
98 // Different types should not be transformed.
99 int_p e = new int, *f = new int_p;
100
101 int_p *g = new int*, *h = new int_p;
102 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
103 // CHECK-FIXES: auto *g = new int*, *h = new int_p;
104 }
105
106 // Don't warn when 'auto' is already being used.
107 auto aut = new MyType();
108 auto *paut = new MyType();
109 const auto *pcaut = new MyType();
110 }
111