| 1 | // RUN: %check_clang_tidy %s readability-redundant-string-cstr %t |
| 2 | |
| 3 | namespace std { |
| 4 | template <typename T> |
| 5 | class allocator {}; |
| 6 | template <typename T> |
| 7 | class char_traits {}; |
| 8 | template <typename C, typename T, typename A> |
| 9 | struct basic_string { |
| 10 | basic_string(); |
| 11 | // MSVC headers define two constructors instead of using optional arguments. |
| 12 | basic_string(const C *p); |
| 13 | basic_string(const C *p, const A &a); |
| 14 | const C *c_str() const; |
| 15 | const C *data() const; |
| 16 | }; |
| 17 | typedef basic_string<char, std::char_traits<char>, std::allocator<char>> string; |
| 18 | } |
| 19 | namespace llvm { |
| 20 | struct StringRef { |
| 21 | StringRef(const char *p); |
| 22 | StringRef(const std::string &); |
| 23 | }; |
| 24 | } |
| 25 | |
| 26 | void f1(const std::string &s) { |
| 27 | f1(s: s.c_str()); |
| 28 | // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'c_str' [readability-redundant-string-cstr] |
| 29 | // CHECK-FIXES: {{^ }}f1(s);{{$}} |
| 30 | f1(s: s.data()); |
| 31 | // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to 'data' [readability-redundant-string-cstr] |
| 32 | // CHECK-FIXES: {{^ }}f1(s);{{$}} |
| 33 | } |
| 34 | void f2(const llvm::StringRef r) { |
| 35 | std::string s; |
| 36 | f2(r: s.c_str()); |
| 37 | // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call {{.*}} |
| 38 | // CHECK-FIXES: {{^ }}std::string s;{{$}} |
| 39 | // CHECK-FIXES-NEXT: {{^ }}f2(s);{{$}} |
| 40 | } |
| 41 | void f3(const llvm::StringRef &r) { |
| 42 | std::string s; |
| 43 | f3(r: s.c_str()); |
| 44 | // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call {{.*}} |
| 45 | // CHECK-FIXES: {{^ }}std::string s;{{$}} |
| 46 | // CHECK-FIXES-NEXT: {{^ }}f3(s);{{$}} |
| 47 | } |
| 48 | |