File: modernize-return-braced-init-list.cpp

package info (click to toggle)
llvm-toolchain-6.0 1%3A6.0.1-10
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 598,080 kB
  • sloc: cpp: 3,046,253; ansic: 595,057; asm: 271,965; python: 128,926; objc: 106,554; sh: 21,906; lisp: 10,191; pascal: 6,094; ml: 5,544; perl: 5,265; makefile: 2,227; cs: 2,027; xml: 686; php: 212; csh: 117
file content (198 lines) | stat: -rw-r--r-- 3,628 bytes parent folder | download | duplicates (4)
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
// RUN: %check_clang_tidy %s modernize-return-braced-init-list %t -- -- -std=c++14

namespace std {
typedef decltype(sizeof(int)) size_t;

// libc++'s implementation
template <class _E>
class initializer_list {
  const _E *__begin_;
  size_t __size_;

  initializer_list(const _E *__b, size_t __s)
      : __begin_(__b),
        __size_(__s) {}

public:
  typedef _E value_type;
  typedef const _E &reference;
  typedef const _E &const_reference;
  typedef size_t size_type;

  typedef const _E *iterator;
  typedef const _E *const_iterator;

  initializer_list() : __begin_(nullptr), __size_(0) {}

  size_t size() const { return __size_; }
  const _E *begin() const { return __begin_; }
  const _E *end() const { return __begin_ + __size_; }
};

template <typename T>
class vector {
public:
  vector(T) {}
  vector(std::initializer_list<T>) {}
};
}

class Bar {};

Bar b0;

class Foo {
public:
  Foo(Bar) {}
  explicit Foo(Bar, unsigned int) {}
  Foo(unsigned int) {}
};

class Baz {
public:
  Foo m() {
    Bar bm;
    return Foo(bm);
    // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: avoid repeating the return type from the declaration; use a braced initializer list instead [modernize-return-braced-init-list]
    // CHECK-FIXES: return {bm};
  }
};

class Quux : public Foo {
public:
  Quux(Bar bar) : Foo(bar) {}
  Quux(unsigned, unsigned, unsigned k = 0) : Foo(k) {}
};

Foo f() {
  Bar b1;
  return Foo(b1);
  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: avoid repeating the return type
  // CHECK-FIXES: return {b1};
}

Foo f2() {
  Bar b2;
  return {b2};
}

auto f3() {
  Bar b3;
  return Foo(b3);
}

#define A(b) Foo(b)

Foo f4() {
  Bar b4;
  return A(b4);
}

Foo f5() {
  Bar b5;
  return Quux(b5);
}

Foo f6() {
  Bar b6;
  return Foo(b6, 1);
}

std::vector<int> f7() {
  int i7 = 1;
  return std::vector<int>(i7);
  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: avoid repeating the return type
}

Bar f8() {
  return {};
}

Bar f9() {
  return Bar();
  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: avoid repeating the return type
}

Bar f10() {
  return Bar{};
}

Foo f11(Bar b11) {
  return Foo(b11);
  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: avoid repeating the return type
  // CHECK-FIXES: return {b11};
}

Foo f12() {
  return f11(Bar());
}

Foo f13() {
  return Foo(Bar()); // 13
  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: avoid repeating the return type
  // CHECK-FIXES: return {Bar()}; // 13
}

Foo f14() {
  // FIXME: Type narrowing should not occur!
  return Foo(-1);
  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: avoid repeating the return type
  // CHECK-FIXES: return {-1};
}

Foo f15() {
  return Foo(f10());
  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: avoid repeating the return type
  // CHECK-FIXES: return {f10()};
}

Quux f16() {
  return Quux(1, 2);
  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: avoid repeating the return type
  // CHECK-FIXES: return {1, 2};
}

Quux f17() {
  return Quux(1, 2, 3);
  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: avoid repeating the return type
  // CHECK-FIXES: return {1, 2, 3};
}

template <typename T>
T f19() {
  return T();
}

Bar i1 = f19<Bar>();
Baz i2 = f19<Baz>();

template <typename T>
Foo f20(T t) {
  return Foo(t);
}

Foo i3 = f20(b0);

template <typename T>
class BazT {
public:
  T m() {
    Bar b;
    return T(b);
  }

  Foo m2(T t) {
    return Foo(t);
  }
};

BazT<Foo> bazFoo;
Foo i4 = bazFoo.m();
Foo i5 = bazFoo.m2(b0);

BazT<Quux> bazQuux;
Foo i6 = bazQuux.m();
Foo i7 = bazQuux.m2(b0);

auto v1 = []() { return std::vector<int>({1, 2}); }();
auto v2 = []() -> std::vector<int> { return std::vector<int>({1, 2}); }();