blob: 00e3c8951941202b5187b5190061a06ea9d8d4ee [file] [log] [blame]
Alexander Kornienkoc0308c42016-06-03 21:22:58 +00001// RUN: %check_clang_tidy %s modernize-use-auto %t -- \
Zinovy Nis60874d42018-04-12 06:45:47 +00002// RUN: -config="{CheckOptions: [{key: modernize-use-auto.RemoveStars, value: '1'}, {key: modernize-use-auto.MinTypeNameLength, value: '0'}]}" \
Alexander Kornienkoc0308c42016-06-03 21:22:58 +00003// RUN: -- -std=c++11
4
5class MyType {};
6
7class 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 '*').
11void 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 MyType *derived = new MyDerivedType();
21
22 void *vd = new MyType();
23
24 // CV-qualifier tests.
25 //
26 // NOTE : the form "type const" is expected here because of a deficiency in
27 // TypeLoc where CV qualifiers are not considered part of the type location
28 // info. That is, all that is being replaced in each case is "MyType *" and
29 // not "MyType * const".
30 static MyType * const d_static = new MyType();
31 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use auto when initializing with new
32 // CHECK-FIXES: static auto const d_static = new MyType();
33
34 MyType * const a_const = new MyType();
35 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new
36 // CHECK-FIXES: auto const a_const = new MyType();
37
38 MyType * volatile vol = new MyType();
39 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new
40 // CHECK-FIXES: auto volatile vol = new MyType();
41
42 struct SType {} *stype = new SType;
43
44 int (**func)(int, int) = new (int(*[5])(int,int));
45
46 int *array = new int[5];
47 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new
48 // CHECK-FIXES: auto array = new int[5];
49
50 MyType *ptr(new MyType);
51 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use auto when initializing with new
52 // CHECK-FIXES: auto ptr(new MyType);
53
54 MyType *ptr2{new MyType};
55
56 {
57 // Test for declaration lists.
58 MyType *a = new MyType(), *b = new MyType(), *c = new MyType();
59 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
60 // CHECK-FIXES: auto a = new MyType(), b = new MyType(), c = new MyType();
61
62 // Non-initialized declaration should not be transformed.
63 MyType *d = new MyType(), *e;
64
65 MyType **f = new MyType*(), **g = new MyType*();
66 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
67 // CHECK-FIXES: auto f = new MyType*(), g = new MyType*();
68
69 // Mismatching types in declaration lists should not be transformed.
70 MyType *h = new MyType(), **i = new MyType*();
71
72 // '*' shouldn't be removed in case of mismatching types with multiple
73 // declarations.
74 MyType *j = new MyType(), *k = new MyType(), **l = new MyType*();
75 }
76
77 {
78 // Test for typedefs.
79 typedef int * int_p;
80 // CHECK-FIXES: typedef int * int_p;
81
82 int_p a = new int;
83 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
84 // CHECK-FIXES: auto a = new int;
85 int_p *b = new int*;
86 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
87 // CHECK-FIXES: auto b = new int*;
88
89 // Test for typedefs in declarations lists.
90 int_p c = new int, d = new int;
91 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
92 // CHECK-FIXES: auto c = new int, d = new int;
93
94 // Different types should not be transformed.
95 int_p e = new int, *f = new int_p;
96
97 int_p *g = new int*, *h = new int_p;
98 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use auto when initializing with new
99 // CHECK-FIXES: auto g = new int*, h = new int_p;
100 }
101
102 // Don't warn when 'auto' is already being used.
103 auto aut = new MyType();
104 auto *paut = new MyType();
105 const auto *pcaut = new MyType();
106}