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