Skip to content

Commit 5b8b6d4

Browse files
fubowenflovent
authored andcommitted
[clang-tidy] fix false positives with deducing this in readability-convert-member-functions-to-static check
1 parent eed98e1 commit 5b8b6d4

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ AST_MATCHER(CXXMethodDecl, usesThis) {
6262
return false; // Stop traversal.
6363
}
6464

65+
bool VisitDeclRefExpr(const DeclRefExpr *E) {
66+
if (const auto *PVD = dyn_cast_if_present<ParmVarDecl>(E->getDecl());
67+
PVD && PVD->isExplicitObjectParameter()) {
68+
Used = true;
69+
return false; // Stop traversal.
70+
}
71+
72+
return true;
73+
}
74+
6575
// If we enter a class declaration, don't traverse into it as any usages of
6676
// `this` will correspond to the nested class.
6777
bool TraverseCXXRecordDecl(CXXRecordDecl *RD) { return true; }

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ Changes in existing checks
252252
tolerating fix-it breaking compilation when functions is used as pointers
253253
to avoid matching usage of functions within the current compilation unit.
254254

255+
- Improved :doc:`readability-convert-member-functions-to-static
256+
<clang-tidy/checks/readability/convert-member-functions-to-static>` check by
257+
fixing false positives on member functions with an explicit object parameter.
258+
255259
- Improved :doc:`readability-math-missing-parentheses
256260
<clang-tidy/checks/readability/math-missing-parentheses>` check by fixing
257261
false negatives where math expressions are the operand of assignment operators
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %check_clang_tidy -std=c++23 %s readability-convert-member-functions-to-static %t
2+
3+
namespace std{
4+
class string {};
5+
void println(const char *format, const std::string &str) {}
6+
}
7+
8+
namespace PR141381 {
9+
struct Hello {
10+
std::string str_;
11+
12+
void hello(this Hello &self) { std::println("Hello, {0}!", self.str_); }
13+
};
14+
}

0 commit comments

Comments
 (0)