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 @@ -52,6 +52,10 @@ if (Text.empty()) return std::string(); + + // https://github.com/llvm/llvm-project/issues/56705 + Text.consume_back("->"); + // Add leading '*'. if (needParensAfterUnaryOperator(ExprNode)) { return (llvm::Twine("*(") + Text + ")").str(); 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 @@ -1,6 +1,11 @@ // RUN: %check_clang_tidy %s readability-redundant-string-cstr %t -- -- -isystem %clang_tidy_headers #include +template +struct iterator { + T *operator->(); +}; + namespace llvm { struct StringRef { StringRef(const char *p); @@ -202,6 +207,15 @@ m1p2(s.c_str()); } +// Test for iterator +void it(iterator i) +{ + std::string tmp; + tmp = i->c_str(); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: redundant call to 'c_str' [readability-redundant-string-cstr] + // CHECK-FIXES: {{^ }}tmp = *i;{{$}} +} + namespace PR45286 { struct Foo { void func(const std::string &) {}