[clang-tools-extra] [clang-tidy] Add AnalyzeParameters option to misc-const-correctness (PR #171215)

Baranov Victor via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 9 11:05:17 PST 2025


================
@@ -135,20 +147,83 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) {
           .bind("function-decl");
 
   Finder->addMatcher(FunctionScope, this);
+
+  if (AnalyzeParameters) {
+    const auto ParamMatcher =
+        parmVarDecl(unless(CommonExcludeTypes),
+                    anyOf(hasType(referenceType()), hasType(pointerType())))
+            .bind("param-var");
+
+    // Match function parameters which could be 'const' if not modified later.
+    // Example: `void foo(int* ptr)` would match `int* ptr`.
+    const auto FunctionWithParams =
+        functionDecl(
+            hasBody(stmt().bind("scope-params")),
+            has(typeLoc(forEach(ParamMatcher))), unless(cxxMethodDecl()),
+            unless(isFunctionTemplateSpecialization()), unless(isTemplate()))
+            .bind("function-params");
+
+    Finder->addMatcher(FunctionWithParams, this);
+  }
+}
+
+static void addConstFixits(DiagnosticBuilder &Diag, const VarDecl *Variable,
----------------
vbvictor wrote:

For this to happen, we need to have function definition in user code, but one of function declaration in system headers.
If we skip fixits in system headers, the user will be left with inconsistent code that will likely lead to link errors (or something worse). IMHO, we should either fix everything or fix nothing and if function definition in user code, then I guess user does control system headers and can apply fix there too.

https://github.com/llvm/llvm-project/pull/171215


More information about the cfe-commits mailing list