[PATCH] D83864: [ClangTidy] Fix false positives of readability-non-const-parameters check

Jacques Lucke via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 15 05:12:16 PDT 2020


JacquesLucke updated this revision to Diff 278154.
JacquesLucke added a comment.

For some reason `arc` did not upload all the changes. Maybe because I cloned from github, will check. Anyway, here is the patch.


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

https://reviews.llvm.org/D83864

Files:
  clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
  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
@@ -217,11 +217,20 @@
 // Don't warn about nonconst record pointers that can be const.
 struct XY {
   int *x;
-  int *y;
+  const int *y;
 };
 void recordpointer(struct XY *xy) {
   *(xy->x) = 0;
 }
+// CHECK-MESSAGES: :[[@LINE+1]]:21: warning: pointer parameter 'y' can be
+void initlist1(int *y) {
+  // CHECK-FIXES: {{^}}void initlist1(const int *y) {{{$}}
+  XY xy = {nullptr, y};
+}
+// Don't warn when pointer is assigned to non-const struct member.
+void initlist2(int *x) {
+  XY xy = {x};
+}
 
 class C {
 public:
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
@@ -92,6 +92,19 @@
     }
   } else if (const auto *VD = Result.Nodes.getNodeAs<VarDecl>("Mark")) {
     const QualType T = VD->getType();
+    if (T->isRecordType()) {
+      if (const auto *ILE = dyn_cast_or_null<InitListExpr>(VD->getInit())) {
+        const auto *D = T->getAs<RecordType>()->getDecl();
+        unsigned InitNr = 0U;
+        for (const auto *F : D->fields()) {
+          if (InitNr >= ILE->getNumInits())
+            break;
+          const auto *Init = ILE->getInit(InitNr++);
+          if (!F->getType().isConstQualified())
+            markCanNotBeConst(Init, true);
+        }
+      }
+    }
     if ((T->isPointerType() && !T->getPointeeType().isConstQualified()) ||
         T->isArrayType())
       markCanNotBeConst(VD->getInit(), true);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D83864.278154.patch
Type: text/x-patch
Size: 1903 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200715/92ca523d/attachment.bin>


More information about the cfe-commits mailing list