File tree Expand file tree Collapse file tree 3 files changed +56
-0
lines changed Expand file tree Collapse file tree 3 files changed +56
-0
lines changed Original file line number Diff line number Diff line change @@ -164,6 +164,10 @@ void RedundantSmartptrGetCheck::check(const MatchFinder::MatchResult &Result) {
164
164
StringRef SmartptrText = Lexer::getSourceText (
165
165
CharSourceRange::getTokenRange (Smartptr->getSourceRange ()),
166
166
*Result.SourceManager , getLangOpts ());
167
+ // Check if the last two characters are "->" and remove them
168
+ if (SmartptrText.ends_with (" ->" )) {
169
+ SmartptrText = SmartptrText.drop_back (2 );
170
+ }
167
171
// Replace foo->get() with *foo, and foo.get() with foo.
168
172
std::string Replacement = Twine (IsPtrToPtr ? " *" : " " , SmartptrText).str ();
169
173
diag (GetCall->getBeginLoc (), " redundant get() call on smart pointer" )
Original file line number Diff line number Diff line change @@ -104,6 +104,10 @@ New check aliases
104
104
Changes in existing checks
105
105
^^^^^^^^^^^^^^^^^^^^^^^^^^
106
106
107
+ - Improved :doc: `readability-redundant-smartptr-get
108
+ <clang-tidy/checks/readability/redundant-smartptr-get>` check to
109
+ remove `-> `, when reduntant `get() ` is removed.
110
+
107
111
Removed checks
108
112
^^^^^^^^^^^^^^
109
113
Original file line number Diff line number Diff line change @@ -20,6 +20,23 @@ struct shared_ptr {
20
20
explicit operator bool () const noexcept ;
21
21
};
22
22
23
+ template <typename T>
24
+ struct vector {
25
+ vector ();
26
+ bool operator ==(const vector<T>& other) const ;
27
+ bool operator !=(const vector<T>& other) const ;
28
+ unsigned long size () const ;
29
+ bool empty () const ;
30
+
31
+ using iterator = T*;
32
+
33
+ iterator begin ();
34
+ iterator end ();
35
+
36
+ T* data;
37
+ unsigned long sz;
38
+ };
39
+
23
40
} // namespace std
24
41
25
42
struct Bar {
@@ -235,3 +252,34 @@ void Negative() {
235
252
if (MACRO (x) == nullptr )
236
253
;
237
254
}
255
+
256
+ void test_redundant_get () {
257
+ std::vector<std::shared_ptr<int >> v;
258
+ auto f = [](int ) {};
259
+ for (auto i = v.begin (); i != v.end (); ++i) {
260
+ f (*i->get ());
261
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
262
+ // CHECK-FIXES: f(**i);
263
+ }
264
+ }
265
+
266
+ struct Inner {
267
+ int a;
268
+ int *getValue () { return &a; }
269
+ };
270
+
271
+ struct Example {
272
+ Inner inner;
273
+ Inner* get () { return &inner; }
274
+ int *getValue () { return inner.getValue (); }
275
+ };
276
+
277
+ void test_redundant_get_with_member () {
278
+ std::vector<std::shared_ptr<Example>> v;
279
+ auto f = [](int ) {};
280
+ for (auto i = v.begin (); i != v.end (); ++i) {
281
+ f (*(*i).get ()->get ()->getValue ());
282
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
283
+ // CHECK-FIXES: f(**i->get()->getValue());
284
+ }
285
+ }
You can’t perform that action at this time.
0 commit comments