Skip to content

Commit a985021

Browse files
clang-tidy: readability-redundant-smartptr-get does not remove (#97964)
modified ReleaseNotes
1 parent b1f263e commit a985021

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ New check aliases
104104
Changes in existing checks
105105
^^^^^^^^^^^^^^^^^^^^^^^^^^
106106

107+
- Improved :doc:`readability-simplify-boolean-expr
108+
<clang-tidy/checks/readability/simplify-boolean-expr>` check to avoid to emit
109+
warning for macro when IgnoreMacro option is enabled and improve messages
110+
when auto-fix does not work.
111+
107112
Removed checks
108113
^^^^^^^^^^^^^^
109114

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,47 @@ 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+
// Basic iterator implementation for testing
32+
struct iterator {
33+
T* ptr;
34+
iterator(T* p) : ptr(p) {}
35+
T& operator*() { return *ptr; }
36+
T* operator->() { return ptr; }
37+
iterator& operator++() {
38+
++ptr;
39+
return *this;
40+
}
41+
bool operator!=(const iterator& other) const { return ptr != other.ptr; }
42+
};
43+
44+
iterator begin();
45+
iterator end();
46+
47+
T* data;
48+
unsigned long sz;
49+
};
50+
51+
template <typename T>
52+
vector<T>::vector() : data(nullptr), sz(0) {}
53+
54+
template <typename T>
55+
typename vector<T>::iterator vector<T>::begin() {
56+
return iterator(data);
57+
}
58+
59+
template <typename T>
60+
typename vector<T>::iterator vector<T>::end() {
61+
return iterator(data + sz);
62+
}
63+
2364
} // namespace std
2465

2566
struct Bar {
@@ -235,3 +276,34 @@ void Negative() {
235276
if (MACRO(x) == nullptr)
236277
;
237278
}
279+
280+
void test_redundant_get() {
281+
std::vector<std::shared_ptr<int>> v;
282+
auto f = [](int) {};
283+
for (auto i = v.begin(); i != v.end(); ++i) {
284+
f(*i->get());
285+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
286+
// CHECK-FIXES: f(**i);
287+
}
288+
}
289+
290+
struct Inner {
291+
int a;
292+
int *getValue() { return &a; }
293+
};
294+
295+
struct Example {
296+
Inner inner;
297+
Inner* get() { return &inner; }
298+
int *getValue() { return inner.getValue(); }
299+
};
300+
301+
void test_redundant_get_with_member() {
302+
std::vector<std::shared_ptr<Example>> v;
303+
auto f = [](int) {};
304+
for (auto i = v.begin(); i != v.end(); ++i) {
305+
f(*(*i).get()->get()->getValue());
306+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
307+
// CHECK-FIXES: f(**i->get()->getValue());
308+
}
309+
}

0 commit comments

Comments
 (0)