1// RUN: %check_clang_tidy -std=c++17-or-later %s readability-isolate-declaration %t
2
3template <typename T1, typename T2>
4struct pair {
5 T1 first;
6 T2 second;
7 pair(T1 v1, T2 v2) : first(v1), second(v2) {}
8
9 template <int N>
10 decltype(auto) get() const {
11 if constexpr (N == 0)
12 return first;
13 else if constexpr (N == 1)
14 return second;
15 }
16};
17
18void forbidden_transformations() {
19 if (int i = 42, j = i; i == j)
20 ;
21 switch (int i = 12, j = 14; i)
22 ;
23
24 auto [i, j] = pair<int, int>(42, 42);
25}
26
27struct SomeClass {
28 SomeClass() = default;
29 SomeClass(int value);
30};
31
32namespace std {
33template <typename T>
34class initializer_list {};
35
36template <typename T>
37class vector {
38public:
39 vector() = default;
40 vector(initializer_list<T> init) {}
41};
42
43class string {
44public:
45 string() = default;
46 string(const char *) {}
47};
48
49namespace string_literals {
50string operator""s(const char *, decltype(sizeof(int))) {
51 return string();
52}
53} // namespace string_literals
54} // namespace std
55
56namespace Types {
57typedef int MyType;
58} // namespace Types
59
60int touch1, touch2;
61
62void modern() {
63 auto autoInt1 = 3, autoInt2 = 4;
64 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
65 // CHECK-FIXES: auto autoInt1 = 3;
66 // CHECK-FIXES: {{^ }}auto autoInt2 = 4;
67
68 decltype(int()) declnottouch = 4;
69 decltype(int()) declint1 = 5, declint2 = 3;
70 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
71 // CHECK-FIXES: decltype(int()) declint1 = 5;
72 // CHECK-FIXES: {{^ }}decltype(int()) declint2 = 3;
73
74 std::vector<int> vectorA = {1, 2}, vectorB = {1, 2, 3}, vectorC({1, 1, 1});
75 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
76 // CHECK-FIXES: std::vector<int> vectorA = {1, 2};
77 // CHECK-FIXES: {{^ }}std::vector<int> vectorB = {1, 2, 3};
78 // CHECK-FIXES: {{^ }}std::vector<int> vectorC({1, 1, 1});
79
80 using uType = int;
81 uType utype1, utype2;
82 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
83 // CHECK-FIXES: uType utype1;
84 // CHECK-FIXES: {{^ }}uType utype2;
85
86 Types::MyType mytype1, mytype2, mytype3 = 3;
87 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: multiple declarations in a single statement reduces readability
88 // CHECK-FIXES: Types::MyType mytype1;
89 // CHECK-FIXES: {{^ }}Types::MyType mytype2;
90 // CHECK-FIXES: {{^ }}Types::MyType mytype3 = 3;
91
92 {
93 using namespace std::string_literals;
94
95 std::vector<std::string> s{"foo"s, "bar"s}, t{"foo"s}, u, a({"hey", "you"}), bb = {"h", "a"};
96 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: multiple declarations in a single statement reduces readability
97 // CHECK-FIXES: std::vector<std::string> s{"foo"s, "bar"s};
98 // CHECK-FIXES: {{^ }}std::vector<std::string> t{"foo"s};
99 // CHECK-FIXES: {{^ }}std::vector<std::string> u;
100 // CHECK-FIXES: {{^ }}std::vector<std::string> a({"hey", "you"});
101 // CHECK-FIXES: {{^ }}std::vector<std::string> bb = {"h", "a"};
102 }
103}
104

source code of clang-tools-extra/test/clang-tidy/checkers/readability/isolate-declaration-cxx17.cpp