https://github.com/gxyd created https://github.com/llvm/llvm-project/pull/191712
Don't suggest conversion of overloaded + same signature methods
differing in `const`ness to replace the `const` method with `static`.
E.g.;
```
void S::f(); // method1
void S::f() const; // method2
```
method2 can't have it's `const` replaced with `static`.
Fixes #149152
>From 37c1904c6030c79d3fa52c6e410a090cbbcd7cae Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra <[email protected]>
Date: Sun, 12 Apr 2026 19:05:58 +0530
Subject: [PATCH 1/3] [clang-tidy] Fix false positive in
readability-convert-member-functions-to-static for const overloads
Don't suggest conversion of overloaded + same signature methods
differing in `const`ness to replace the `const` method with `static`.
E.g.;
```
void S::f(); // method1
void S::f() const; // method2
```
method2 can't have it's `const` replaced with `static`.
Fixes #149152
---
.../ConvertMemberFunctionsToStaticCheck.cpp | 31 ++++++++++++++++++-
.../convert-member-functions-to-static.cpp | 28 +++++++++++++++++
2 files changed, 58 insertions(+), 1 deletion(-)
diff --git
a/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStaticCheck.cpp
b/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStaticCheck.cpp
index 4bca9686e94e1..a0066a58f6f96 100644
---
a/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStaticCheck.cpp
+++
b/clang-tools-extra/clang-tidy/readability/ConvertMemberFunctionsToStaticCheck.cpp
@@ -76,6 +76,35 @@ AST_MATCHER(CXXMethodDecl, usesThis) {
return UsageOfThis.Used;
}
+static bool hasSameParameterTypes(const CXXMethodDecl &MD1,
+ const CXXMethodDecl &MD2) {
+ if (MD1.getNumParams() != MD2.getNumParams())
+ return false;
+ for (unsigned I = 0, E = MD1.getNumParams(); I < E; ++I)
+ if (MD1.getParamDecl(I)->getType().getCanonicalType() !=
+ MD2.getParamDecl(I)->getType().getCanonicalType())
+ return false;
+ return true;
+}
+
+AST_MATCHER(CXXMethodDecl, hasNonConstOverload) {
+ const auto *Method = &Node;
+ const DeclContext::lookup_result LookupResult =
+ Method->getParent()->lookup(Method->getNameInfo().getName());
+ if (LookupResult.isSingleResult())
+ return false;
+
+ for (const Decl *D : LookupResult) {
+ if (const auto *Overload = dyn_cast<CXXMethodDecl>(D)) {
+ if (Overload != Method && !Overload->isConst() &&
+ hasSameParameterTypes(*Method, *Overload)) {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
} // namespace
void ConvertMemberFunctionsToStaticCheck::registerMatchers(
@@ -87,7 +116,7 @@ void ConvertMemberFunctionsToStaticCheck::registerMatchers(
isVirtual(), isStatic(), hasTrivialBody(),
isOverloadedOperator(),
cxxConstructorDecl(), cxxDestructorDecl(), cxxConversionDecl(),
isExplicitObjectMemberFunction(), isTemplate(),
- isDependentContext(),
+ isDependentContext(), allOf(isConst(), hasNonConstOverload()),
ofClass(anyOf(
isLambda(),
hasAnyDependentBases()) // Method might become virtual
diff --git
a/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static.cpp
b/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static.cpp
index f9330388c1174..7d0eb858b9daa 100644
---
a/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static.cpp
+++
b/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static.cpp
@@ -235,3 +235,31 @@ struct NoFixitInMacro {
return;
}
};
+
+
+struct OverloadedMethods {
+ void f() {
+ this->i++;
+ }
+ void f() const {
+ ;
+ };
+
+ void g(int) {
+ this->i++;
+ }
+ void g(int) const {
+ ;
+ };
+
+ void h(int) {
+ this->i++;
+ };
+ void h(float) const {
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: method 'h' can be made static
+ // CHECK-FIXES: static void h(float) {
+ ;
+ };
+
+ int i = 0;
+};
>From bfcfe0e9014ad42fe71e4167e4ecb4067c381688 Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra <[email protected]>
Date: Sun, 12 Apr 2026 19:20:18 +0530
Subject: [PATCH 2/3] 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 02315415b975f..3c26e47926f05 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -412,6 +412,11 @@ Changes in existing checks
- Reduce verbosity by removing the note indicating source location of the
``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 with same signature.
+
- Improved :doc:`readability-else-after-return
<clang-tidy/checks/readability/else-after-return>` check:
>From 697832378ff9cd701f3858c4fdd49e9b8890c116 Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra <[email protected]>
Date: Sun, 12 Apr 2026 19:22:04 +0530
Subject: [PATCH 3/3] add one more test case
---
.../readability/convert-member-functions-to-static.cpp | 7 +++++++
1 file changed, 7 insertions(+)
diff --git
a/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static.cpp
b/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static.cpp
index 7d0eb858b9daa..7681e780f4624 100644
---
a/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static.cpp
+++
b/clang-tools-extra/test/clang-tidy/checkers/readability/convert-member-functions-to-static.cpp
@@ -261,5 +261,12 @@ struct OverloadedMethods {
;
};
+ void j() {
+ this->i++;
+ }
+ int j() const {
+ ;
+ }
+
int i = 0;
};
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits