https://github.com/NagyDonat updated 
https://github.com/llvm/llvm-project/pull/184388

From cce5a879b9ae7933f1d3400eb0a982f348a941b4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Don=C3=A1t=20Nagy?= <[email protected]>
Date: Tue, 3 Mar 2026 17:58:12 +0100
Subject: [PATCH 1/3] [clang-tidy] Don't report unnamed params for
 misc-const-correctness

Previously misc-const-correctness warned about non-const unnamed
parameters; but this commit excludes them because these warnings are not
actually useful. An unnamed parameter cannot be referenced at all, so
marking them as 'const' doesn't add additional information.

Also the diagnostic messages look awkward without a name.

Fixes #184330
---
 .../clang-tidy/misc/ConstCorrectnessCheck.cpp              | 7 +++++++
 .../checkers/misc/const-correctness-parameters.cpp         | 4 ++++
 2 files changed, 11 insertions(+)

diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp 
b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
index 7e388201bf79a..4d4805d7518e7 100644
--- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
@@ -207,6 +207,13 @@ void ConstCorrectnessCheck::check(const 
MatchFinder::MatchResult &Result) {
 
   assert(Variable && LocalScope && Function);
 
+  // If a variable (e.g. function parameter) is unnamed, don't report it. Being
+  // unnamed already guarantees that the variable can't be accessed, so
+  // 'const'ness (can't be modified) doesn't add extra information. Also, the
+  // messages would be awkward in this case.
+  if (Variable->getDeclName().isIdentifier() && Variable->getName().empty())
+    return;
+
   // It can not be guaranteed that the variable is declared isolated,
   // therefore a transformation might effect the other variables as well and
   // be incorrect. Parameters don't need this check - they receive values from
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-parameters.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-parameters.cpp
index 96d54b8441700..a9cfde7ec2c0f 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-parameters.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-parameters.cpp
@@ -509,3 +509,7 @@ void struct_ptr_param_modified(Bar** bp) {
   // CHECK-FIXES: void struct_ptr_param_modified(Bar** const bp) {
   (*bp)->mutating_method();
 }
+
+void unnamed_parameters(int, char *, Bar, Bar&, int[5]) {
+    // Unnamed parameters are never reported.
+}

From 3598a580f5b386519e489507b4fa94be57b40a7c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Don=C3=A1t=20Nagy?= <[email protected]>
Date: Wed, 4 Mar 2026 13:18:47 +0100
Subject: [PATCH 2/3] Update docs

---
 .../docs/clang-tidy/checks/misc/const-correctness.rst       | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git 
a/clang-tools-extra/docs/clang-tidy/checks/misc/const-correctness.rst 
b/clang-tools-extra/docs/clang-tidy/checks/misc/const-correctness.rst
index 0365df2aa4504..7a0c73e099494 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/misc/const-correctness.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/misc/const-correctness.rst
@@ -110,15 +110,15 @@ Options
 
   Enable or disable the analysis of function parameters, like
   ``void foo(int* ptr)``. Only reference and pointer parameters are analyzed.
-  Currently, member functions (including constructors) and lambdas are excluded
-  from the analysis. Default is `true`.
+  Unnamed parameters, member functions (including constructors) and lambdas are
+  excluded from the analysis. Default is `true`.
 
   .. code-block:: c++
 
     // Warning
     void function(int& param) {}
     // No warning
-    void function(const int& param) {}
+    void function(const int& param, int&) {}
 
 .. option:: WarnPointersAsValues
 

From b9a53575af1fab97dc744cf60a4e3229852e0938 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Don=C3=A1t=20Nagy?= <[email protected]>
Date: Wed, 4 Mar 2026 14:21:55 +0100
Subject: [PATCH 3/3] Add more tests

---
 .../misc/const-correctness-parameters.cpp     | 31 +++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-parameters.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-parameters.cpp
index a9cfde7ec2c0f..8ff099eadb5a9 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-parameters.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-parameters.cpp
@@ -510,6 +510,33 @@ void struct_ptr_param_modified(Bar** bp) {
   (*bp)->mutating_method();
 }
 
-void unnamed_parameters(int, char *, Bar, Bar&, int[5]) {
-    // Unnamed parameters are never reported.
+void unnamed_parameters(int&, char*, Bar, Bar&, int[5]) {
+  // Unnamed parameters are never reported.
+}
+
+void unnamed_and_named_irrelevant_params(const int& X, char*, Bar Z, Bar&, 
int[5]) {
+  // No report, all parameters are excluded for various reasons.
+}
+
+void unnamed_and_named_params(int& X, int&) {
+  // But a named parameter is reported even if it is next to an unnamed one.
+  // CHECK-MESSAGES: [[@LINE-2]]:31: warning: variable 'X' of type 'int &' can 
be declared 'const'
+  // CHECK-FIXES: void unnamed_and_named_params(int const& X, int&) {
+}
+
+// If the parameter is named in the definition, its type will be updated in all
+// declarations, inclunding ones where it is unnamed.
+void unnamed_in_decl_named_in_def(int&);
+// CHECK-FIXES: void unnamed_in_decl_named_in_def(int const&);
+
+void unnamed_in_decl_named_in_def(int& X) {
+  // CHECK-MESSAGES: [[@LINE-1]]:35: warning: variable 'X' of type 'int &' can 
be declared 'const'
+  // CHECK-FIXES: void unnamed_in_decl_named_in_def(int const& X) {
+}
+
+void named_in_decl_unnamed_in_def(int& X);
+
+void named_in_decl_unnamed_in_def(int&) {
+  // If the parameter is unnamed in the definition, it will not be reported, 
even
+  // if it is named in some declaration.
 }

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

Reply via email to