malcolm.parsons created this revision.
malcolm.parsons added reviewers: etienneb, alexfh, aaron.ballman.
malcolm.parsons added a subscriber: cfe-commits.

std::string::data() and std::string::c_str() are equivalent.
Enhance the readability-redundant-string-cstr check to also handle
calls to data().


https://reviews.llvm.org/D26279

Files:
  clang-tidy/readability/RedundantStringCStrCheck.cpp
  test/clang-tidy/readability-redundant-string-cstr-msvc.cpp
  test/clang-tidy/readability-redundant-string-cstr.cpp

Index: test/clang-tidy/readability-redundant-string-cstr.cpp
===================================================================
--- test/clang-tidy/readability-redundant-string-cstr.cpp
+++ test/clang-tidy/readability-redundant-string-cstr.cpp
@@ -16,6 +16,7 @@
   basic_string(const C *p, const A &a = A());
 
   const C *c_str() const;
+  const C *data() const;
 
   _Type& append(const C *s);
   _Type& append(const C *s, size n);
@@ -66,7 +67,10 @@
 
 void f1(const std::string &s) {
   f1(s.c_str());
-  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to `c_str()` [readability-redundant-string-cstr]
+  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
+  // CHECK-FIXES: {{^  }}f1(s);{{$}}
+  f1(s.data());
+  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'data' [readability-redundant-string-cstr]
   // CHECK-FIXES: {{^  }}f1(s);{{$}}
 }
 void f2(const llvm::StringRef r) {
@@ -86,7 +90,7 @@
 void f4(const std::string &s) {
   const std::string* ptr = &s;
   f1(ptr->c_str());
-  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to `c_str()` [readability-redundant-string-cstr]
+  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
   // CHECK-FIXES: {{^  }}f1(*ptr);{{$}}
 }
 void f5(const std::string &s) {
@@ -168,23 +172,23 @@
 
 void g1(const std::wstring &s) {
   g1(s.c_str());
-  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to `c_str()` [readability-redundant-string-cstr]
+  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
   // CHECK-FIXES: {{^  }}g1(s);{{$}}
 }
 
 // Tests for std::u16string.
 
 void h1(const std::u16string &s) {
   h1(s.c_str());
-  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to `c_str()` [readability-redundant-string-cstr]
+  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
   // CHECK-FIXES: {{^  }}h1(s);{{$}}
 }
 
 // Tests for std::u32string.
 
 void k1(const std::u32string &s) {
   k1(s.c_str());
-  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to `c_str()` [readability-redundant-string-cstr]
+  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
   // CHECK-FIXES: {{^  }}k1(s);{{$}}
 }
 
Index: test/clang-tidy/readability-redundant-string-cstr-msvc.cpp
===================================================================
--- test/clang-tidy/readability-redundant-string-cstr-msvc.cpp
+++ test/clang-tidy/readability-redundant-string-cstr-msvc.cpp
@@ -12,6 +12,7 @@
   basic_string(const C *p);
   basic_string(const C *p, const A &a);
   const C *c_str() const;
+  const C *data() const;
 };
 typedef basic_string<char, std::char_traits<char>, std::allocator<char>> string;
 }
@@ -24,7 +25,10 @@
 
 void f1(const std::string &s) {
   f1(s.c_str());
-  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to `c_str()` [readability-redundant-string-cstr]
+  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
+  // CHECK-FIXES: {{^  }}f1(s);{{$}}
+  f1(s.data());
+  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'data' [readability-redundant-string-cstr]
   // CHECK-FIXES: {{^  }}f1(s);{{$}}
 }
 void f2(const llvm::StringRef r) {
Index: clang-tidy/readability/RedundantStringCStrCheck.cpp
===================================================================
--- clang-tidy/readability/RedundantStringCStrCheck.cpp
+++ clang-tidy/readability/RedundantStringCStrCheck.cpp
@@ -99,7 +99,7 @@
   const auto StringCStrCallExpr =
       cxxMemberCallExpr(on(StringExpr.bind("arg")),
                         callee(memberExpr().bind("member")),
-                        callee(cxxMethodDecl(hasName("c_str"))))
+                        callee(cxxMethodDecl(hasAnyName("c_str", "data"))))
           .bind("call");
 
   // Detect redundant 'c_str()' calls through a string constructor.
@@ -192,15 +192,17 @@
 void RedundantStringCStrCheck::check(const MatchFinder::MatchResult &Result) {
   const auto *Call = Result.Nodes.getStmtAs<CallExpr>("call");
   const auto *Arg = Result.Nodes.getStmtAs<Expr>("arg");
-  bool Arrow = Result.Nodes.getStmtAs<MemberExpr>("member")->isArrow();
+  const auto *Member = Result.Nodes.getStmtAs<MemberExpr>("member");
+  bool Arrow = Member->isArrow();
   // Replace the "call" node with the "arg" node, prefixed with '*'
   // if the call was using '->' rather than '.'.
   std::string ArgText =
       Arrow ? formatDereference(Result, *Arg) : getText(Result, *Arg).str();
   if (ArgText.empty())
     return;
 
-  diag(Call->getLocStart(), "redundant call to `c_str()`")
+  diag(Call->getLocStart(), "redundant call to %0")
+      << Member->getMemberDecl()
       << FixItHint::CreateReplacement(Call->getSourceRange(), ArgText);
 }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to