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)) {
if ((T->isPointerType() && !T->getPointeeType().isConstQualified()) ||
T->isArrayType())
markCanNotBeConst(VD->getInit(), true);
+ else if (T->isLValueReferenceType() &&
+ !T->getPointeeType().isConstQualified())
+ markCanNotBeConst(VD->getInit(), false);
}
}
Changes in existing checks
^^^^^^^^^^^^^^^^^^^^^^^^^^
+- Fixed a false positive in :doc:`readability-non-const-parameter
+ <clang-tidy/checks/readability-non-const-parameter>` when the parameter is referenced by an lvalue
+
+
Removed checks
^^^^^^^^^^^^^^
}
char foo(char *s); // 2
// CHECK-FIXES: {{^}}char foo(const char *s); // 2{{$}}
+
+void lvalueReference(int *p) {
+ // CHECK-MESSAGES-NOT: warning: pointer parameter 'p' can be
+ 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: pointer parameter 'p' can be
+ 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: pointer parameter 'p' can be
+ Temp1 t(*p);
+}