[clang-tools-extra] [clang-tidy] Ignore parameters of `main` in `misc-const-correctness` (PR #228120)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 1 08:55:26 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tools-extra
Author: David Meng (davidmenggx)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/228120.diff
4 Files Affected:
- (modified) clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp (+2-2)
- (modified) clang-tools-extra/docs/ReleaseNotes.md (+3)
- (modified) clang-tools-extra/docs/clang-tidy/checks/misc/const-correctness.rst (+2-2)
- (added) clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-parameters-main.cpp (+23)
``````````diff
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;
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/228120
More information about the cfe-commits
mailing list