[PATCH] D140018: [clang-tidy] Support std::string_view in readability-redundant-string-cstr

Tamas Berghammer via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 14 08:36:47 PST 2022


tberghammer updated this revision to Diff 482871.
tberghammer added a comment.

Fix formatting type in release notes


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D140018/new/

https://reviews.llvm.org/D140018

Files:
  clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  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,12 @@
 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* s);
+};
+typedef basic_string_view<char, std::char_traits<char>> string_view;
 }
 
 std::string operator+(const std::string&, const std::string&);
@@ -169,6 +175,15 @@
   tmp.insert(1, s);
   tmp.insert(1, s.c_str(), 2);
 }
+void f7(std::string_view sv) {
+  std::string s;
+  f7(s.c_str());
+  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
+  // CHECK-FIXES: {{^  }}f7(s);{{$}}
+  f7(s.data());
+  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'data' [readability-redundant-string-cstr]
+  // CHECK-FIXES: {{^  }}f7(s);{{$}}
+}
 
 // Tests for std::wstring.
 
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -201,6 +201,10 @@
   The check now skips concept definitions since redundant expressions still make sense
   inside them.
 
+- Support removing ``c_str`` calls from ``std::string_view`` constructor calls in
+  :doc: `readability-redundant-string-cstr <clang-tidy/checks/readability/redundant-string-cstr>`
+  check.
+
 Removed checks
 ^^^^^^^^^^^^^^
 
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
@@ -86,6 +86,11 @@
           // be present explicitly.
           hasArgument(1, cxxDefaultArgExpr()))));
 
+  // Match string constructor.
+  const auto StringViewConstructorExpr = cxxConstructExpr(
+      argumentCountIs(1),
+      hasDeclaration(cxxMethodDecl(hasName("basic_string_view"))));
+
   // Match a call to the string 'c_str()' method.
   const auto StringCStrCallExpr =
       cxxMemberCallExpr(on(StringExpr.bind("arg")),
@@ -101,7 +106,8 @@
       traverse(
           TK_AsIs,
           cxxConstructExpr(
-              StringConstructorExpr, hasArgument(0, StringCStrCallExpr),
+              anyOf(StringConstructorExpr, StringViewConstructorExpr),
+              hasArgument(0, StringCStrCallExpr),
               unless(anyOf(HasRValueTempParent, hasParent(cxxBindTemporaryExpr(
                                                     HasRValueTempParent)))))),
       this);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140018.482871.patch
Type: text/x-patch
Size: 3098 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221214/580f3627/attachment.bin>


More information about the cfe-commits mailing list