[clang-tools-extra] [clang-tidy][misc-const-correctness] fix fp when using const array type. (PR #133018)
Congcong Cai via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 26 11:13:21 PDT 2025
https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/133018
>From c2defc601e2d8e42130600802ff330a0feb8b52a Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Tue, 25 Mar 2025 23:31:38 +0000
Subject: [PATCH 1/2] [clang-tidy][misc-const-correctness] fix fp when using
const array type.
Fixed: #132931
const array is immutable in C/C++ language design, we don't need to
check constness for it.
---
.../clang-tidy/misc/ConstCorrectnessCheck.cpp | 6 +++++-
clang-tools-extra/docs/ReleaseNotes.rst | 3 ++-
.../clang-tidy/checkers/misc/const-correctness-values.cpp | 8 ++++++++
3 files changed, 15 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
index 50e6722badf50..13eba246faf56 100644
--- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
@@ -89,6 +89,8 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) {
const auto ConstReference = hasType(references(isConstQualified()));
const auto RValueReference = hasType(
referenceType(anyOf(rValueReferenceType(), unless(isSpelledAsLValue()))));
+ const auto ConstArrayType =
+ hasType(arrayType(hasElementType(isConstQualified())));
const auto TemplateType = anyOf(
hasType(hasCanonicalType(templateTypeParmType())),
@@ -115,7 +117,7 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) {
// Example: `int i = 10` would match `int i`.
const auto LocalValDecl = varDecl(
isLocal(), hasInitializer(anything()),
- unless(anyOf(ConstType, ConstReference, TemplateType,
+ unless(anyOf(ConstType, ConstReference, ConstArrayType, TemplateType,
hasInitializer(isInstantiationDependent()), AutoTemplateType,
RValueReference, FunctionPointerRef,
hasType(cxxRecordDecl(isLambda())), isImplicit(),
@@ -161,6 +163,7 @@ void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) {
VariableCategory VC = VariableCategory::Value;
const QualType VT = Variable->getType();
+ VT->dump();
if (VT->isReferenceType()) {
VC = VariableCategory::Reference;
} else if (VT->isPointerType()) {
@@ -169,6 +172,7 @@ void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) {
if (ArrayT->getElementType()->isPointerType())
VC = VariableCategory::Pointer;
}
+ llvm::errs() << (int)VC << "\n";
auto CheckValue = [&]() {
// The scope is only registered if the analysis shall be run.
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index aa85105918ecf..7bbf2190ee262 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -146,7 +146,8 @@ Changes in existing checks
`AllowedTypes`, that excludes specified types from const-correctness
checking and fixing false positives when modifying variant by ``operator[]``
with template in parameters and supporting to check pointee mutation by
- `AnalyzePointers` option.
+ `AnalyzePointers` option and fixing false positives when using const array
+ type.
- Improved :doc:`misc-redundant-expression
<clang-tidy/checks/misc/redundant-expression>` check by providing additional
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 4cf78aeef5bd4..a80e1e1af1870 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
@@ -1007,3 +1007,11 @@ template <typename T> void f() {
x[T{}] = 3;
}
} // namespace gh127776_false_positive
+
+namespace gh132931_false_positive {
+using T = const int;
+void valid(int i) {
+ const int arr0[] = {1, 2, 3};
+ T arr1[] = {1, 2, 3};
+}
+} // namespace gh132931_false_positive
>From 166e4578536884a1ec7a97efabfd5d64e14895ba Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Thu, 27 Mar 2025 02:13:13 +0800
Subject: [PATCH 2/2] Update
clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
---
clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
index 13eba246faf56..ca04c83280412 100644
--- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp
@@ -172,7 +172,6 @@ void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) {
if (ArrayT->getElementType()->isPointerType())
VC = VariableCategory::Pointer;
}
- llvm::errs() << (int)VC << "\n";
auto CheckValue = [&]() {
// The scope is only registered if the analysis shall be run.
More information about the cfe-commits
mailing list