llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tidy

Author: mitchell (zeyi2)

<details>
<summary>Changes</summary>

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 #<!-- -->177354

---
Full diff: https://github.com/llvm/llvm-project/pull/179059.diff


3 Files Affected:

- (modified) clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp (+2-3) 
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+3) 
- (modified) 
clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp
 (+27) 


``````````diff
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];
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/179059
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to