diff --git a/clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp --- a/clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp @@ -83,24 +83,25 @@ // Check that ParamDecl of CallExprDecl has rvalue type. static bool checkParamDeclOfAncestorCallExprHasRValueRefType( const Expr *TheCxxConstructExpr, ASTContext &Context) { - if (const clang::CallExpr *TheCallExpr = - tryGetCallExprAncestorForCxxConstructExpr(TheCxxConstructExpr, - Context)) { - for (unsigned i = 0; i < TheCallExpr->getNumArgs(); ++i) { - const Expr *Arg = TheCallExpr->getArg(i); - if (Arg->getSourceRange() == TheCxxConstructExpr->getSourceRange()) { - if (const auto *TheCallExprFuncProto = - TheCallExpr->getCallee() - ->getType() - ->getPointeeType() - ->getAs()) { - if (TheCallExprFuncProto->getParamType(i)->isRValueReferenceType()) - return true; - } - } + const clang::CallExpr *TheCallExpr = + tryGetCallExprAncestorForCxxConstructExpr(TheCxxConstructExpr, Context); + if (!TheCallExpr) + return false; + for (unsigned I = 0; I < TheCallExpr->getNumArgs(); ++I) { + const Expr *Arg = TheCallExpr->getArg(I); + if (Arg->getSourceRange() != TheCxxConstructExpr->getSourceRange()) + continue; + QualType CalleePointerType = + TheCallExpr->getCallee()->getType()->getPointeeType(); + if (CalleePointerType.isNull()) + // Not a pointer type, should probably avoid this + continue; + if (const auto *TheCallExprFuncProto = + CalleePointerType->getAs()) { + if (TheCallExprFuncProto->getParamType(I)->isRValueReferenceType()) + return true; } } - return false; } diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-cstr.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-cstr.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-cstr.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-cstr.cpp @@ -220,3 +220,16 @@ m1tp m1p2 = m1; m1p2(s.c_str()); } + +namespace PR45286 { +struct Foo { + void func(const std::string &) {} +}; + +void bar() { + std::string Str{"aaa"}; + Foo Foo; + Foo.func(Str.c_str()); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant call to 'c_str' [readability-redundant-string-cstr] +} +} // namespace PR45286