[clang-tidy] Include std::basic_string_view in readability-redundant-string-init.
authorChris Kennelly <ckennelly@ckennelly.com>
Sat, 7 Nov 2020 19:14:08 +0000 (14:14 -0500)
committerChris Kennelly <ckennelly@ckennelly.com>
Fri, 20 Nov 2020 15:06:57 +0000 (10:06 -0500)
std::string_view("") produces a string_view instance that compares
equal to std::string_view(), but requires more complex initialization
(storing the address of the string literal, rather than zeroing).

Reviewed By: aaron.ballman

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

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 e5825bc..24defc8 100644 (file)
@@ -18,7 +18,8 @@ namespace clang {
 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) {
index cc9de10..5e78de2 100644 (file)
@@ -152,6 +152,11 @@ Changes in existing checks
 - 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 c455688..dc3dfac 100644 (file)
@@ -19,12 +19,21 @@ Examples
   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 c33d1a7..ed3d90c 100644 (file)
@@ -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 @@ struct basic_string {
 };
 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 @@ void f() {
   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 @@ void g() {
   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 @@ public:
   // 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("") {}