https://github.com/davidmenggx created https://github.com/llvm/llvm-project/pull/228120
The standard fixes the signature of `main`, so suggesting `const` for its parameters is a false positive. The check warned on `char **argv` but not on `char *argv[]`. The array form did not warn because the check does not analyze array-style parameters. The check now skips all parameters of `main`. Local variables inside `main` are still checked. Fixes https://github.com/llvm/llvm-project/issues/227988 >From 564de6c0fca84472801dd3cb24e1c28d61ca5c51 Mon Sep 17 00:00:00 2001 From: David Meng <[email protected]> Date: Thu, 1 Oct 2026 08:49:04 -0700 Subject: [PATCH] [clang-tidy] Ignore parameters of `main` in `misc-const-correctness` The standard fixes the signature of `main`, so suggesting `const` for its parameters is a false positive. The check warned on `char **argv` but not on `char *argv[]`. The array form did not warn because the check does not analyze array-style parameters. The check now skips all parameters of `main`. Local variables inside `main` are still checked. Fixes https://github.com/llvm/llvm-project/issues/227988 --- .../clang-tidy/misc/ConstCorrectnessCheck.cpp | 4 ++-- clang-tools-extra/docs/ReleaseNotes.md | 3 +++ .../checks/misc/const-correctness.rst | 4 ++-- .../const-correctness-parameters-main.cpp | 23 +++++++++++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-parameters-main.cpp diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp index 57c2bda6c3fd5..90919555d447e 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp @@ -191,8 +191,8 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { const auto FunctionWithParams = functionDecl( hasBody(stmt().bind("scope")), has(typeLoc(forEach(ParamMatcher))), - unless(cxxMethodDecl()), unless(isFunctionTemplateSpecialization()), - unless(isTemplate())) + unless(cxxMethodDecl()), unless(isMain()), + unless(isFunctionTemplateSpecialization()), unless(isTemplate())) .bind("function-decl"); Finder->addMatcher(FunctionWithParams, this); diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md index c2ee1b06c43e0..41aa783cabd24 100644 --- a/clang-tools-extra/docs/ReleaseNotes.md +++ b/clang-tools-extra/docs/ReleaseNotes.md @@ -218,6 +218,9 @@ infrastructure are described first, followed by tool-specific sections. - No longer diagnoses variables declared with `decltype(auto)`, where the suggested `const` does not compile. + - No longer diagnoses parameters of `main`, whose signature is fixed by the + standard. + - Fixed an infinite loop in {doc}`misc-multiple-inheritance <clang-tidy/checks/misc/multiple-inheritance>` when checking a class that inherits from itself or has a circular inheritance graph. 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 ce699e06b9276..0a1eaf0e45a57 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 @@ -122,8 +122,8 @@ Options Enable or disable the analysis of function parameters, like ``void foo(int* ptr)``. Only reference and pointer parameters are analyzed. - Unnamed parameters, member functions (including constructors) and lambdas are - excluded from the analysis. Default is `true`. + Unnamed parameters, parameters of ``main``, member functions (including + constructors) and lambdas are excluded from the analysis. Default is `true`. .. code-block:: c++ diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-parameters-main.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-parameters-main.cpp new file mode 100644 index 0000000000000..9582ed892b734 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-parameters-main.cpp @@ -0,0 +1,23 @@ +// RUN: %check_clang_tidy %s misc-const-correctness %t \ +// RUN: -config='{CheckOptions: {misc-const-correctness.WarnPointersAsValues: true}}' +// RUN: %check_clang_tidy %s misc-const-correctness %t \ +// RUN: -config='{CheckOptions: {misc-const-correctness.WarnPointersAsValues: true}}' \ +// RUN: -- -DARRAY_SYNTAX + +namespace ns { +int main(int argc, char **argv) { + // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: pointee of variable 'argv' of type 'char **' can be declared 'const' + // CHECK-MESSAGES: :[[@LINE-2]]:20: warning: variable 'argv' of type 'char **' can be declared 'const' + return 0; +} +} // namespace ns + +#ifdef ARRAY_SYNTAX +int main(int argc, char *argv[], char *envp[]) { +#else +int main(int argc, char **argv, char **envp) { +#endif + int n = argc; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: variable 'n' of type 'int' can be declared 'const' + return n; +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
