diff --git a/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp --- a/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp +++ b/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 hasAnyNameStdString(std::vector Names) { diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -146,6 +146,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 + ` check. + + Added `std::basic_string_view` to default list of ``string``-like types. + Improvements to include-fixer ----------------------------- diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst b/clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst --- a/clang-tools-extra/docs/clang-tidy/checks/readability-redundant-string-init.rst +++ b/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 diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp +++ b/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 string; typedef basic_string wstring; + +template , typename A = std::allocator> +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 + basic_string_view(It, End); +}; +typedef basic_string_view string_view; +typedef basic_string_view 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 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("") {}