This revision was automatically updated to reflect the committed changes. Closed by commit rG9c8bc090a0c2: [clang-tidy] Fix readability-redundant-string-cstr for smart pointer #576705 (authored by mikecrowe, committed by PiotrZSL).
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D145730/new/ https://reviews.llvm.org/D145730 Files: clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp clang-tools-extra/test/clang-tidy/checkers/readability/redundant-string-cstr.cpp Index: clang-tools-extra/test/clang-tidy/checkers/readability/redundant-string-cstr.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/readability/redundant-string-cstr.cpp +++ 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 <string> +template <typename T> +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<std::string> 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<std::string> *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 &) {} Index: clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp +++ 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();
Index: clang-tools-extra/test/clang-tidy/checkers/readability/redundant-string-cstr.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/readability/redundant-string-cstr.cpp +++ 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 <string> +template <typename T> +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<std::string> 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<std::string> *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 &) {} Index: clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp +++ 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();
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits