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(); + + // Remove remaining '->' from overloaded operator call + 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,12 @@ // RUN: %check_clang_tidy %s readability-redundant-string-cstr %t -- -- -isystem %clang_tidy_headers #include +template +struct iterator { + T *operator->(); + T &operator*(); +}; + namespace llvm { struct StringRef { StringRef(const char *p); @@ -202,6 +208,31 @@ m1p2(s.c_str()); } +// Test for overloaded operator-> +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;{{$}} + + // An unlikely situation and the outcome is not ideal, but at least the fix doesn't generate broken code. + tmp = i.operator->()->c_str(); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: redundant call to 'c_str' [readability-redundant-string-cstr] + // CHECK-FIXES: {{^ }}tmp = *i.operator->();{{$}} + + // The fix contains an unnecessary set of parentheses, but these have no effect. + iterator *pi = &i; + tmp = (*pi)->c_str(); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: redundant call to 'c_str' [readability-redundant-string-cstr] + // CHECK-FIXES: {{^ }}tmp = *(*pi);{{$}} + + // An unlikely situation, but at least the fix doesn't generate broken code. + tmp = pi->operator->()->c_str(); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: redundant call to 'c_str' [readability-redundant-string-cstr] + // CHECK-FIXES: {{^ }}tmp = *pi->operator->();{{$}} +} + namespace PR45286 { struct Foo { void func(const std::string &) {}