Author: mitchell
Date: 2026-01-29T01:07:26+08:00
New Revision: 39431347f8691ce9487a620de342bd42c09ea934

URL: 
https://github.com/llvm/llvm-project/commit/39431347f8691ce9487a620de342bd42c09ea934
DIFF: 
https://github.com/llvm/llvm-project/commit/39431347f8691ce9487a620de342bd42c09ea934.diff

LOG: [clang-tidy] Fix false positive in readability-non-const-parameter for 
dependent expression (#177345)

Closes #176623

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    
clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp

Removed: 
    


################################################################################
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 754880bd1a381..d5837ae109401 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -171,6 +171,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..0079fa9e96434 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 V1 = []<bool tc>(char* p) { auto X = A<tc>(p); };
+    auto const V2 = []<bool tc>(char* p) { auto Y = B(p); };
+}


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to