1 | // RUN: %check_clang_tidy %s modernize-use-emplace %t -- \ |
2 | // RUN: -config="{CheckOptions: \ |
3 | // RUN: {modernize-use-emplace.IgnoreImplicitConstructors: \ |
4 | // RUN: true}}" |
5 | |
6 | namespace std { |
7 | template <typename> |
8 | class initializer_list |
9 | { |
10 | public: |
11 | initializer_list() noexcept {} |
12 | }; |
13 | |
14 | template <typename T> |
15 | class vector { |
16 | public: |
17 | vector() = default; |
18 | vector(initializer_list<T>) {} |
19 | |
20 | void push_back(const T &) {} |
21 | void push_back(T &&) {} |
22 | |
23 | template <typename... Args> |
24 | void emplace_back(Args &&... args){}; |
25 | ~vector(); |
26 | }; |
27 | |
28 | } // namespace std |
29 | |
30 | void testInts() { |
31 | std::vector<int> v; |
32 | v.push_back(42); |
33 | v.push_back(int(42)); |
34 | v.push_back(int{42}); |
35 | v.push_back(42.0); |
36 | int z; |
37 | v.push_back(z); |
38 | } |
39 | |
40 | struct Something { |
41 | Something(int a, int b = 41) {} |
42 | Something() {} |
43 | void push_back(Something); |
44 | int getInt() { return 42; } |
45 | }; |
46 | |
47 | struct Convertable { |
48 | operator Something() { return Something{}; } |
49 | }; |
50 | |
51 | struct Zoz { |
52 | Zoz(Something, int = 42) {} |
53 | }; |
54 | |
55 | Zoz getZoz(Something s) { return Zoz(s); } |
56 | |
57 | void test_Something() { |
58 | std::vector<Something> v; |
59 | |
60 | v.push_back(Something(1, 2)); |
61 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back instead of push_back [modernize-use-emplace] |
62 | // CHECK-FIXES: v.emplace_back(1, 2); |
63 | |
64 | v.push_back(Something{1, 2}); |
65 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back |
66 | // CHECK-FIXES: v.emplace_back(1, 2); |
67 | |
68 | v.push_back(Something()); |
69 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back |
70 | // CHECK-FIXES: v.emplace_back(); |
71 | |
72 | v.push_back(Something{}); |
73 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back |
74 | // CHECK-FIXES: v.emplace_back(); |
75 | |
76 | Something Different; |
77 | v.push_back(Something(Different.getInt(), 42)); |
78 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back |
79 | // CHECK-FIXES: v.emplace_back(Different.getInt(), 42); |
80 | |
81 | v.push_back(Different.getInt()); |
82 | v.push_back(42); |
83 | |
84 | Something temporary(42, 42); |
85 | temporary.push_back(temporary); |
86 | v.push_back(temporary); |
87 | |
88 | v.push_back(Convertable()); |
89 | v.push_back(Convertable{}); |
90 | Convertable s; |
91 | v.push_back(s); |
92 | } |
93 | |
94 | template <typename ElemType> |
95 | void dependOnElem() { |
96 | std::vector<ElemType> v; |
97 | v.push_back(ElemType(42)); |
98 | } |
99 | |
100 | template <typename ContainerType> |
101 | void dependOnContainer() { |
102 | ContainerType v; |
103 | v.push_back(Something(42)); |
104 | } |
105 | |
106 | void callDependent() { |
107 | dependOnElem<Something>(); |
108 | dependOnContainer<std::vector<Something>>(); |
109 | } |
110 | |
111 | void test2() { |
112 | std::vector<Zoz> v; |
113 | v.push_back(Zoz(Something(21, 37))); |
114 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back |
115 | // CHECK-FIXES: v.emplace_back(Something(21, 37)); |
116 | |
117 | v.push_back(Zoz(Something(21, 37), 42)); |
118 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back |
119 | // CHECK-FIXES: v.emplace_back(Something(21, 37), 42); |
120 | |
121 | v.push_back(getZoz(s: Something(1, 2))); |
122 | } |
123 | |