Skip to content

Commit 5297b75

Browse files
clang-tidy: readability-redundant-smartptr-get does not remove (#97964) (#100177)
added a check to remove '->' if exists added testcase and modified Release Notes
1 parent 6897004 commit 5297b75

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

clang-tools-extra/clang-tidy/readability/RedundantSmartptrGetCheck.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ void RedundantSmartptrGetCheck::check(const MatchFinder::MatchResult &Result) {
164164
StringRef SmartptrText = Lexer::getSourceText(
165165
CharSourceRange::getTokenRange(Smartptr->getSourceRange()),
166166
*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+
}
167171
// Replace foo->get() with *foo, and foo.get() with foo.
168172
std::string Replacement = Twine(IsPtrToPtr ? "*" : "", SmartptrText).str();
169173
diag(GetCall->getBeginLoc(), "redundant get() call on smart pointer")

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ New check aliases
104104
Changes in existing checks
105105
^^^^^^^^^^^^^^^^^^^^^^^^^^
106106

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+
107111
Removed checks
108112
^^^^^^^^^^^^^^
109113

clang-tools-extra/test/clang-tidy/checkers/readability/redundant-smartptr-get.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@ struct shared_ptr {
2020
explicit operator bool() const noexcept;
2121
};
2222

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+
2340
} // namespace std
2441

2542
struct Bar {
@@ -235,3 +252,34 @@ void Negative() {
235252
if (MACRO(x) == nullptr)
236253
;
237254
}
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+
}

0 commit comments

Comments
 (0)