[clang-tools-extra] [clang-tidy] Overloaded Unresolved member function call can't be static (PR #191432)
Gaurav Dhingra via cfe-commits
cfe-commits at lists.llvm.org
Wed May 6 08:59:47 PDT 2026
https://github.com/gxyd updated https://github.com/llvm/llvm-project/pull/191432
>From 53379153367e36adf2bb8a36088b78ba6d65ede2 Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra <gauravdhingra.gxyd at gmail.com>
Date: Fri, 10 Apr 2026 19:55:21 +0530
Subject: [PATCH 1/8] [clang-tidy] Unresolve member function call can't be
static
readability-convert-member-functions-to-static incorrectly suggests
making overloaded member function, with lambda function call, as
static (false-positive)
Mark usage of "this" as true, when a call to "UnresolveMemberExpr"
is obvserved
Fixes #171626
---
.../readability/ConvertMemberFunctionsToStaticCheck.cpp | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStaticCheck.cpp b/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStaticCheck.cpp
index 40952b2fc9d0c..8b0debc78bb77 100644
--- a/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStaticCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStaticCheck.cpp
@@ -64,6 +64,11 @@ AST_MATCHER(CXXMethodDecl, usesThis) {
return false; // Stop traversal.
}
+ bool VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *E) {
+ Used = true;
+ return false;
+ }
+
// If we enter a class declaration, don't traverse into it as any usages of
// `this` will correspond to the nested class.
bool TraverseCXXRecordDecl(CXXRecordDecl *RD) { return true; }
>From 4087cf9ee29ed167c839c2f4f3cdfc4a68dc3ceb Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra <gauravdhingra.gxyd at gmail.com>
Date: Fri, 10 Apr 2026 20:07:06 +0530
Subject: [PATCH 2/8] add tests for overloaded unresolve with lambda call
---
...mber-functions-to-static-deducing-this.cpp | 24 +++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static-deducing-this.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static-deducing-this.cpp
index a6ca86dd7cefa..790c8f0f927cc 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static-deducing-this.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static-deducing-this.cpp
@@ -20,3 +20,27 @@ struct Hello {
void UnnamedExplicitObjectParam(this Hello &) {}
};
+
+
+class OverloadedUnresolvedWithAutoLambda {
+public:
+ void CallsFunctionVar();
+ void CallsOverloadedMethodWithArg(int a);
+ void OverloadedMethodWithoutArg();
+ void OverloadedMethodWithArg(int a) { };
+};
+
+void OverloadedUnresolvedWithAutoLambda::CallsFunctionVar() {
+ // CHECK-MESSAGES: :[[@LINE-1]]:42: warning: method 'CallsFunctionVar' can be made static [readability-convert-member-functions-to-static]
+ auto fun = [&](auto a) {
+ var(a);
+ };
+}
+
+void OverloadedUnresolvedWithAutoLambda::CallsOverloadedMethodWithArg(int a) {
+ auto fun = [&](auto b) {
+ OverloadedMethodWithArg(b);
+ };
+}
+
+void Var(int a) { }
>From 7e4383da88de69547ca3092d1e375defd001db05 Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra <gauravdhingra.gxyd at gmail.com>
Date: Sun, 12 Apr 2026 21:16:37 +0530
Subject: [PATCH 3/8] add ReleaseNotes entry
---
clang-tools-extra/docs/ReleaseNotes.rst | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index f70bf9e9f9eb8..0bf56708f2f92 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -480,6 +480,11 @@ Changes in existing checks
avoiding false positive on ``const`` member functions to static when they are
a part of const/non-const overload pair.
+- Improved :doc:`readability-convert-member-functions-to-static
+ <clang-tidy/checks/readability/convert-member-functions-to-static>` check to
+ correctly detect ``this`` usage within a member function with a call to an
+ overloaded method inside a generic lambda.
+
- Improved :doc:`readability-else-after-return
<clang-tidy/checks/readability/else-after-return>` check:
>From 914e34f844529ed26995a53ad5d4902a1e9bc123 Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra <gauravdhingra.gxyd at gmail.com>
Date: Wed, 22 Apr 2026 15:05:57 +0530
Subject: [PATCH 4/8] split release notes entries and improve grammar
used claude AI to help improve the grammar and I've ensured that it
does look technically correct
---
clang-tools-extra/docs/ReleaseNotes.rst | 13 ++++++-------
1 file changed, 6 insertions(+), 7 deletions(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 0bf56708f2f92..10a8249d318f3 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -476,14 +476,13 @@ Changes in existing checks
``empty`` function.
- Improved :doc:`readability-convert-member-functions-to-static
- <clang-tidy/checks/readability/convert-member-functions-to-static>` check by
- avoiding false positive on ``const`` member functions to static when they are
- a part of const/non-const overload pair.
+ <clang-tidy/checks/readability/convert-member-functions-to-static>` check:
-- Improved :doc:`readability-convert-member-functions-to-static
- <clang-tidy/checks/readability/convert-member-functions-to-static>` check to
- correctly detect ``this`` usage within a member function with a call to an
- overloaded method inside a generic lambda.
+ - Fixing a false positive where ``const`` member functions were incorrectly
+ flagged when they are part of a const/non-const overload pair.
+
+ - Correctly detecting ``this`` usage when a generic lambda calls an overloaded
+ member function.
- Improved :doc:`readability-else-after-return
<clang-tidy/checks/readability/else-after-return>` check:
>From d8e71c36167f551647cb4e3d5d747d40e661c8d1 Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra <gauravdhingra.gxyd at gmail.com>
Date: Thu, 23 Apr 2026 11:27:10 +0530
Subject: [PATCH 5/8] actually overload the methods (this fixes the tests)
---
.../convert-member-functions-to-static-deducing-this.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static-deducing-this.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static-deducing-this.cpp
index 790c8f0f927cc..df0b7e68bbfd2 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static-deducing-this.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static-deducing-this.cpp
@@ -26,8 +26,8 @@ class OverloadedUnresolvedWithAutoLambda {
public:
void CallsFunctionVar();
void CallsOverloadedMethodWithArg(int a);
- void OverloadedMethodWithoutArg();
- void OverloadedMethodWithArg(int a) { };
+ void OverloadedMethod();
+ void OverloadedMethod(int a) { };
};
void OverloadedUnresolvedWithAutoLambda::CallsFunctionVar() {
@@ -39,7 +39,7 @@ void OverloadedUnresolvedWithAutoLambda::CallsFunctionVar() {
void OverloadedUnresolvedWithAutoLambda::CallsOverloadedMethodWithArg(int a) {
auto fun = [&](auto b) {
- OverloadedMethodWithArg(b);
+ OverloadedMethod(b);
};
}
>From 3d4b8b47d363495f11193ea9164ff3d2a1f9671f Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra <gauravdhingra.gxyd at gmail.com>
Date: Thu, 23 Apr 2026 14:03:38 +0530
Subject: [PATCH 6/8] mark UnresolvedMemberExpr with "this" usage only for
implicit access
---
.../readability/ConvertMemberFunctionsToStaticCheck.cpp | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStaticCheck.cpp b/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStaticCheck.cpp
index 8b0debc78bb77..21ac0034c67ac 100644
--- a/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStaticCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStaticCheck.cpp
@@ -65,8 +65,11 @@ AST_MATCHER(CXXMethodDecl, usesThis) {
}
bool VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *E) {
- Used = true;
- return false;
+ if (E->isImplicitAccess()) {
+ Used = true;
+ return false;
+ }
+ return true; // Continue traversal.
}
// If we enter a class declaration, don't traverse into it as any usages of
>From 6b65da935eff3b58c25702fe680d50e51b46d71d Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra <gauravdhingra.gxyd at gmail.com>
Date: Thu, 23 Apr 2026 14:55:43 +0530
Subject: [PATCH 7/8] add more robust tests
---
...mber-functions-to-static-deducing-this.cpp | 104 +++++++++++++++++-
1 file changed, 98 insertions(+), 6 deletions(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static-deducing-this.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static-deducing-this.cpp
index df0b7e68bbfd2..42e0c39254265 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static-deducing-this.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static-deducing-this.cpp
@@ -21,26 +21,118 @@ struct Hello {
void UnnamedExplicitObjectParam(this Hello &) {}
};
+class BaseOverloadedAutoLambdaTest {
+public:
+ void BaseOverloadedMethod();
+ void BaseOverloadedMethod(int a) { };
+};
+
-class OverloadedUnresolvedWithAutoLambda {
+class OverloadedAutoLambdaTest : public BaseOverloadedAutoLambdaTest {
public:
- void CallsFunctionVar();
- void CallsOverloadedMethodWithArg(int a);
+ void CallFunVar();
+ void LambdaUsesThis1(int a);
+ void LambdaUsesThis2(int a);
+ void LambdaUsesThis3(int a);
+ void LambdaUsesThis4();
+ void LambdaUsesThis5();
+
+ void LambdaNoThis1(int a);
+ void LambdaNoThis2(int a);
+ void LambdaNoThis3(int a);
+ void LambdaNoThis4(int a);
+ void LambdaNoThis5(int a);
+
void OverloadedMethod();
void OverloadedMethod(int a) { };
+
+ template <typename T>
+ void TemplatedOverloadedMethod(T);
+ void TemplatedOverloadedMethod(int);
};
-void OverloadedUnresolvedWithAutoLambda::CallsFunctionVar() {
- // CHECK-MESSAGES: :[[@LINE-1]]:42: warning: method 'CallsFunctionVar' can be made static [readability-convert-member-functions-to-static]
+void OverloadedAutoLambdaTest::CallFunVar() {
+ // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: method 'CallFunVar' can be made static [readability-convert-member-functions-to-static]
auto fun = [&](auto a) {
var(a);
};
}
-void OverloadedUnresolvedWithAutoLambda::CallsOverloadedMethodWithArg(int a) {
+void OverloadedAutoLambdaTest::LambdaUsesThis1(int a) {
auto fun = [&](auto b) {
OverloadedMethod(b);
};
}
+void OverloadedAutoLambdaTest::LambdaUsesThis2(int a) {
+ auto fun = [&](auto b) {
+ this->OverloadedMethod(b);
+ };
+}
+
+void OverloadedAutoLambdaTest::LambdaNoThis1(int a) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: method 'LambdaNoThis1' can be made static [readability-convert-member-functions-to-static]
+ OverloadedAutoLambdaTest f;
+ auto fun = [&](auto b) {
+ f.OverloadedMethod(b);
+ };
+}
+
+void OverloadedAutoLambdaTest::LambdaUsesThis3(int a) {
+ OverloadedAutoLambdaTest g;
+ auto fun1 = [&](auto a) {
+ auto fun2 = [&](auto b) {
+ OverloadedMethod(b);
+ };
+ fun2(a);
+ };
+}
+
+void OverloadedAutoLambdaTest::LambdaNoThis2(int a) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: method 'LambdaNoThis2' can be made static [readability-convert-member-functions-to-static]
+ OverloadedAutoLambdaTest g;
+ auto fun1 = [&](auto a) {
+ auto fun2 = [&](auto b) {
+ g.OverloadedMethod(b);
+ };
+ fun2(a);
+ };
+}
+
+void OverloadedAutoLambdaTest::LambdaUsesThis4() {
+ auto fun = [&](auto a) {
+ OverloadedMethod(a);
+ };
+}
+
+void OverloadedAutoLambdaTest::LambdaNoThis3(int a) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: method 'LambdaNoThis3' can be made static [readability-convert-member-functions-to-static]
+ OverloadedAutoLambdaTest f;
+ auto fun = [&](auto b) {
+ f.OverloadedMethod(b);
+ };
+}
+
+void OverloadedAutoLambdaTest::LambdaNoThis4(int a) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: method 'LambdaNoThis4' can be made static [readability-convert-member-functions-to-static]
+ BaseOverloadedAutoLambdaTest f;
+ auto fun = [&](auto b) {
+ f.BaseOverloadedMethod(b);
+ };
+}
+
+void OverloadedAutoLambdaTest::LambdaNoThis5(int a) {
+ // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: method 'LambdaNoThis5' can be made static [readability-convert-member-functions-to-static]
+ OverloadedAutoLambdaTest f;
+ auto fun = [&](auto b) {
+ f.BaseOverloadedMethod(b);
+ };
+}
+
+void OverloadedAutoLambdaTest::LambdaUsesThis5() {
+ auto fun = [&](auto b) {
+ TemplatedOverloadedMethod(b);
+ };
+}
+
void Var(int a) { }
>From fb385f9803dfd45e93e661da5f0635d74618f707 Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra <gauravdhingra.gxyd at gmail.com>
Date: Wed, 6 May 2026 21:29:04 +0530
Subject: [PATCH 8/8] add tests using CHECK-FIXES as well to test `-fix`
---
.../convert-member-functions-to-static-deducing-this.cpp | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static-deducing-this.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static-deducing-this.cpp
index 42e0c39254265..0fb7bec28e161 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static-deducing-this.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static-deducing-this.cpp
@@ -31,6 +31,7 @@ class BaseOverloadedAutoLambdaTest {
class OverloadedAutoLambdaTest : public BaseOverloadedAutoLambdaTest {
public:
void CallFunVar();
+ // CHECK-FIXES: static void CallFunVar();
void LambdaUsesThis1(int a);
void LambdaUsesThis2(int a);
void LambdaUsesThis3(int a);
@@ -38,10 +39,15 @@ class OverloadedAutoLambdaTest : public BaseOverloadedAutoLambdaTest {
void LambdaUsesThis5();
void LambdaNoThis1(int a);
+ // CHECK-FIXES: static void LambdaNoThis1(int a);
void LambdaNoThis2(int a);
+ // CHECK-FIXES: static void LambdaNoThis2(int a);
void LambdaNoThis3(int a);
+ // CHECK-FIXES: static void LambdaNoThis3(int a);
void LambdaNoThis4(int a);
+ // CHECK-FIXES: static void LambdaNoThis4(int a);
void LambdaNoThis5(int a);
+ // CHECK-FIXES: static void LambdaNoThis5(int a);
void OverloadedMethod();
void OverloadedMethod(int a) { };
More information about the cfe-commits
mailing list