This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGe4f9b5d442a2: [clang-tidy] Include std::basic_string_view in 
readability-redundant-string… (authored by ckennelly).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D91009

Files:
  clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst
  
clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp
@@ -1,7 +1,7 @@
 // RUN: %check_clang_tidy -std=c++11,c++14 %s readability-redundant-string-init %t \
 // RUN:   -config="{CheckOptions: \
 // RUN:             [{key: readability-redundant-string-init.StringNames, \
-// RUN:               value: '::std::basic_string;our::TestString'}] \
+// RUN:               value: '::std::basic_string;::std::basic_string_view;our::TestString'}] \
 // RUN:             }"
 // FIXME: Fix the checker to work in C++17 mode.
 
@@ -19,6 +19,20 @@
 };
 typedef basic_string<char> string;
 typedef basic_string<wchar_t> wstring;
+
+template <typename C, typename T = std::char_traits<C>, typename A = std::allocator<C>>
+struct basic_string_view {
+  using size_type = decltype(sizeof(0));
+
+  basic_string_view();
+  basic_string_view(const basic_string_view &);
+  basic_string_view(const C *, size_type);
+  basic_string_view(const C *);
+  template <class It, class End>
+  basic_string_view(It, End);
+};
+typedef basic_string_view<char> string_view;
+typedef basic_string_view<wchar_t> wstring_view;
 }
 
 void f() {
@@ -48,6 +62,33 @@
   std::string z;
 }
 
+void fview() {
+  std::string_view a = "";
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization [readability-redundant-string-init]
+  // CHECK-FIXES: std::string_view a;
+  std::string_view b("");
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view b;
+  std::string_view c = R"()";
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view c;
+  std::string_view d(R"()");
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view d;
+  std::string_view e{""};
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view e;
+  std::string_view f = {""};
+  // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
+  // CHECK-FIXES: std::string_view f;
+
+  std::string_view u = "u";
+  std::string_view w("w");
+  std::string_view x = R"(x)";
+  std::string_view y(R"(y)");
+  std::string_view z;
+}
+
 void g() {
   std::wstring a = L"";
   // CHECK-MESSAGES: [[@LINE-1]]:16: warning: redundant string initialization
@@ -69,6 +110,33 @@
   std::wstring z;
 }
 
+void gview() {
+  std::wstring_view a = L"";
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization [readability-redundant-string-init]
+  // CHECK-FIXES: std::wstring_view a;
+  std::wstring_view b(L"");
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
+  // CHECK-FIXES: std::wstring_view b;
+  std::wstring_view c = L"";
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
+  // CHECK-FIXES: std::wstring_view c;
+  std::wstring_view d(L"");
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
+  // CHECK-FIXES: std::wstring_view d;
+  std::wstring_view e{L""};
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
+  // CHECK-FIXES: std::wstring_view e;
+  std::wstring_view f = {L""};
+  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
+  // CHECK-FIXES: std::wstring_view f;
+
+  std::wstring_view u = L"u";
+  std::wstring_view w(L"w");
+  std::wstring_view x = LR"(x)";
+  std::wstring_view y(LR"(y)");
+  std::wstring_view z;
+}
+
 template <typename T>
 void templ() {
   std::string s = "";
@@ -274,7 +342,6 @@
   // CHECK-MESSAGES: [[@LINE-2]]:23: warning: redundant string initialization
   // CHECK-FIXES:  Foo(float)  {}
 
-
   // Check how it handles removing some redundant initializers while leaving
   // valid initializers intact.
   Foo(std::string Arg) : A(Arg), B(""), C("NonEmpty"), D(R"()"), E("") {}
Index: clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst
===================================================================
--- clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst
+++ clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst
@@ -19,12 +19,21 @@
   std::string a;
   std::string b;
 
+  // Initializing a string_view with an empty string literal produces an
+  // instance that compares equal to string_view().
+  std::string_view a = "";
+  std::string_view b("");
+
+  // becomes
+  std::string_view a;
+  std::string_view b;
+
 Options
 -------
 
 .. option:: StringNames
 
-    Default is `::std::basic_string`.
+    Default is `::std::basic_string;::std::basic_string_view`.
 
     Semicolon-delimited list of class names to apply this check to.
     By default `::std::basic_string` applies to ``std::string`` and
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -152,6 +152,11 @@
 - Removed `google-runtime-references` check because the rule it checks does
   not exist in the Google Style Guide anymore.
 
+- Improved :doc:`readability-redundant-string-init
+  <clang-tidy/checks/readability-redundant-string-init>` check.
+
+  Added `std::basic_string_view` to default list of ``string``-like types.
+
 Improvements to include-fixer
 -----------------------------
 
Index: clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
@@ -18,7 +18,8 @@
 namespace tidy {
 namespace readability {
 
-const char DefaultStringNames[] = "::std::basic_string";
+const char DefaultStringNames[] =
+    "::std::basic_string_view;::std::basic_string";
 
 static ast_matchers::internal::Matcher<NamedDecl>
 hasAnyNameStdString(std::vector<std::string> Names) {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to