Author: Congcong Cai
Date: 2023-08-22T11:43:27+08:00
New Revision: 1c9412441b87eb15262fb46dddd53015ff30c42d

URL: 
https://github.com/llvm/llvm-project/commit/1c9412441b87eb15262fb46dddd53015ff30c42d
DIFF: 
https://github.com/llvm/llvm-project/commit/1c9412441b87eb15262fb46dddd53015ff30c42d.diff

LOG: [clang-tidy]mark record initList as non-const param

```
struct XY {
  int *x;
  int *y;
};
void recordInitList(int *x) {
  XY xy = {x, nullptr};
}
```
x cannot be const int* becase it in a initialize list which only accept int*

Reviewed By: PiotrZSL

Differential Revision: https://reviews.llvm.org/D158152

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    
clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp

Removed: 
    


################################################################################
diff  --git 
a/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
index 974909f3ab35a4..95a3a5165e2e82 100644
--- a/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
@@ -103,7 +103,7 @@ void NonConstParameterCheck::check(const 
MatchFinder::MatchResult &Result) {
   } else if (const auto *VD = Result.Nodes.getNodeAs<VarDecl>("Mark")) {
     const QualType T = VD->getType();
     if ((T->isPointerType() && !T->getPointeeType().isConstQualified()) ||
-        T->isArrayType())
+        T->isArrayType() || T->isRecordType())
       markCanNotBeConst(VD->getInit(), true);
     else if (T->isLValueReferenceType() &&
              !T->getPointeeType().isConstQualified())

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 470f06d82bf1d5..28a386e529c84f 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -241,6 +241,10 @@ Changes in existing checks
   do-while loops into account for the `AllowIntegerConditions` and
   `AllowPointerConditions` options.
 
+- Improved :doc:`readability-non-const-parameter
+  <clang-tidy/checks/readability/non-const-parameter>` check to ignore
+  false-positives in initializer list of record.
+
 - Improved :doc:`readability-static-accessed-through-instance
   <clang-tidy/checks/readability/static-accessed-through-instance>` check to
   identify calls to static member functions with out-of-class inline 
definitions.

diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp
index 373d9ea6bb0a36..1f89e0b8d5f2dd 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp
@@ -222,6 +222,24 @@ struct XY {
 void recordpointer(struct XY *xy) {
   *(xy->x) = 0;
 }
+void recordInitList(int *x) {
+  XY xy = {x, nullptr};
+}
+
+struct XYConst {
+  int const *x;
+};
+// CHECK-MESSAGES: :[[@LINE+1]]:30: warning: pointer parameter 'x' can be 
pointer to const
+void recordInitListDiag(int *x) {
+  // CHECK-FIXES: {{^}}void recordInitListDiag(const int *x) {{{$}}
+  XYConst xy = {x};
+}
+typedef XYConst XYConstAlias;
+// CHECK-MESSAGES: :[[@LINE+1]]:35: warning: pointer parameter 'x' can be 
pointer to const
+void recordInitListAliasDiag(int *x) {
+  // CHECK-FIXES: {{^}}void recordInitListAliasDiag(const int *x) {{{$}}
+  XYConstAlias xy = {x};
+}
 
 class C {
 public:


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to