Author: Chris Kennelly Date: 2020-11-20T10:06:57-05:00 New Revision: e4f9b5d442a260dd78b3de581cec1e90567a2aac
URL: https://github.com/llvm/llvm-project/commit/e4f9b5d442a260dd78b3de581cec1e90567a2aac DIFF: https://github.com/llvm/llvm-project/commit/e4f9b5d442a260dd78b3de581cec1e90567a2aac.diff LOG: [clang-tidy] Include std::basic_string_view in readability-redundant-string-init. 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 Added: Modified: 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 Removed: ################################################################################ diff --git a/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp index e5825bc4f0e3..24defc80f161 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp @@ -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) { diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index cc9de109900b..5e78de2b0edc 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -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 ----------------------------- 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 index c4556887f89a..dc3dfacb15d5 100644 --- 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 @@ 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 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 index c33d1a7d5f2a..ed3d90ca307e 100644 --- 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 @@ 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 @@ class Foo { // 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("") {} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits