[clang-tools-extra] [clang-tidy] make `misc-const-correctness` work with `auto` variables and lambdas (PR #157319)
Victor Chernyakin via cfe-commits
cfe-commits at lists.llvm.org
Sun Jan 11 12:38:50 PST 2026
https://github.com/localspook updated https://github.com/llvm/llvm-project/pull/157319
>From e28e9e234b6165b49884cf254e1fb4efe44fe756 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Sat, 6 Sep 2025 17:35:36 -0700
Subject: [PATCH 1/6] [clang-tidy] make `misc-const-correctness` work with
`auto` variables and lambdas
---
.../clang-tidy/misc/ConstCorrectnessCheck.cpp | 12 ++------
clang-tools-extra/docs/ReleaseNotes.rst | 4 +++
.../const-correctness-pointer-as-values.cpp | 6 +++-
.../misc/const-correctness-templates.cpp | 6 ++++
.../const-correctness-transform-values.cpp | 6 ----
.../misc/const-correctness-values.cpp | 28 ++++++++++++++++---
6 files changed, 42 insertions(+), 20 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
index b32507d66cbac..390046873f2d7 100644
--- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
@@ -98,17 +98,13 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) {
hasType(referenceType(pointee(hasCanonicalType(templateTypeParmType())))),
hasType(referenceType(pointee(substTemplateTypeParmType()))));
- auto AllowedTypeDecl = namedDecl(
+ const auto AllowedTypeDecl = namedDecl(
anyOf(matchers::matchesAnyListedName(AllowedTypes), usingShadowDecl()));
const auto AllowedType = hasType(qualType(
anyOf(hasDeclaration(AllowedTypeDecl), references(AllowedTypeDecl),
pointerType(pointee(hasDeclaration(AllowedTypeDecl))))));
- const auto AutoTemplateType = varDecl(
- anyOf(hasType(autoType()), hasType(referenceType(pointee(autoType()))),
- hasType(pointerType(pointee(autoType())))));
-
const auto FunctionPointerRef =
hasType(hasCanonicalType(referenceType(pointee(functionType()))));
@@ -117,10 +113,8 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) {
const auto LocalValDecl = varDecl(
isLocal(), hasInitializer(anything()),
unless(anyOf(ConstType, ConstReference, TemplateType,
- hasInitializer(isInstantiationDependent()), AutoTemplateType,
- RValueReference, FunctionPointerRef,
- hasType(cxxRecordDecl(isLambda())), isImplicit(),
- AllowedType)));
+ hasInitializer(isInstantiationDependent()), RValueReference,
+ FunctionPointerRef, isImplicit(), AllowedType)));
// Match the function scope for which the analysis of all local variables
// shall be run.
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 780e5b3fc21cf..1ae709c9a2744 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -185,6 +185,10 @@ Changes in existing checks
adding an option to allow pointer arithmetic via prefix/postfix increment or
decrement operators.
+- Improved :doc:`misc-const-correctness
+ <clang-tidy/checks/misc/const-correctness>` check to diagnose
+ variables declared with ``auto``.
+
- Improved :doc:`misc-header-include-cycle
<clang-tidy/checks/misc/header-include-cycle>` check performance.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-values.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-values.cpp
index 74be3dccc9daa..c675482513819 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-values.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-values.cpp
@@ -33,7 +33,9 @@ void range_for() {
int *p_local2[2] = {nullptr, nullptr};
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'p_local2' of type 'int *[2]' can be declared 'const'
// CHECK-FIXES: int *const p_local2[2]
- for (const auto *con_ptr : p_local2) {
+ for (const auto *p_local3 : p_local2) {
+ // CHECK-MESSAGES: [[@LINE-1]]:8: warning: variable 'p_local3' of type 'const int *' can be declared 'const'
+ // CHECK-FIXES: for (const auto *const p_local3 : p_local2)
}
}
@@ -62,6 +64,8 @@ void EmitProtocolMethodList(T &&Methods) {
// CHECK-FIXES: SmallVector<const int *> const p_local0
SmallVector<const int *> np_local0;
for (const auto *I : Methods) {
+ // CHECK-MESSAGES: [[@LINE-1]]:8: warning: variable 'I' of type 'const int *' can be declared 'const'
+ // CHECK-FIXES: for (const auto *const I : Methods)
if (I == nullptr)
np_local0.push_back(I);
}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-templates.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-templates.cpp
index 5a890f212a603..323d687cc4f24 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-templates.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-templates.cpp
@@ -10,6 +10,12 @@ template <typename T>
void type_dependent_variables() {
T value = 42;
auto &ref = value;
+ // CHECK-MESSAGES:[[@LINE-1]]:3: warning: variable 'ref' of type 'int &' can be declared 'const'
+ // CHECK-FIXES: auto const&ref = value;
+ //
+ // FIXME: This is a false positive, the reference points to a template type
+ // and needs to be excluded from analysis. See the 'more_template_locals()'
+ // test in 'const-correctness-values.cpp' for more examples of the problem.
T &templateRef = value;
int value_int = 42;
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-transform-values.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-transform-values.cpp
index 190d8ecec4c59..46ecb521b74cf 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-transform-values.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-transform-values.cpp
@@ -14,12 +14,6 @@ int scoped;
float np_scoped = 1; // namespace variables are like globals
} // namespace foo
-// Lambdas should be ignored, because they do not follow the normal variable
-// semantic (e.g. the type is only known to the compiler).
-void lambdas() {
- auto Lambda = [](int i) { return i < 0; };
-}
-
void some_function(double, wchar_t);
void some_function(double np_arg0, wchar_t np_arg1) {
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp
index 17dcf12e2536c..726caa5ffc531 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp
@@ -27,10 +27,19 @@ int np_anonymous_global;
int p_anonymous_global = 43;
} // namespace
-// Lambdas should be ignored, because they do not follow the normal variable
-// semantic (e.g. the type is only known to the compiler).
void lambdas() {
auto Lambda = [](int i) { return i < 0; };
+ // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'Lambda' of type '{{.*}}' can be declared 'const'
+ // CHECK-FIXES: auto const Lambda
+
+ auto LambdaWithMutableCallOperator = []() mutable {};
+ LambdaWithMutableCallOperator();
+
+ int x = 0;
+ auto LambdaModifyingCapture = [&x] { ++x; };
+ // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'LambdaModifyingCapture' of type '{{.*}}' can be declared 'const'
+ // CHECK-FIXES: auto const LambdaModifyingCapture
+ LambdaModifyingCapture();
}
void some_function(double, wchar_t);
@@ -965,14 +974,23 @@ template <typename T>
T *return_ptr() { return &return_ref<T>(); }
void auto_usage_variants() {
+ auto auto_int = int{};
+ // CHECK-MESSAGES:[[@LINE-1]]:3: warning: variable 'auto_int' of type 'int' can be declared 'const'
+ // CHECK-FIXES: auto const auto_int
+
auto auto_val0 = int{};
// CHECK-FIXES-NOT: auto const auto_val0
auto &auto_val1 = auto_val0;
+ // CHECK-MESSAGES:[[@LINE-1]]:3: warning: variable 'auto_val1' of type 'int &' can be declared 'const'
+ // CHECK-FIXES: auto const&auto_val1
auto *auto_val2 = &auto_val0;
auto auto_ref0 = return_ref<int>();
- // CHECK-FIXES-NOT: auto const auto_ref0
- auto &auto_ref1 = return_ref<int>(); // Bad
+ // CHECK-MESSAGES:[[@LINE-1]]:3: warning: variable 'auto_ref0' of type 'int' can be declared 'const'
+ // CHECK-FIXES: auto const auto_ref0
+ auto &auto_ref1 = return_ref<int>();
+ // CHECK-MESSAGES:[[@LINE-1]]:3: warning: variable 'auto_ref1' of type 'int &' can be declared 'const'
+ // CHECK-FIXES: auto const&auto_ref1
auto *auto_ref2 = return_ptr<int>();
auto auto_ptr0 = return_ptr<int>();
@@ -984,6 +1002,8 @@ void auto_usage_variants() {
auto auto_td0 = MyTypedef{};
// CHECK-FIXES-NOT: auto const auto_td0
auto &auto_td1 = auto_td0;
+ // CHECK-MESSAGES:[[@LINE-1]]:3: warning: variable 'auto_td1' of type 'MyTypedef &' (aka 'int &') can be declared 'const'
+ // CHECK-FIXES: auto const&auto_td1
auto *auto_td2 = &auto_td0;
}
>From 07a85c1fd3e0fd62651b2772cd0a7c51b0d6492b Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Sun, 7 Sep 2025 12:47:48 -0700
Subject: [PATCH 2/6] make comment less confusing
---
.../clang-tidy/checkers/misc/const-correctness-templates.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-templates.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-templates.cpp
index 323d687cc4f24..98bf27033c3b5 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-templates.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-templates.cpp
@@ -9,14 +9,14 @@
template <typename T>
void type_dependent_variables() {
T value = 42;
+ T &templateRef = value;
+
auto &ref = value;
// CHECK-MESSAGES:[[@LINE-1]]:3: warning: variable 'ref' of type 'int &' can be declared 'const'
// CHECK-FIXES: auto const&ref = value;
- //
// FIXME: This is a false positive, the reference points to a template type
// and needs to be excluded from analysis. See the 'more_template_locals()'
// test in 'const-correctness-values.cpp' for more examples of the problem.
- T &templateRef = value;
int value_int = 42;
// CHECK-MESSAGES:[[@LINE-1]]:3: warning: variable 'value_int' of type 'int' can be declared 'const'
>From c8043f835d746e435c45521c4cf8acc2f7714597 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Sun, 7 Sep 2025 13:07:11 -0700
Subject: [PATCH 3/6] add tests ensuring rvalue references aren't diagnosed
---
.../clang-tidy/checkers/misc/const-correctness-values.cpp | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp
index 726caa5ffc531..c0020a2b3c182 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp
@@ -94,6 +94,11 @@ void ignore_reference_to_pointers() {
int *&np_local1 = np_local0;
}
+void ignore_rvalue_references() {
+ int &&np_local0 = 42;
+ auto &&np_local1 = 42;
+}
+
void some_lambda_environment_capture_all_by_reference(double np_arg0) {
int np_local0 = 0;
int p_local0 = 1;
>From ef1540510ba6f360f2e2329bb559814562dc7562 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Sat, 20 Dec 2025 11:45:46 -0700
Subject: [PATCH 4/6] Add options to control analyzing auto variables and
lambdas
---
.../clang-tidy/misc/ConstCorrectnessCheck.cpp | 14 +++++++++++++-
.../clang-tidy/misc/ConstCorrectnessCheck.h | 2 ++
.../clang-tidy/checks/misc/const-correctness.rst | 12 ++++++++++++
3 files changed, 27 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
index 27f4b7447f1f4..358189e238c7b 100644
--- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
@@ -33,6 +33,10 @@ AST_MATCHER(ReferenceType, isSpelledAsLValue) {
return Node.isSpelledAsLValue();
}
AST_MATCHER(Type, isDependentType) { return Node.isDependentType(); }
+
+AST_MATCHER(TypeLoc, hasContainedAutoType) {
+ return !TypeLoc.getContainedAutoTypeLoc().isNull();
+}
} // namespace
ConstCorrectnessCheck::ConstCorrectnessCheck(StringRef Name,
@@ -41,6 +45,8 @@ ConstCorrectnessCheck::ConstCorrectnessCheck(StringRef Name,
AnalyzePointers(Options.get("AnalyzePointers", true)),
AnalyzeReferences(Options.get("AnalyzeReferences", true)),
AnalyzeValues(Options.get("AnalyzeValues", true)),
+ AnalyzeAutoVariables(Options.get("AnalyzeAutoVariables", true)),
+ AnalyzeLambdas(Options.get("AnalyzeLambdas", true)),
WarnPointersAsPointers(Options.get("WarnPointersAsPointers", true)),
WarnPointersAsValues(Options.get("WarnPointersAsValues", false)),
@@ -66,6 +72,8 @@ void ConstCorrectnessCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "AnalyzePointers", AnalyzePointers);
Options.store(Opts, "AnalyzeReferences", AnalyzeReferences);
Options.store(Opts, "AnalyzeValues", AnalyzeValues);
+ Options.store(Opts, "AnalyzeAutoVariables", AnalyzeAutoVariables);
+ Options.store(Opts, "AnalyzeLambdas", AnalyzeLambdas);
Options.store(Opts, "WarnPointersAsPointers", WarnPointersAsPointers);
Options.store(Opts, "WarnPointersAsValues", WarnPointersAsValues);
@@ -114,7 +122,11 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) {
isLocal(), hasInitializer(anything()),
unless(anyOf(ConstType, ConstReference, TemplateType,
hasInitializer(isInstantiationDependent()), RValueReference,
- FunctionPointerRef, isImplicit(), AllowedType)));
+ FunctionPointerRef, isImplicit(), AllowedType)),
+ AnalyzeLambdas ? TypeMatcher(anything())
+ : unless(hasType(cxxRecordDecl(isLambda()))),
+ AnalyzeAutoVariables ? TypeLocMatcher(anything())
+ : unless(hasTypeLoc(hasContainedAutoType())));
// Match the function scope for which the analysis of all local variables
// shall be run.
diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h
index fafcac407e029..449aaaf93c0c0 100644
--- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h
+++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h
@@ -41,6 +41,8 @@ class ConstCorrectnessCheck : public ClangTidyCheck {
const bool AnalyzePointers;
const bool AnalyzeReferences;
const bool AnalyzeValues;
+ const bool AnalyzeAutoVariables;
+ const bool AnalyzeLambdas;
const bool WarnPointersAsPointers;
const bool WarnPointersAsValues;
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 01f3b75fc1ffa..9caa5d2478472 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
@@ -104,6 +104,18 @@ Options
:option:`WarnPointersAsValues` and :option:`WarnPointersAsPointers`.
Default is `true`.
+.. option:: AnalyzeAutoVariables
+
+ Enable or disable the analysis of variables declared with ``auto``,
+ such as ``auto i = 10;`` or ``auto *ptr = &i``. Default is `true`.
+
+.. option:: AnalyzeLambdas
+
+ Enable or disable the analysis of lambda variables, like
+ ``auto f = [] { return 10; };``. For this option to have any
+ effect, `AnalyzeAutoVariables` must be `true` as well.
+ Default is `true`.
+
.. option:: WarnPointersAsValues
This option enables the suggestion for ``const`` of the pointer itself.
>From 1d3539c6da53d63fee7eac3e72c0d6c26d8aabf6 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Sat, 20 Dec 2025 11:58:16 -0700
Subject: [PATCH 5/6] Fix build
---
.../clang-tidy/misc/ConstCorrectnessCheck.cpp | 13 +++++++-----
.../checkers/misc/const-correctness-auto.cpp | 20 +++++++++++++++++++
.../misc/const-correctness-lambdas.cpp | 14 +++++++++++++
3 files changed, 42 insertions(+), 5 deletions(-)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-auto.cpp
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-lambdas.cpp
diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
index 358189e238c7b..ee71464ade695 100644
--- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
@@ -16,6 +16,7 @@
#include <cassert>
using namespace clang::ast_matchers;
+using namespace clang::ast_matchers::internal;
namespace clang::tidy::misc {
@@ -35,7 +36,7 @@ AST_MATCHER(ReferenceType, isSpelledAsLValue) {
AST_MATCHER(Type, isDependentType) { return Node.isDependentType(); }
AST_MATCHER(TypeLoc, hasContainedAutoType) {
- return !TypeLoc.getContainedAutoTypeLoc().isNull();
+ return !Node.getContainedAutoTypeLoc().isNull();
}
} // namespace
@@ -123,10 +124,12 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) {
unless(anyOf(ConstType, ConstReference, TemplateType,
hasInitializer(isInstantiationDependent()), RValueReference,
FunctionPointerRef, isImplicit(), AllowedType)),
- AnalyzeLambdas ? TypeMatcher(anything())
- : unless(hasType(cxxRecordDecl(isLambda()))),
- AnalyzeAutoVariables ? TypeLocMatcher(anything())
- : unless(hasTypeLoc(hasContainedAutoType())));
+ AnalyzeLambdas
+ ? Matcher<VarDecl>(anything())
+ : Matcher<VarDecl>(unless(hasType(cxxRecordDecl(isLambda())))),
+ AnalyzeAutoVariables
+ ? Matcher<VarDecl>(anything())
+ : Matcher<VarDecl>(unless(hasTypeLoc(hasContainedAutoType()))));
// Match the function scope for which the analysis of all local variables
// shall be run.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-auto.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-auto.cpp
new file mode 100644
index 0000000000000..243686435d8f4
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-auto.cpp
@@ -0,0 +1,20 @@
+// RUN: %check_clang_tidy %s misc-const-correctness %t \
+// RUN: -config='{CheckOptions: \
+// RUN: {misc-const-correctness.AnalyzeAutoVariables: false,\
+// RUN: misc-const-correctness.AnalyzeValues: true,\
+// RUN: misc-const-correctness.AnalyzeLambdas: true,\
+// RUN: misc-const-correctness.WarnPointersAsValues: true,\
+// RUN: misc-const-correctness.WarnPointersAsPointers: true,\
+// RUN: misc-const-correctness.TransformPointersAsValues: true}}' \
+// RUN: -- -fno-delayed-template-parsing
+
+void auto_types_ignored() {
+ int i = 0;
+ // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'i' of type 'int' can be declared 'const'
+ // CHECK-FIXES: int const i = 0;
+
+ auto auto_i = 0;
+ auto& auto_ref = auto_i;
+ auto auto_lambda = [] {};
+ auto *auto_ptr = nullptr;
+}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-lambdas.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-lambdas.cpp
new file mode 100644
index 0000000000000..b037a8d6a113a
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-lambdas.cpp
@@ -0,0 +1,14 @@
+// RUN: %check_clang_tidy %s misc-const-correctness %t \
+// RUN: -config='{CheckOptions: \
+// RUN: {misc-const-correctness.AnalyzeAutoVariables: true,\
+// RUN: misc-const-correctness.AnalyzeValues: true,\
+// RUN: misc-const-correctness.AnalyzeLambdas: false}}' \
+// RUN: -- -fno-delayed-template-parsing
+
+void lambdas_ignored_but_other_auto_variables_diagnosed() {
+ auto i = 0;
+ // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'i' of type 'int' can be declared 'const'
+ // CHECK-FIXES: auto const i = 0;
+
+ auto lambda = [] {};
+}
>From b63136185f1928ca16ce22bb3cce3839c4015558 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Sun, 11 Jan 2026 12:36:30 -0800
Subject: [PATCH 6/6] Fix tests; add test with reassigned lambda
---
.../checkers/misc/const-correctness-auto.cpp | 2 +-
.../const-correctness-pointer-as-values.cpp | 4 +--
.../misc/const-correctness-values.cpp | 25 +++++++++++--------
3 files changed, 18 insertions(+), 13 deletions(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-auto.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-auto.cpp
index 243686435d8f4..210e8ae74d806 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-auto.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-auto.cpp
@@ -16,5 +16,5 @@ void auto_types_ignored() {
auto auto_i = 0;
auto& auto_ref = auto_i;
auto auto_lambda = [] {};
- auto *auto_ptr = nullptr;
+ auto *auto_ptr = static_cast<void *>(nullptr);
}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-values.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-values.cpp
index e048fd1f66b3d..1c5e37053aa12 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-values.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-values.cpp
@@ -35,7 +35,7 @@ void range_for() {
// CHECK-FIXES: int *const p_local2[2] = {nullptr, nullptr};
for (const auto *p_local3 : p_local2) {
// CHECK-MESSAGES: [[@LINE-1]]:8: warning: variable 'p_local3' of type 'const int *' can be declared 'const'
- // CHECK-FIXES: for (const auto *const p_local3 : p_local2)
+ // CHECK-FIXES: for (const auto *const p_local3 : p_local2) {
}
}
@@ -64,7 +64,7 @@ void EmitProtocolMethodList(T &&Methods) {
SmallVector<const int *> np_local0;
for (const auto *I : Methods) {
// CHECK-MESSAGES: [[@LINE-1]]:8: warning: variable 'I' of type 'const int *' can be declared 'const'
- // CHECK-FIXES: for (const auto *const I : Methods)
+ // CHECK-FIXES: for (const auto *const I : Methods) {
if (I == nullptr)
np_local0.push_back(I);
}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp
index 2f11464885a4f..6b993d43c96b6 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp
@@ -30,15 +30,20 @@ int p_anonymous_global = 43;
void lambdas() {
auto Lambda = [](int i) { return i < 0; };
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'Lambda' of type '{{.*}}' can be declared 'const'
- // CHECK-FIXES: auto const Lambda
+ // CHECK-FIXES: auto const Lambda = [](int i) { return i < 0; };
auto LambdaWithMutableCallOperator = []() mutable {};
LambdaWithMutableCallOperator();
+#if __cplusplus >= 202002L
+ auto ReassignedLambda = [] {};
+ ReassignedLambda = {};
+#endif
+
int x = 0;
auto LambdaModifyingCapture = [&x] { ++x; };
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'LambdaModifyingCapture' of type '{{.*}}' can be declared 'const'
- // CHECK-FIXES: auto const LambdaModifyingCapture
+ // CHECK-FIXES: auto const LambdaModifyingCapture = [&x] { ++x; };
LambdaModifyingCapture();
}
@@ -981,34 +986,34 @@ T *return_ptr() { return &return_ref<T>(); }
void auto_usage_variants() {
auto auto_int = int{};
// CHECK-MESSAGES:[[@LINE-1]]:3: warning: variable 'auto_int' of type 'int' can be declared 'const'
- // CHECK-FIXES: auto const auto_int
+ // CHECK-FIXES: auto const auto_int = int{};
auto auto_val0 = int{};
- // CHECK-FIXES-NOT: auto const auto_val0
+ // CHECK-FIXES-NOT: auto const auto_val0 = int{};
auto &auto_val1 = auto_val0;
// CHECK-MESSAGES:[[@LINE-1]]:3: warning: variable 'auto_val1' of type 'int &' can be declared 'const'
- // CHECK-FIXES: auto const&auto_val1
+ // CHECK-FIXES: auto const&auto_val1 = auto_val0;
auto *auto_val2 = &auto_val0;
auto auto_ref0 = return_ref<int>();
// CHECK-MESSAGES:[[@LINE-1]]:3: warning: variable 'auto_ref0' of type 'int' can be declared 'const'
- // CHECK-FIXES: auto const auto_ref0
+ // CHECK-FIXES: auto const auto_ref0 = return_ref<int>();
auto &auto_ref1 = return_ref<int>();
// CHECK-MESSAGES:[[@LINE-1]]:3: warning: variable 'auto_ref1' of type 'int &' can be declared 'const'
- // CHECK-FIXES: auto const&auto_ref1
+ // CHECK-FIXES: auto const&auto_ref1 = return_ref<int>();
auto *auto_ref2 = return_ptr<int>();
auto auto_ptr0 = return_ptr<int>();
- // CHECK-FIXES-NOT: auto const auto_ptr0
+ // CHECK-FIXES-NOT: auto const auto_ptr0 = return_ptr<int>();
auto &auto_ptr1 = auto_ptr0;
auto *auto_ptr2 = return_ptr<int>();
using MyTypedef = int;
auto auto_td0 = MyTypedef{};
- // CHECK-FIXES-NOT: auto const auto_td0
+ // CHECK-FIXES-NOT: auto const auto_td0 = MyTypedef{};
auto &auto_td1 = auto_td0;
// CHECK-MESSAGES:[[@LINE-1]]:3: warning: variable 'auto_td1' of type 'MyTypedef &' (aka 'int &') can be declared 'const'
- // CHECK-FIXES: auto const&auto_td1
+ // CHECK-FIXES: auto const&auto_td1 = auto_td0;
auto *auto_td2 = &auto_td0;
}
More information about the cfe-commits
mailing list