[PATCH] D117090: Fix `readability-non-const-parameter` for parameter referenced by an lvalue
gehry via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 12 00:11:32 PST 2022
Sockke created this revision.
Sockke added reviewers: danielmarjamaki, njames93, aaron.ballman, MTC.
Herald added a subscriber: carlosgalvezp.
Sockke requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.
The checker missed a check for a case when the parameter is referenced by an lvalue and this could cause build breakages.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D117090
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
@@ -287,3 +287,39 @@
}
char foo(char *s); // 2
// CHECK-FIXES: {{^}}char foo(const char *s); // 2{{$}}
+
+void lvalueReference(int *p) {
+ // CHECK-MESSAGES-NOT: warning:
+ int &x = *p;
+}
+
+// CHECK-MESSAGES: :[[@LINE+1]]:32: warning: pointer parameter 'p' can be
+void constLValueReference(int *p) {
+ // CHECK-FIXES: {{^}}void constLValueReference(const int *p) {{{$}}
+ const int &x = *p;
+}
+
+void lambdaLVRef(int *p) {
+ // CHECK-MESSAGES-NOT: warning:
+ auto foo = [&]() {
+ int &x = *p;
+ };
+}
+
+// CHECK-MESSAGES: :[[@LINE+1]]:28: warning: pointer parameter 'p' can be
+void lambdaConstLVRef(int *p) {
+ // CHECK-FIXES: {{^}}void lambdaConstLVRef(const int *p) {{{$}}
+ auto foo = [&]() {
+ const int &x = *p;
+ };
+}
+
+struct Temp1 {
+ Temp1(int &i) {
+ i = 10;
+ }
+};
+void constructLVRef(int *p) {
+ // CHECK-MESSAGES-NOT: warning:
+ Temp1 t(*p);
+}
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
@@ -83,6 +83,20 @@
for (const auto *Arg : CE->arguments()) {
markCanNotBeConst(Arg->IgnoreParenCasts(), true);
}
+ // Data passed by nonconst reference should not be made const.
+ unsigned ArgNr = 0U;
+ if (const auto *CD = CE->getConstructor()) {
+ for (const auto *Par : CD->parameters()) {
+ if (ArgNr >= CE->getNumArgs())
+ break;
+ const Expr *Arg = CE->getArg(ArgNr++);
+ // Is this a non constant reference parameter?
+ const Type *ParType = Par->getType().getTypePtr();
+ if (!ParType->isReferenceType() || Par->getType().isConstQualified())
+ continue;
+ markCanNotBeConst(Arg->IgnoreParenCasts(), false);
+ }
+ }
} else if (const auto *R = dyn_cast<ReturnStmt>(S)) {
markCanNotBeConst(R->getRetValue(), true);
} else if (const auto *U = dyn_cast<UnaryOperator>(S)) {
@@ -93,6 +107,9 @@
if ((T->isPointerType() && !T->getPointeeType().isConstQualified()) ||
T->isArrayType())
markCanNotBeConst(VD->getInit(), true);
+ else if (T->isLValueReferenceType() &&
+ !T->getPointeeType().isConstQualified())
+ markCanNotBeConst(VD->getInit(), false);
}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117090.399230.patch
Type: text/x-patch
Size: 2754 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220112/3e9debd8/attachment.bin>
More information about the cfe-commits
mailing list