Skip to content

clang-tidy: readability-redundant-smartptr-get does not remove (#97964) #100177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ void RedundantSmartptrGetCheck::check(const MatchFinder::MatchResult &Result) {
StringRef SmartptrText = Lexer::getSourceText(
CharSourceRange::getTokenRange(Smartptr->getSourceRange()),
*Result.SourceManager, getLangOpts());
// Check if the last two characters are "->" and remove them
if (SmartptrText.ends_with("->")) {
SmartptrText = SmartptrText.drop_back(2);
}
// Replace foo->get() with *foo, and foo.get() with foo.
std::string Replacement = Twine(IsPtrToPtr ? "*" : "", SmartptrText).str();
diag(GetCall->getBeginLoc(), "redundant get() call on smart pointer")
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ New check aliases
Changes in existing checks
^^^^^^^^^^^^^^^^^^^^^^^^^^

- Improved :doc:`readability-redundant-smartptr-get
<clang-tidy/checks/readability/redundant-smartptr-get>` check to
remove `->`, when reduntant `get()` is removed.

Removed checks
^^^^^^^^^^^^^^

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ struct shared_ptr {
explicit operator bool() const noexcept;
};

template <typename T>
struct vector {
vector();
bool operator==(const vector<T>& other) const;
bool operator!=(const vector<T>& other) const;
unsigned long size() const;
bool empty() const;

using iterator = T*;

iterator begin();
iterator end();

T* data;
unsigned long sz;
};

} // namespace std

struct Bar {
Expand Down Expand Up @@ -235,3 +252,34 @@ void Negative() {
if (MACRO(x) == nullptr)
;
}

void test_redundant_get() {
std::vector<std::shared_ptr<int>> v;
auto f = [](int) {};
for (auto i = v.begin(); i != v.end(); ++i) {
f(*i->get());
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
// CHECK-FIXES: f(**i);
}
}

struct Inner {
int a;
int *getValue() { return &a; }
};

struct Example {
Inner inner;
Inner* get() { return &inner; }
int *getValue() { return inner.getValue(); }
};

void test_redundant_get_with_member() {
std::vector<std::shared_ptr<Example>> v;
auto f = [](int) {};
for (auto i = v.begin(); i != v.end(); ++i) {
f(*(*i).get()->get()->getValue());
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: redundant get() call
// CHECK-FIXES: f(**i->get()->getValue());
}
}
Loading