[clang-tools-extra] [clang-tidy] Correctly handle array of pointers in misc-const-correctness (PR #179059)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Feb 15 06:42:45 PST 2026
https://github.com/zeyi2 updated https://github.com/llvm/llvm-project/pull/179059
>From 430727b011c3ffeb953d66940dcfadbffc2a8833 Mon Sep 17 00:00:00 2001
From: mtx <mitchell.xu2 at gmail.com>
Date: Sun, 1 Feb 2026 02:01:02 +0800
Subject: [PATCH 1/2] [clang-tidy] Correctly handle array of pointers in
misc-const-correctness
---
.../clang-tidy/misc/ConstCorrectnessCheck.cpp | 5 ++--
clang-tools-extra/docs/ReleaseNotes.rst | 3 +++
.../const-correctness-pointer-as-pointers.cpp | 27 +++++++++++++++++++
3 files changed, 32 insertions(+), 3 deletions(-)
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 754880bd1a381..547a3b8f26afa 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -147,6 +147,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-use-std-format
<clang-tidy/checks/modernize/use-std-format>` check by fixing a crash
when an argument is part of a macro expansion.
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];
+}
>From d79ebd0db34149a5dd8d63132442ee5c809f9bec Mon Sep 17 00:00:00 2001
From: mtx <mitchell.xu2 at gmail.com>
Date: Sun, 1 Feb 2026 15:01:55 +0800
Subject: [PATCH 2/2] Address review feedback [1/N]
---
clang-tools-extra/docs/ReleaseNotes.rst | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 547a3b8f26afa..efe13e5885ebc 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -147,8 +147,8 @@ 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.
+ - Fixed false positive where an array of pointers to ``const`` was
+ incorrectly diagnosed as allowing the pointee to be made ``const``.
- Improved :doc:`modernize-use-std-format
<clang-tidy/checks/modernize/use-std-format>` check by fixing a crash
More information about the cfe-commits
mailing list