xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-format.cpp (revision 4589bf90aa1849b3851945fb611f5c96af821332)
1 // RUN: %check_clang_tidy \
2 // RUN:   -std=c++20 %s modernize-use-std-format %t -- \
3 // RUN:   -config="{CheckOptions: {StrictMode: true}}" \
4 // RUN:   -- -isystem %clang_tidy_headers
5 // RUN: %check_clang_tidy \
6 // RUN:   -std=c++20 %s modernize-use-std-format %t -- \
7 // RUN:   -config="{CheckOptions: {StrictMode: false}}" \
8 // RUN:   -- -isystem %clang_tidy_headers
9 #include <string>
10 // CHECK-FIXES: #include <format>
11 
12 namespace absl
13 {
14 template <typename S, typename... Args>
15 std::string StrFormat(const S &format, const Args&... args);
16 } // namespace absl
17 
18 template <typename T>
19 struct iterator {
20   T *operator->();
21   T &operator*();
22 };
23 
24 std::string StrFormat_simple() {
25   return absl::StrFormat("Hello");
26   // CHECK-MESSAGES: [[@LINE-1]]:10: warning: use 'std::format' instead of 'StrFormat' [modernize-use-std-format]
27   // CHECK-FIXES: return std::format("Hello");
28 }
29 
30 std::string StrFormat_complex(const char *name, double value) {
31   return absl::StrFormat("'%s'='%f'", name, value);
32   // CHECK-MESSAGES: [[@LINE-1]]:10: warning: use 'std::format' instead of 'StrFormat' [modernize-use-std-format]
33   // CHECK-FIXES: return std::format("'{}'='{:f}'", name, value);
34 }
35 
36 std::string StrFormat_integer_conversions() {
37   return absl::StrFormat("int:%d int:%d char:%c char:%c", 65, 'A', 66, 'B');
38   // CHECK-MESSAGES: [[@LINE-1]]:10: warning: use 'std::format' instead of 'StrFormat' [modernize-use-std-format]
39   // CHECK-FIXES: return std::format("int:{} int:{:d} char:{:c} char:{}", 65, 'A', 66, 'B');
40 }
41 
42 // FormatConverter is capable of removing newlines from the end of the format
43 // string. Ensure that isn't incorrectly happening for std::format.
44 std::string StrFormat_no_newline_removal() {
45   return absl::StrFormat("a line\n");
46   // CHECK-MESSAGES: [[@LINE-1]]:10: warning: use 'std::format' instead of 'StrFormat' [modernize-use-std-format]
47   // CHECK-FIXES: return std::format("a line\n");
48 }
49 
50 // FormatConverter is capable of removing newlines from the end of the format
51 // string. Ensure that isn't incorrectly happening for std::format.
52 std::string StrFormat_cstr_removal(const std::string &s1, const std::string *s2) {
53   return absl::StrFormat("%s %s %s %s", s1.c_str(), s1.data(), s2->c_str(), s2->data());
54   // CHECK-MESSAGES: [[@LINE-1]]:10: warning: use 'std::format' instead of 'StrFormat' [modernize-use-std-format]
55   // CHECK-FIXES: return std::format("{} {} {} {}", s1, s1, *s2, *s2);
56 }
57 
58 std::string StrFormat_strict_conversion() {
59   const unsigned char uc = 'A';
60   return absl::StrFormat("Integer %hhd from unsigned char\n", uc);
61   // CHECK-MESSAGES: [[@LINE-1]]:10: warning: use 'std::format' instead of 'StrFormat' [modernize-use-std-format]
62   // CHECK-FIXES: return std::format("Integer {} from unsigned char\n", uc);
63 }
64 
65 std::string StrFormat_field_width_and_precision() {
66   auto s1 = absl::StrFormat("width only:%*d width and precision:%*.*f precision only:%.*f", 3, 42, 4, 2, 3.14159265358979323846, 5, 2.718);
67   // CHECK-MESSAGES: [[@LINE-1]]:13: warning: use 'std::format' instead of 'StrFormat' [modernize-use-std-format]
68   // CHECK-FIXES: std::format("width only:{:{}} width and precision:{:{}.{}f} precision only:{:.{}f}", 42, 3, 3.14159265358979323846, 4, 2, 2.718, 5);
69 
70   auto s2 = absl::StrFormat("width and precision positional:%1$*2$.*3$f after", 3.14159265358979323846, 4, 2);
71   // CHECK-MESSAGES: [[@LINE-1]]:13: warning: use 'std::format' instead of 'StrFormat' [modernize-use-std-format]
72   // CHECK-FIXES: std::format("width and precision positional:{0:{1}.{2}f} after", 3.14159265358979323846, 4, 2);
73 
74   const int width = 10, precision = 3;
75   const unsigned int ui1 = 42, ui2 = 43, ui3 = 44;
76   auto s3 = absl::StrFormat("casts width only:%*d width and precision:%*.*d precision only:%.*d\n", 3, ui1, 4, 2, ui2, 5, ui3);
77   // CHECK-MESSAGES: [[@LINE-1]]:13: warning: use 'std::format' instead of 'StrFormat' [modernize-use-std-format]
78   // CHECK-FIXES-NOTSTRICT: std::format("casts width only:{:{}} width and precision:{:{}.{}} precision only:{:.{}}", ui1, 3, ui2, 4, 2, ui3, 5);
79   // CHECK-FIXES-STRICT: std::format("casts width only:{:{}} width and precision:{:{}.{}} precision only:{:.{}}", static_cast<int>(ui1), 3, static_cast<int>(ui2), 4, 2, static_cast<int>(ui3), 5);
80 
81   auto s4 = absl::StrFormat("c_str removal width only:%*s width and precision:%*.*s precision only:%.*s", 3, s1.c_str(), 4, 2, s2.c_str(), 5, s3.c_str());
82   // CHECK-MESSAGES: [[@LINE-1]]:13: warning: use 'std::format' instead of 'StrFormat' [modernize-use-std-format]
83   // CHECK-FIXES: std::format("c_str removal width only:{:>{}} width and precision:{:>{}.{}} precision only:{:.{}}", s1, 3, s2, 4, 2, s3, 5);
84 
85   const std::string *ps1 = &s1, *ps2 = &s2, *ps3 = &s3;
86   auto s5 = absl::StrFormat("c_str() removal pointer width only:%-*s width and precision:%-*.*s precision only:%-.*s", 3, ps1->c_str(), 4, 2, ps2->c_str(), 5, ps3->c_str());
87   // CHECK-MESSAGES: [[@LINE-1]]:13: warning: use 'std::format' instead of 'StrFormat' [modernize-use-std-format]
88   // CHECK-FIXES: std::format("c_str() removal pointer width only:{:{}} width and precision:{:{}.{}} precision only:{:.{}}", *ps1, 3, *ps2, 4, 2, *ps3, 5);
89 
90   iterator<std::string> is1, is2, is3;
91   auto s6 = absl::StrFormat("c_str() removal iterator width only:%-*s width and precision:%-*.*s precision only:%-.*s", 3, is1->c_str(), 4, 2, is2->c_str(), 5, is3->c_str());
92   // CHECK-MESSAGES: [[@LINE-1]]:13: warning: use 'std::format' instead of 'StrFormat' [modernize-use-std-format]
93   // CHECK-FIXES: std::format("c_str() removal iterator width only:{:{}} width and precision:{:{}.{}} precision only:{:.{}}", *is1, 3, *is2, 4, 2, *is3, 5);
94 
95   return s1 + s2 + s3 + s4 + s5 + s6;
96 }
97 
98 std::string StrFormat_macros() {
99   // The function call is replaced even though it comes from a macro.
100 #define FORMAT absl::StrFormat
101   auto s1 = FORMAT("Hello %d", 42);
102   // CHECK-MESSAGES: [[@LINE-1]]:13: warning: use 'std::format' instead of 'StrFormat' [modernize-use-std-format]
103   // CHECK-FIXES: std::format("Hello {}", 42);
104 
105   // The format string is replaced even though it comes from a macro, this
106   // behaviour is required so that that <inttypes.h> macros are replaced.
107 #define FORMAT_STRING "Hello %s"
108   auto s2 = absl::StrFormat(FORMAT_STRING, 42);
109   // CHECK-MESSAGES: [[@LINE-1]]:13: warning: use 'std::format' instead of 'StrFormat' [modernize-use-std-format]
110   // CHECK-FIXES: std::format("Hello {}", 42);
111 
112   // Arguments that are macros aren't replaced with their value, even if they are rearranged.
113 #define VALUE 3.14159265358979323846
114 #define WIDTH 10
115 #define PRECISION 4
116   auto s3 = absl::StrFormat("Hello %*.*f", WIDTH, PRECISION, VALUE);
117   // CHECK-MESSAGES: [[@LINE-1]]:13: warning: use 'std::format' instead of 'StrFormat' [modernize-use-std-format]
118   // CHECK-FIXES: std::format("Hello {:{}.{}f}", VALUE, WIDTH, PRECISION);
119 }
120