[clang-tidy] Fix readability-redundant-string-init for c++17/c++2a
Summary:
`readability-redundant-string-init` was one of several clang-tidy checks documented as failing for C++17. (The failure mode in C++17 is that it changes `std::string Name = ""`; to `std::string Name = Name;`, which actually compiles but crashes at run-time.)
Analyzing the AST with `clang -Xclang -ast-dump` showed that the outer `CXXConstructExprs` that previously held the correct SourceRange were being elided in C++17/2a, but the containing `VarDecl` expressions still had all the relevant information. So this patch changes the fix to get its source ranges from `VarDecl`.
It adds one test `std::string g = "u", h = "", i = "uuu", j = "", k;` to confirm proper warnings and fixit replacements in a single `DeclStmt` where some strings require replacement and others don't. The readability-redundant-string-init.cpp and readability-redundant-string-init-msvc.cpp tests now pass for C++11/14/17/2a.
Reviewers: gribozavr, etienneb, alexfh, hokein, aaron.ballman, gribozavr2
Patch by: poelmanc
Subscribers: NoQ, MyDeveloperDay, Eugene.Zelenko, dylanmckay, cfe-commits
Tags: #clang, #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D69238
2019-11-19 20:45:53 +08:00
|
|
|
// RUN: %check_clang_tidy %s readability-redundant-string-init %t
|
2016-03-23 01:39:36 +08:00
|
|
|
|
|
|
|
namespace std {
|
|
|
|
template <typename T>
|
|
|
|
class allocator {};
|
|
|
|
template <typename T>
|
|
|
|
class char_traits {};
|
|
|
|
template <typename C, typename T = std::char_traits<C>, typename A = std::allocator<C>>
|
|
|
|
struct basic_string {
|
|
|
|
basic_string();
|
|
|
|
basic_string(const basic_string&);
|
|
|
|
// MSVC headers define two constructors instead of using optional arguments.
|
|
|
|
basic_string(const C *);
|
|
|
|
basic_string(const C *, const A &);
|
|
|
|
~basic_string();
|
|
|
|
};
|
|
|
|
typedef basic_string<char> string;
|
|
|
|
typedef basic_string<wchar_t> wstring;
|
|
|
|
}
|
|
|
|
|
|
|
|
void f() {
|
|
|
|
std::string a = "";
|
|
|
|
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: redundant string initialization [readability-redundant-string-init]
|
|
|
|
// CHECK-FIXES: std::string a;
|
|
|
|
std::string b("");
|
|
|
|
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: redundant string initialization
|
|
|
|
// CHECK-FIXES: std::string b;
|
|
|
|
std::string c = R"()";
|
|
|
|
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: redundant string initialization
|
|
|
|
// CHECK-FIXES: std::string c;
|
|
|
|
std::string d(R"()");
|
|
|
|
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: redundant string initialization
|
|
|
|
// CHECK-FIXES: std::string d;
|
|
|
|
|
|
|
|
std::string u = "u";
|
|
|
|
std::string w("w");
|
|
|
|
std::string x = R"(x)";
|
|
|
|
std::string y(R"(y)");
|
|
|
|
std::string z;
|
|
|
|
}
|
|
|
|
|
|
|
|
void g() {
|
|
|
|
std::wstring a = L"";
|
|
|
|
// CHECK-MESSAGES: [[@LINE-1]]:16: warning: redundant string initialization
|
|
|
|
// CHECK-FIXES: std::wstring a;
|
|
|
|
std::wstring b(L"");
|
|
|
|
// CHECK-MESSAGES: [[@LINE-1]]:16: warning: redundant string initialization
|
|
|
|
// CHECK-FIXES: std::wstring b;
|
|
|
|
std::wstring c = LR"()";
|
|
|
|
// CHECK-MESSAGES: [[@LINE-1]]:16: warning: redundant string initialization
|
|
|
|
// CHECK-FIXES: std::wstring c;
|
|
|
|
std::wstring d(LR"()");
|
|
|
|
// CHECK-MESSAGES: [[@LINE-1]]:16: warning: redundant string initialization
|
|
|
|
// CHECK-FIXES: std::wstring d;
|
|
|
|
|
|
|
|
std::wstring u = L"u";
|
|
|
|
std::wstring w(L"w");
|
|
|
|
std::wstring x = LR"(x)";
|
|
|
|
std::wstring y(LR"(y)");
|
|
|
|
std::wstring z;
|
|
|
|
}
|