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 <congcongcai0...@163.com> Date: Tue, 25 Mar 2025 23:31:38 +0000 Subject: [PATCH 1/4] [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 <congcongcai0...@163.com> Date: Thu, 27 Mar 2025 02:13:13 +0800 Subject: [PATCH 2/4] 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. >From 42466ed6d8584fc06354e11cdd052ef08544a285 Mon Sep 17 00:00:00 2001 From: Congcong Cai <congcongcai0...@163.com> Date: Thu, 27 Mar 2025 02:13:21 +0800 Subject: [PATCH 3/4] 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 ca04c83280412..b2c2015a79f66 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp @@ -163,7 +163,6 @@ 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()) { >From 8352c500ac7be59ab3daa700e751e912e71a9dd2 Mon Sep 17 00:00:00 2001 From: Congcong Cai <congcongcai0...@163.com> Date: Thu, 27 Mar 2025 14:08:31 +0000 Subject: [PATCH 4/4] fix with review --- .../clang-tidy/misc/ConstCorrectnessCheck.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp index 13eba246faf56..c7650ad43d808 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp @@ -81,16 +81,14 @@ void ConstCorrectnessCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { } void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { - const auto ConstType = hasType( - qualType(isConstQualified(), - // pointee check will check the const pointer and const array - unless(pointerType()), unless(arrayType()))); + const auto ConstType = + hasType(qualType(isConstQualified(), + // pointee check will check the constness of pointer + unless(pointerType()))); 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())), @@ -117,7 +115,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, ConstArrayType, TemplateType, + unless(anyOf(ConstType, ConstReference, TemplateType, hasInitializer(isInstantiationDependent()), AutoTemplateType, RValueReference, FunctionPointerRef, hasType(cxxRecordDecl(isLambda())), isImplicit(), _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits