Author: mitchell Date: 2026-02-21T15:37:30+08:00 New Revision: 7c6159660d97c634965bc387b1267519989b3800
URL: https://github.com/llvm/llvm-project/commit/7c6159660d97c634965bc387b1267519989b3800 DIFF: https://github.com/llvm/llvm-project/commit/7c6159660d97c634965bc387b1267519989b3800.diff LOG: [clang-tidy] Correctly handle array of pointers in misc-const-correctness (#179059) In arrays of pointers, `misc-const-correctness` check wrongly inspects whether the array element type was const-qualified, rather than the type it points to, leading to redundant `const` suggestions. This patch fixes the problem. Closes [#178880](https://github.com/llvm/llvm-project/issues/178880) Added: Modified: clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp index 6eb371a58ed1a..7e388201bf79a 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp @@ -316,10 +316,9 @@ void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) { CheckPointee(); } if (const auto *AT = dyn_cast<ArrayType>(VT)) { - if (!AT->getElementType().isConstQualified()) { - assert(AT->getElementType()->isPointerType()); + assert(AT->getElementType()->isPointerType()); + if (!AT->getElementType()->getPointeeType().isConstQualified()) CheckPointee(); - } } } return; diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 22992c5a1d5d8..77be492d4093e 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -205,6 +205,9 @@ Changes in existing checks - Added support for analyzing function parameters with the `AnalyzeParameters` option. + - Fixed false positive where an array of pointers to ``const`` was + incorrectly diagnosed as allowing the pointee to be made ``const``. + - Improved :doc:`modernize-pass-by-value <clang-tidy/checks/modernize/pass-by-value>` check by adding `IgnoreMacros` option to suppress warnings in macros. diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp index 0cb58c2e83643..9ecd804cf9235 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp @@ -88,3 +88,30 @@ void pass_address_to_void_pointer_to_pointer() { // CHECK-NOT: warning void_pointer_to_pointer_param(&ptr); } + +void already_const_pointee() { + const char* foo[] = {"a", "b"}; + // CHECK-NOT: warning + foo[0] = "c"; +} + +void array_of_const_pointers() { + const int i = 0; + const int* const bar[] = {&i}; + // CHECK-NOT: warning +} + +using ConstChar = const char; +void alias_to_const_pointer() { + ConstChar * foo[] = {"a", "b"}; + // CHECK-NOT: warning + foo[0] = "c"; +} + +void multi_level_pointer() { + const char * s = "a"; + const char ** foo[] = {&s}; + // CHECK-MESSAGES: [[@LINE-1]]:3: warning: pointee of variable 'foo' of type 'const char **[1]' can be declared 'const' + // CHECK-FIXES: const char * const* foo[] = {&s}; + const char * p = *foo[0]; +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
