[PATCH] D145730: [clang-tidy] Fix readability-redundant-string-cstr for smart pointer #576705
Piotr Zegar via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 10 05:53:45 PST 2023
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();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145730.504116.patch
Type: text/x-patch
Size: 2475 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230310/203be5b7/attachment.bin>
More information about the cfe-commits
mailing list