HerrCai0907 created this revision. Herald added subscribers: PiotrZSL, carlosgalvezp, xazax.hun. Herald added a reviewer: njames93. Herald added a project: All. HerrCai0907 requested review of this revision. Herald added subscribers: cfe-commits, wangpc. Herald added a project: clang-tools-extra.
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* Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D158152 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 @@ -222,6 +222,18 @@ 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}; +} 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 @@ -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())
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,18 @@ 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}; +} 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 @@ -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())
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits