1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
// RUN: %check_clang_tidy -check-suffix=STDFORMAT -std=c++20 %s readability-redundant-string-cstr %t -- -- -isystem %clang_tidy_headers -DTEST_STDFORMAT
// RUN: %check_clang_tidy -check-suffixes=STDFORMAT,STDPRINT -std=c++2b %s readability-redundant-string-cstr %t -- -- -isystem %clang_tidy_headers -DTEST_STDFORMAT -DTEST_STDPRINT
#include <string>
namespace std {
template<typename T>
struct type_identity { using type = T; };
template<typename T>
using type_identity_t = typename type_identity<T>::type;
template <typename CharT, typename... Args>
struct basic_format_string {
consteval basic_format_string(const CharT *format) : str(format) {}
basic_string_view<CharT, std::char_traits<CharT>> str;
};
template<typename... Args>
using format_string = basic_format_string<char, type_identity_t<Args>...>;
template<typename... Args>
using wformat_string = basic_format_string<wchar_t, type_identity_t<Args>...>;
#if defined(TEST_STDFORMAT)
template<typename ...Args>
std::string format(format_string<Args...>, Args &&...);
template<typename ...Args>
std::string format(wformat_string<Args...>, Args &&...);
#endif // TEST_STDFORMAT
#if defined(TEST_STDPRINT)
template<typename ...Args>
void print(format_string<Args...>, Args &&...);
template<typename ...Args>
void print(wformat_string<Args...>, Args &&...);
#endif // TEST_STDPRINT
}
namespace notstd {
#if defined(TEST_STDFORMAT)
template<typename ...Args>
std::string format(const char *, Args &&...);
template<typename ...Args>
std::string format(const wchar_t *, Args &&...);
#endif // TEST_STDFORMAT
#if defined(TEST_STDPRINT)
template<typename ...Args>
void print(const char *, Args &&...);
template<typename ...Args>
void print(const wchar_t *, Args &&...);
#endif // TEST_STDPRINT
}
std::string return_temporary();
std::wstring return_wtemporary();
#if defined(TEST_STDFORMAT)
void std_format(const std::string &s1, const std::string &s2, const std::string &s3) {
auto r1 = std::format("One:{}\n", s1.c_str());
// CHECK-MESSAGES-STDFORMAT: :[[@LINE-1]]:37: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-FIXES-STDFORMAT: {{^ }}auto r1 = std::format("One:{}\n", s1);
auto r2 = std::format("One:{} Two:{} Three:{} Four:{}\n", s1.c_str(), s2, s3.c_str(), return_temporary().c_str());
// CHECK-MESSAGES-STDFORMAT: :[[@LINE-1]]:61: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-MESSAGES-STDFORMAT: :[[@LINE-2]]:77: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-MESSAGES-STDFORMAT: :[[@LINE-3]]:89: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-FIXES-STDFORMAT: {{^ }}auto r2 = std::format("One:{} Two:{} Three:{} Four:{}\n", s1, s2, s3, return_temporary());
using namespace std;
auto r3 = format("Four:{}\n", s1.c_str());
// CHECK-MESSAGES-STDFORMAT: :[[@LINE-1]]:33: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-FIXES-STDFORMAT: {{^ }}auto r3 = format("Four:{}\n", s1);
}
void std_format_wide(const std::wstring &s1, const std::wstring &s2, const std::wstring &s3) {
auto r1 = std::format(L"One:{}\n", s1.c_str());
// CHECK-MESSAGES-STDFORMAT: :[[@LINE-1]]:38: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-FIXES-STDFORMAT: {{^ }}auto r1 = std::format(L"One:{}\n", s1);
auto r2 = std::format(L"One:{} Two:{} Three:{} Four:{}\n", s1.c_str(), s2, s3.c_str(), return_wtemporary().c_str());
// CHECK-MESSAGES-STDFORMAT: :[[@LINE-1]]:62: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-MESSAGES-STDFORMAT: :[[@LINE-2]]:78: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-MESSAGES-STDFORMAT: :[[@LINE-3]]:90: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-FIXES-STDFORMAT: {{^ }}auto r2 = std::format(L"One:{} Two:{} Three:{} Four:{}\n", s1, s2, s3, return_wtemporary());
using namespace std;
auto r3 = format(L"Four:{}\n", s1.c_str());
// CHECK-MESSAGES-STDFORMAT: :[[@LINE-1]]:34: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-FIXES-STDFORMAT: {{^ }}auto r3 = format(L"Four:{}\n", s1);
}
// There's are c_str() calls here, so it shouldn't be touched.
std::string std_format_no_cstr(const std::string &s1, const std::string &s2) {
return std::format("One: {}, Two: {}\n", s1, s2);
}
// There's are c_str() calls here, so it shouldn't be touched.
std::string std_format_no_cstr_wide(const std::string &s1, const std::string &s2) {
return std::format(L"One: {}, Two: {}\n", s1, s2);
}
// This is not std::format, so it shouldn't be fixed.
std::string not_std_format(const std::string &s1) {
return notstd::format("One: {}\n", s1.c_str());
using namespace notstd;
format("One: {}\n", s1.c_str());
}
// This is not std::format, so it shouldn't be fixed.
std::string not_std_format_wide(const std::string &s1) {
return notstd::format(L"One: {}\n", s1.c_str());
using namespace notstd;
format(L"One: {}\n", s1.c_str());
}
#endif // TEST_STDFORMAT
#if defined(TEST_STDPRINT)
void std_print(const std::string &s1, const std::string &s2, const std::string &s3) {
std::print("One:{}\n", s1.c_str());
// CHECK-MESSAGES-STDPRINT: :[[@LINE-1]]:26: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-FIXES-STDPRINT: {{^ }}std::print("One:{}\n", s1);
std::print("One:{} Two:{} Three:{} Four:{}\n", s1.c_str(), s2, s3.c_str(), return_temporary().c_str());
// CHECK-MESSAGES-STDPRINT: :[[@LINE-1]]:50: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-MESSAGES-STDPRINT: :[[@LINE-2]]:66: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-MESSAGES-STDPRINT: :[[@LINE-3]]:78: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-FIXES-STDPRINT: {{^ }}std::print("One:{} Two:{} Three:{} Four:{}\n", s1, s2, s3, return_temporary());
using namespace std;
print("Four:{}\n", s1.c_str());
// CHECK-MESSAGES-STDPRINT: :[[@LINE-1]]:22: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-FIXES-STDPRINT: {{^ }}print("Four:{}\n", s1);
}
void std_print_wide(const std::wstring &s1, const std::wstring &s2, const std::wstring &s3) {
std::print(L"One:{}\n", s1.c_str());
// CHECK-MESSAGES-STDPRINT: :[[@LINE-1]]:27: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-FIXES-STDPRINT: {{^ }}std::print(L"One:{}\n", s1);
std::print(L"One:{} Two:{} Three:{} Four:{}\n", s1.c_str(), s2, s3.c_str(), return_wtemporary().c_str());
// CHECK-MESSAGES-STDPRINT: :[[@LINE-1]]:51: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-MESSAGES-STDPRINT: :[[@LINE-2]]:67: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-MESSAGES-STDPRINT: :[[@LINE-3]]:79: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-FIXES-STDPRINT: {{^ }}std::print(L"One:{} Two:{} Three:{} Four:{}\n", s1, s2, s3, return_wtemporary());
using namespace std;
print(L"Four:{}\n", s1.c_str());
// CHECK-MESSAGES-STDPRINT: :[[@LINE-1]]:23: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
// CHECK-FIXES-STDPRINT: {{^ }}print(L"Four:{}\n", s1);
}
// There's no c_str() call here, so it shouldn't be touched.
void std_print_no_cstr(const std::string &s1, const std::string &s2) {
std::print("One: {}, Two: {}\n", s1, s2);
}
// There's no c_str() call here, so it shouldn't be touched.
void std_print_no_cstr_wide(const std::wstring &s1, const std::wstring &s2) {
std::print(L"One: {}, Two: {}\n", s1, s2);
}
// This isn't std::print, so it shouldn't be fixed.
void not_std_print(const std::string &s1) {
notstd::print("One: {}\n", s1.c_str());
using namespace notstd;
print("One: {}\n", s1.c_str());
}
// This isn't std::print, so it shouldn't be fixed.
void not_std_print_wide(const std::string &s1) {
notstd::print(L"One: {}\n", s1.c_str());
using namespace notstd;
print(L"One: {}\n", s1.c_str());
}
#endif // TEST_STDPRINT
#if defined(TEST_STDFORMAT)
// We can't declare these earlier since they make the "using namespace std"
// tests ambiguous.
template<typename ...Args>
std::string format(const char *, Args &&...);
template<typename ...Args>
std::string format(const wchar_t *, Args &&...);
// This is not std::format, so it shouldn't be fixed.
std::string not_std_format2(const std::wstring &s1) {
return format("One: {}\n", s1.c_str());
}
// This is not std::format, so it shouldn't be fixed.
std::string not_std_format2_wide(const std::wstring &s1) {
return format(L"One: {}\n", s1.c_str());
}
#endif // TEST_STDFORMAT
#if defined(TEST_STDPRINT)
template<typename ...Args>
void print(const char *, Args &&...);
template<typename ...Args>
void print(const wchar_t *, Args &&...);
// This isn't std::print, so it shouldn't be fixed.
void not_std_print2(const std::string &s1) {
print("One: {}\n", s1.c_str());
}
// This isn't std::print, so it shouldn't be fixed.
void not_std_print2_wide(const std::string &s1) {
print(L"One: {}\n", s1.c_str());
}
#endif // TEST_STDPRINT
|