1 // RUN: %check_clang_tidy %s readability-redundant-smartptr-get %t
2
3 #define NULL __null
4
5 namespace std {
6
7 template <typename T>
8 struct unique_ptr {
9 T& operator*() const;
10 T* operator->() const;
11 T* get() const;
12 explicit operator bool() const noexcept;
13 };
14
15 template <typename T>
16 struct shared_ptr {
17 T& operator*() const;
18 T* operator->() const;
19 T* get() const;
20 explicit operator bool() const noexcept;
21 };
22
23 } // namespace std
24
25 struct Bar {
26 void Do();
27 void ConstDo() const;
28 };
29 struct BarPtr {
30 Bar* operator->();
31 Bar& operator*();
32 Bar* get();
33 explicit operator bool() const;
34 };
35 struct int_ptr {
36 int* get();
37 int* operator->();
38 int& operator*();
39 };
40
41 struct Fail1 {
42 Bar* get();
43 };
44 struct Fail2 {
45 Bar* get();
46 int* operator->();
47 int& operator*();
48 };
49
50 struct PointerWithOverloadedGet {
51 int* get();
52 template <typename T>
53 T* get();
54 int* operator->();
55 int& operator*();
56 };
57
Positive()58 void Positive() {
59 BarPtr u;
60 // CHECK-FIXES: BarPtr u;
61 BarPtr().get()->Do();
62 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant get() call on smart pointer [readability-redundant-smartptr-get]
63 // CHECK-MESSAGES: BarPtr().get()->Do();
64 // CHECK-FIXES: BarPtr()->Do();
65
66 u.get()->ConstDo();
67 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant get() call
68 // CHECK-MESSAGES: u.get()->ConstDo();
69 // CHECK-FIXES: u->ConstDo();
70
71 Bar& b = *BarPtr().get();
72 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: redundant get() call
73 // CHECK-MESSAGES: Bar& b = *BarPtr().get();
74 // CHECK-FIXES: Bar& b = *BarPtr();
75
76 Bar& b2 = *std::unique_ptr<Bar>().get();
77 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: redundant get() call
78 // CHECK-MESSAGES: Bar& b2 = *std::unique_ptr<Bar>().get();
79 // CHECK-FIXES: Bar& b2 = *std::unique_ptr<Bar>();
80
81 (*BarPtr().get()).ConstDo();
82 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant get() call
83 // CHECK-MESSAGES: (*BarPtr().get()).ConstDo();
84 // CHECK-FIXES: (*BarPtr()).ConstDo();
85
86 (*std::unique_ptr<Bar>().get()).ConstDo();
87 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant get() call
88 // CHECK-MESSAGES: (*std::unique_ptr<Bar>().get()).ConstDo();
89 // CHECK-FIXES: (*std::unique_ptr<Bar>()).ConstDo();
90
91 std::unique_ptr<Bar>* up;
92 (*up->get()).Do();
93 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant get() call
94 // CHECK-MESSAGES: (*up->get()).Do();
95 // CHECK-FIXES: (**up).Do();
96
97 int_ptr ip;
98 int i = *ip.get();
99 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant get() call
100 // CHECK-MESSAGES: int i = *ip.get();
101 // CHECK-FIXES: int i = *ip;
102
103 auto ip2 = ip;
104 i = *ip2.get();
105 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
106 // CHECK-MESSAGES: i = *ip2.get();
107 // CHECK-FIXES: i = *ip2;
108
109 std::unique_ptr<int> uu;
110 std::shared_ptr<double> *ss;
111 bool bb = uu.get() == nullptr;
112 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: redundant get() call
113 // CHECK-MESSAGES: uu.get() == nullptr;
114 // CHECK-FIXES: bool bb = uu == nullptr;
115
116 if (up->get());
117 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant get() call
118 // CHECK-MESSAGES: if (up->get());
119 // CHECK-FIXES: if (*up);
120 if ((uu.get()));
121 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
122 // CHECK-MESSAGES: if ((uu.get()));
123 // CHECK-FIXES: if ((uu));
124 bb = !ss->get();
125 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: redundant get() call
126 // CHECK-MESSAGES: bb = !ss->get();
127 // CHECK-FIXES: bb = !*ss;
128 bb = u.get() ? true : false;
129 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
130 // CHECK-MESSAGES: bb = u.get() ? true : false;
131 // CHECK-FIXES: bb = u ? true : false;
132
133 bb = nullptr != ss->get();
134 // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: redundant get() call
135 // CHECK-MESSAGES: nullptr != ss->get();
136 // CHECK-FIXES: bb = nullptr != *ss;
137
138 i = *PointerWithOverloadedGet().get();
139 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
140 // CHECK-MESSAGES: i = *PointerWithOverloadedGet().get();
141 // CHECK-FIXES: i = *PointerWithOverloadedGet();
142
143 bb = std::unique_ptr<int>().get() == NULL;
144 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
145 // CHECK-MESSAGES: bb = std::unique_ptr<int>().get() == NULL;
146 // CHECK-FIXES: bb = std::unique_ptr<int>() == NULL;
147 bb = ss->get() == NULL;
148 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
149 // CHECK-MESSAGES: bb = ss->get() == NULL;
150 // CHECK-FIXES: bb = *ss == NULL;
151
152 std::unique_ptr<int> x, y;
153 if (x.get() == nullptr);
154 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant get() call
155 // CHECK-MESSAGES: if (x.get() == nullptr);
156 // CHECK-FIXES: if (x == nullptr);
157 if (nullptr == y.get());
158 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: redundant get() call
159 // CHECK-MESSAGES: if (nullptr == y.get());
160 // CHECK-FIXES: if (nullptr == y);
161 if (x.get() == NULL);
162 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant get() call
163 // CHECK-MESSAGES: if (x.get() == NULL);
164 // CHECK-FIXES: if (x == NULL);
165 if (NULL == x.get());
166 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: redundant get() call
167 // CHECK-MESSAGES: if (NULL == x.get());
168 // CHECK-FIXES: if (NULL == x);
169 }
170
171 #define MACRO(p) p.get()
172
Negative()173 void Negative() {
174 struct NegPtr {
175 int* get();
176 int* operator->() {
177 return &*this->get();
178 }
179 int& operator*() {
180 return *get();
181 }
182 };
183
184 long l = *PointerWithOverloadedGet().get<long>();
185
186 std::unique_ptr<Bar>* u;
187 u->get()->Do();
188
189 Fail1().get()->Do();
190 Fail2().get()->Do();
191 const Bar& b = *Fail1().get();
192 (*Fail2().get()).Do();
193
194 int_ptr ip;
195 bool bb = ip.get() == nullptr;
196 bb = !ip.get();
197 bb = ip.get() ? true : false;
198 std::unique_ptr<int> x;
199 if (MACRO(x) == nullptr)
200 ;
201 }
202