[clang-tools-extra] r285901 - [clang-tidy] Handle data() in readability-redundant-string-cstr

Malcolm Parsons via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 3 05:56:49 PDT 2016


Author: malcolm.parsons
Date: Thu Nov  3 07:56:48 2016
New Revision: 285901

URL: http://llvm.org/viewvc/llvm-project?rev=285901&view=rev
Log:
[clang-tidy] Handle data() in readability-redundant-string-cstr

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

Reviewers: etienneb, alexfh, aaron.ballman

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D26279

Modified:
    clang-tools-extra/trunk/clang-tidy/readability/RedundantStringCStrCheck.cpp
    clang-tools-extra/trunk/docs/ReleaseNotes.rst
    clang-tools-extra/trunk/docs/clang-tidy/checks/readability-redundant-string-cstr.rst
    clang-tools-extra/trunk/test/clang-tidy/readability-redundant-string-cstr-msvc.cpp
    clang-tools-extra/trunk/test/clang-tidy/readability-redundant-string-cstr.cpp

Modified: clang-tools-extra/trunk/clang-tidy/readability/RedundantStringCStrCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/RedundantStringCStrCheck.cpp?rev=285901&r1=285900&r2=285901&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/RedundantStringCStrCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/RedundantStringCStrCheck.cpp Thu Nov  3 07:56:48 2016
@@ -99,7 +99,7 @@ void RedundantStringCStrCheck::registerM
   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,7 +192,8 @@ void RedundantStringCStrCheck::registerM
 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 =
@@ -200,7 +201,8 @@ void RedundantStringCStrCheck::check(con
   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);
 }
 

Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=285901&r1=285900&r2=285901&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Thu Nov  3 07:56:48 2016
@@ -134,6 +134,10 @@ Improvements to clang-tidy
   Flags member initializations that are unnecessary because the same default
   constructor would be called if they were not present.
 
+- The `readability-redundant-string-cstr
+  <http://clang.llvm.org/extra/clang-tidy/checks/readability-redundant-string-cstr.html>`_ check
+  now warns about redundant calls to data() too.
+
 Fixed bugs:
 
 - `modernize-make-unique

Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/readability-redundant-string-cstr.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/readability-redundant-string-cstr.rst?rev=285901&r1=285900&r2=285901&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/readability-redundant-string-cstr.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/readability-redundant-string-cstr.rst Thu Nov  3 07:56:48 2016
@@ -4,4 +4,4 @@ readability-redundant-string-cstr
 =================================
 
 
-Finds unnecessary calls to ``std::string::c_str()``.
+Finds unnecessary calls to ``std::string::c_str()`` and ``std::string::data()``.

Modified: clang-tools-extra/trunk/test/clang-tidy/readability-redundant-string-cstr-msvc.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-redundant-string-cstr-msvc.cpp?rev=285901&r1=285900&r2=285901&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/readability-redundant-string-cstr-msvc.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/readability-redundant-string-cstr-msvc.cpp Thu Nov  3 07:56:48 2016
@@ -12,6 +12,7 @@ struct basic_string {
   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 @@ struct StringRef {
 
 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) {

Modified: clang-tools-extra/trunk/test/clang-tidy/readability-redundant-string-cstr.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-redundant-string-cstr.cpp?rev=285901&r1=285900&r2=285901&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/readability-redundant-string-cstr.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/readability-redundant-string-cstr.cpp Thu Nov  3 07:56:48 2016
@@ -16,6 +16,7 @@ struct basic_string {
   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 @@ struct StringRef {
 
 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 f3(const llvm::StringRef &r) {
 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,7 +172,7 @@ void f6(const std::string &s) {
 
 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);{{$}}
 }
 
@@ -176,7 +180,7 @@ void g1(const std::wstring &s) {
 
 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);{{$}}
 }
 
@@ -184,7 +188,7 @@ void h1(const std::u16string &s) {
 
 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);{{$}}
 }
 




More information about the cfe-commits mailing list