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

Congcong Cai via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 21 20:43:51 PDT 2023


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG1c9412441b87: [clang-tidy]mark record initList as non-const param (authored by HerrCai0907).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158152/new/

https://reviews.llvm.org/D158152

Files:
  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


Index: clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.cpp
@@ -222,6 +222,24 @@
 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:
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -241,6 +241,10 @@
   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.
Index: clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
@@ -103,7 +103,7 @@
   } 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())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158152.552201.patch
Type: text/x-patch
Size: 2452 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230822/1995c64e/attachment-0001.bin>


More information about the cfe-commits mailing list