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/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 = unsigned; + + basic_string_view(); + basic_string_view(const basic_string_view &); + basic_string_view(const C *, size_type); + basic_string_view(const C *, const A &a = A()); + 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,27 @@ std::wstring z; } +void gview() { + std::wstring_view a = L""; + // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization + // 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 = LR"()"; + // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization + // CHECK-FIXES: std::wstring_view c; + std::wstring_view d(LR"()"); + // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization + // CHECK-FIXES: std::wstring_view d; + + 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 = ""; @@ -121,6 +183,35 @@ DECL_STRING(h, "u"); } +typedef std::string_view MyStringView; +#define STRINGVIEW MyStringView +#define DECL_STRINGVIEW(name, val) STRINGVIEW name = val + +void iview() { + MyStringView a = ""; + // CHECK-MESSAGES: [[@LINE-1]]:16: warning: redundant string initialization + // CHECK-FIXES: MyStringView a; + STRINGVIEW b = ""; + // CHECK-MESSAGES: [[@LINE-1]]:14: warning: redundant string initialization + // CHECK-FIXES: STRINGVIEW b; + MyStringView c = "" + "" + ""; + // CHECK-MESSAGES: [[@LINE-3]]:16: warning: redundant string initialization + // CHECK-FIXES: MyStringView c; + STRINGVIEW d = "" + "" + ""; + // CHECK-MESSAGES: [[@LINE-3]]:14: warning: redundant string initialization + // CHECK-FIXES: STRINGVIEW d; + DECL_STRINGVIEW(e, ""); + // CHECK-MESSAGES: [[@LINE-1]]:19: warning: redundant string initialization + + MyStringView f = "u"; + STRINGVIEW g = "u"; + DECL_STRINGVIEW(h, "u"); +} + #define EMPTY_STR "" void j() { std::string a(EMPTY_STR); @@ -133,6 +224,17 @@ std::string c(EMPTY_STR "u" EMPTY_STR); } +void jview() { + std::string_view a(EMPTY_STR); + // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization + // CHECK-FIXES: std::string_view a; + std::string_view b = (EMPTY_STR); + // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization + // CHECK-FIXES: std::string_view b; + + std::string_view c(EMPTY_STR "u" EMPTY_STR); +} + void k() { std::string a = "", b = "", c = ""; // CHECK-MESSAGES: [[@LINE-1]]:15: warning: redundant string initialization @@ -148,6 +250,21 @@ // CHECK-FIXES: std::string g = "u", h, i = "uuu", j, k; } +void kview() { + std::string_view a = "", b = "", c = ""; + // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization + // CHECK-MESSAGES: [[@LINE-2]]:28: warning: redundant string initialization + // CHECK-MESSAGES: [[@LINE-3]]:36: warning: redundant string initialization + // CHECK-FIXES: std::string_view a, b, c; + + std::string_view d = "u", e = "u", f = "u"; + + std::string_view g = "u", h = "", i = "uuu", j = "", k; + // CHECK-MESSAGES: [[@LINE-1]]:29: warning: redundant string initialization + // CHECK-MESSAGES: [[@LINE-2]]:48: warning: redundant string initialization + // CHECK-FIXES: std::string_view g = "u", h, i = "uuu", j, k; +} + // These cases should not generate warnings. extern void Param1(std::string param = ""); extern void Param2(const std::string& param = ""); @@ -242,6 +359,11 @@ std::string C; std::string D; std::string E = "NotEmpty"; + std::string_view F = ""; + // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization + // CHECK-FIXES: std::string_view F; + std::string_view G; + std::string_view H = "NotEmpty"; public: // Check redundant constructor where Field has a redundant initializer. @@ -274,6 +396,10 @@ // CHECK-MESSAGES: [[@LINE-2]]:23: warning: redundant string initialization // CHECK-FIXES: Foo(float) {} + Foo(double) : F{""}, G{""} {} + // CHECK-MESSAGES: [[@LINE-1]]:17: warning: redundant string initialization + // CHECK-MESSAGES: [[@LINE-2]]:24: warning: redundant string initialization + // CHECK-FIXES: Foo(double) {} // Check how it handles removing some redundant initializers while leaving // valid initializers intact. @@ -282,4 +408,10 @@ // CHECK-MESSAGES: [[@LINE-2]]:56: warning: redundant string initialization // CHECK-MESSAGES: [[@LINE-3]]:66: warning: redundant string initialization // CHECK-FIXES: Foo(std::string Arg) : A(Arg), C("NonEmpty"), E() {} + + Foo(std::string Arg, std::string Arg2) : A(Arg), C(Arg2), F(R"()"), G{""}, H("") {} + // CHECK-MESSAGES: [[@LINE-1]]:61: warning: redundant string initialization + // CHECK-MESSAGES: [[@LINE-2]]:71: warning: redundant string initialization + // CHECK-MESSAGES: [[@LINE-3]]:78: warning: redundant string initialization + // CHECK-FIXES: Foo(std::string Arg, std::string Arg2) : A(Arg), C(Arg2), H() {} };