[clang-tools-extra] [clang-tidy] Fix false positive in readability-non-const-parameter for dependent expression (PR #177345)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 22 03:55:16 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tools-extra
Author: mitchell (zeyi2)
<details>
<summary>Changes</summary>
Closes #<!-- -->176623
---
Full diff: https://github.com/llvm/llvm-project/pull/177345.diff
3 Files Affected:
- (modified) clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp (+9-5)
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
- (modified) clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp (+22)
``````````diff
diff --git a/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp b/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
index a8767db4a4ffc..2ecde56cd7af8 100644
--- a/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
@@ -26,11 +26,12 @@ void NonConstParameterCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(declRefExpr().bind("Ref"), this);
// Analyse parameter usage in function.
- Finder->addMatcher(stmt(anyOf(unaryOperator(hasAnyOperatorName("++", "--")),
- binaryOperator(), callExpr(), returnStmt(),
- cxxConstructExpr()))
- .bind("Mark"),
- this);
+ Finder->addMatcher(
+ stmt(anyOf(unaryOperator(hasAnyOperatorName("++", "--")),
+ binaryOperator(), callExpr(), returnStmt(), cxxConstructExpr(),
+ cxxUnresolvedConstructExpr()))
+ .bind("Mark"),
+ this);
Finder->addMatcher(varDecl(hasInitializer(anything())).bind("Mark"), this);
}
@@ -93,6 +94,9 @@ void NonConstParameterCheck::check(const MatchFinder::MatchResult &Result) {
markCanNotBeConst(Arg->IgnoreParenCasts(), false);
}
}
+ } else if (const auto *CE = dyn_cast<CXXUnresolvedConstructExpr>(S)) {
+ for (const auto *Arg : CE->arguments())
+ markCanNotBeConst(Arg->IgnoreParenCasts(), true);
} else if (const auto *R = dyn_cast<ReturnStmt>(S)) {
markCanNotBeConst(R->getRetValue(), true);
} else if (const auto *U = dyn_cast<UnaryOperator>(S)) {
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 5af634c77f54d..89639828d0efe 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -137,6 +137,10 @@ Changes in existing checks
now uses separate note diagnostics for each uninitialized enumerator, making
it easier to see which specific enumerators need explicit initialization.
+- Improved :doc:`readability-non-const-parameter
+ <clang-tidy/checks/readability/non-const-parameter>` check by avoiding false
+ positives on parameters used in dependent expressions.
+
Removed checks
^^^^^^^^^^^^^^
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp
index a118c320bdae9..82a7715b63ae7 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp
@@ -341,3 +341,25 @@ void constructLVRef(int *p) {
// CHECK-MESSAGES-NOT: warning: pointer parameter 'p' can be
Temp1 t(*p);
}
+
+template<bool>
+class A final {
+ char* sz_ = {};
+
+public:
+ explicit A(char* sz) noexcept : sz_(sz) {}
+ void f() { sz_ = {}; }
+};
+
+class B final {
+ char* sz_ = {};
+
+public:
+ explicit B(char* sz) noexcept : sz_(sz) {}
+ void f() { sz_ = {}; }
+};
+
+void gh176623() {
+ auto const _ = []<bool tc>(char* p) { auto _ = A<tc>(p); };
+ auto const _ = []<bool tc>(char* p) { auto _ = B(p); };
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/177345
More information about the cfe-commits
mailing list