Skip to content

Commit c2defc6

Browse files
committed
[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.
1 parent e04d739 commit c2defc6

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) {
8989
const auto ConstReference = hasType(references(isConstQualified()));
9090
const auto RValueReference = hasType(
9191
referenceType(anyOf(rValueReferenceType(), unless(isSpelledAsLValue()))));
92+
const auto ConstArrayType =
93+
hasType(arrayType(hasElementType(isConstQualified())));
9294

9395
const auto TemplateType = anyOf(
9496
hasType(hasCanonicalType(templateTypeParmType())),
@@ -115,7 +117,7 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) {
115117
// Example: `int i = 10` would match `int i`.
116118
const auto LocalValDecl = varDecl(
117119
isLocal(), hasInitializer(anything()),
118-
unless(anyOf(ConstType, ConstReference, TemplateType,
120+
unless(anyOf(ConstType, ConstReference, ConstArrayType, TemplateType,
119121
hasInitializer(isInstantiationDependent()), AutoTemplateType,
120122
RValueReference, FunctionPointerRef,
121123
hasType(cxxRecordDecl(isLambda())), isImplicit(),
@@ -161,6 +163,7 @@ void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) {
161163

162164
VariableCategory VC = VariableCategory::Value;
163165
const QualType VT = Variable->getType();
166+
VT->dump();
164167
if (VT->isReferenceType()) {
165168
VC = VariableCategory::Reference;
166169
} else if (VT->isPointerType()) {
@@ -169,6 +172,7 @@ void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) {
169172
if (ArrayT->getElementType()->isPointerType())
170173
VC = VariableCategory::Pointer;
171174
}
175+
llvm::errs() << (int)VC << "\n";
172176

173177
auto CheckValue = [&]() {
174178
// The scope is only registered if the analysis shall be run.

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ Changes in existing checks
146146
`AllowedTypes`, that excludes specified types from const-correctness
147147
checking and fixing false positives when modifying variant by ``operator[]``
148148
with template in parameters and supporting to check pointee mutation by
149-
`AnalyzePointers` option.
149+
`AnalyzePointers` option and fixing false positives when using const array
150+
type.
150151

151152
- Improved :doc:`misc-redundant-expression
152153
<clang-tidy/checks/misc/redundant-expression>` check by providing additional

clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-values.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,3 +1007,11 @@ template <typename T> void f() {
10071007
x[T{}] = 3;
10081008
}
10091009
} // namespace gh127776_false_positive
1010+
1011+
namespace gh132931_false_positive {
1012+
using T = const int;
1013+
void valid(int i) {
1014+
const int arr0[] = {1, 2, 3};
1015+
T arr1[] = {1, 2, 3};
1016+
}
1017+
} // namespace gh132931_false_positive

0 commit comments

Comments
 (0)