[PATCH] D130641: [clang-tidy] Extend redundant-string-c-str to also flag constructing a string_view.

Nathan James via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 27 09:02:53 PDT 2022


njames93 created this revision.
njames93 added reviewers: aaron.ballman, alexfh, LegalizeAdulthood.
Herald added subscribers: carlosgalvezp, xazax.hun.
Herald added a project: All.
njames93 requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D130641

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
@@ -48,6 +48,16 @@
 typedef basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>> wstring;
 typedef basic_string<char16, std::char_traits<char16>, std::allocator<char16>> u16string;
 typedef basic_string<char32, std::char_traits<char32>, std::allocator<char32>> u32string;
+
+template<typename C, typename T>
+struct basic_string_view {
+  basic_string_view(const C*);
+};
+
+typedef basic_string_view<char, std::char_traits<char>> string_view;
+typedef basic_string_view<wchar_t, std::char_traits<wchar_t>> wstring_voew;
+typedef basic_string_view<char16, std::char_traits<char16>> u16string_view;
+typedef basic_string_view<char32, std::char_traits<char32>> u32string_view;
 }
 
 std::string operator+(const std::string&, const std::string&);
@@ -170,6 +180,14 @@
   tmp.insert(1, s.c_str(), 2);
 }
 
+void f7(const std::string_view&) {
+  std::string S;
+  f7(S.c_str());
+  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call {{.*}}
+  // CHECK-FIXES: {{^  }}std::string S;{{$}}
+  // CHECK-FIXES-NEXT: {{^  }}f7(S);{{$}}
+}
+
 // Tests for std::wstring.
 
 void g1(const std::wstring &s) {
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
@@ -11,6 +11,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "RedundantStringCStrCheck.h"
+#include "clang/AST/DeclCXX.h"
+#include "clang/AST/ExprCXX.h"
 #include "clang/Lex/Lexer.h"
 #include "clang/Tooling/FixIt.h"
 
@@ -65,6 +67,12 @@
   return Node.isBoundToLvalueReference();
 }
 
+AST_MATCHER_P(CXXConstructExpr, forClass,
+              ast_matchers::internal::Matcher<CXXRecordDecl>, InnerMatcher) {
+  const CXXRecordDecl *Parent = Node.getConstructor()->getParent();
+  return Parent && InnerMatcher.matches(*Parent, Finder, Builder);
+}
+
 } // end namespace
 
 void RedundantStringCStrCheck::registerMatchers(
@@ -157,22 +165,21 @@
 
   // Detect redundant 'c_str()' calls through a StringRef constructor.
   Finder->addMatcher(
-      traverse(
-          TK_AsIs,
-          cxxConstructExpr(
-              // Implicit constructors of these classes are overloaded
-              // wrt. string types and they internally make a StringRef
-              // referring to the argument.  Passing a string directly to
-              // them is preferred to passing a char pointer.
-              hasDeclaration(cxxMethodDecl(hasAnyName(
-                  "::llvm::StringRef::StringRef", "::llvm::Twine::Twine"))),
-              argumentCountIs(1),
-              // The only argument must have the form x.c_str() or p->c_str()
-              // where the method is string::c_str().  StringRef also has
-              // a constructor from string which is more efficient (avoids
-              // strlen), so we can construct StringRef from the string
-              // directly.
-              hasArgument(0, StringCStrCallExpr))),
+      traverse(TK_AsIs,
+               cxxConstructExpr(
+                   // Implicit constructors of these classes are overloaded
+                   // wrt. string types and they internally make a StringRef
+                   // referring to the argument.  Passing a string directly to
+                   // them is preferred to passing a char pointer.
+                   forClass(hasAnyName("::std::basic_string_view",
+                                       "::llvm::StringRef", "::llvm::Twine")),
+                   argumentCountIs(1),
+                   // The only argument must have the form x.c_str() or
+                   // p->c_str() where the method is string::c_str().  StringRef
+                   // also has a constructor from string which is more efficient
+                   // (avoids strlen), so we can construct StringRef from the
+                   // string directly.
+                   hasArgument(0, StringCStrCallExpr))),
       this);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130641.448063.patch
Type: text/x-patch
Size: 4419 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220727/8b0d0ae2/attachment-0001.bin>


More information about the cfe-commits mailing list