Debian Codegraph @

clang-tools-extra/docs/clang-tidy/checks/bugprone/use-after-move.rst

raw
1 .. title:: clang-tidy - bugprone-use-after-move
2
3 bugprone-use-after-move
4 =======================
5
6 Warns if an object is used after it has been moved, for example:
7
8 .. code-block:: c++
9
10 std::string str = "Hello, world!\n";
11 std::vector<std::string> messages;
12 messages.emplace_back(std::move(str));
13 std::cout << str;
14
15 The last line will trigger a warning that ``str`` is used after it has been
16 moved.
17
18 The check does not trigger a warning if the object is reinitialized after the
19 move and before the use. For example, no warning will be output for this code:
20
21 .. code-block:: c++
22
23 messages.emplace_back(std::move(str));
24 str = "Greetings, stranger!\n";
25 std::cout << str;
26
27 Subsections below explain more precisely what exactly the check considers to be
28 a move, use, and reinitialization.
29
30 The check takes control flow into account. A warning is only emitted if the use
31 can be reached from the move. This means that the following code does not
32 produce a warning:
33
34 .. code-block:: c++
35
36 if (condition) {
37 messages.emplace_back(std::move(str));
38 } else {
39 std::cout << str;
40 }
41
42 On the other hand, the following code does produce a warning:
43
44 .. code-block:: c++
45
46 for (int i = 0; i < 10; ++i) {
47 std::cout << str;
48 messages.emplace_back(std::move(str));
49 }
50
51 (The use-after-move happens on the second iteration of the loop.)
52
53 In some cases, the check may not be able to detect that two branches are
54 mutually exclusive. For example (assuming that ``i`` is an int):
55
56 .. code-block:: c++
57
58 if (i == 1) {
59 messages.emplace_back(std::move(str));
60 }
61 if (i == 2) {
62 std::cout << str;
63 }
64
65 In this case, the check will erroneously produce a warning, even though it is
66 not possible for both the move and the use to be executed. More formally, the
67 analysis is `flow-sensitive but not path-sensitive
68 <https://en.wikipedia.org/wiki/Data-flow_analysis#Sensitivities>`_.
69
70 Silencing erroneous warnings
71 ----------------------------
72
73 An erroneous warning can be silenced by reinitializing the object after the
74 move:
75
76 .. code-block:: c++
77
78 if (i == 1) {
79 messages.emplace_back(std::move(str));
80 str = "";
81 }
82 if (i == 2) {
83 std::cout << str;
84 }
85
86 If you want to avoid the overhead of actually reinitializing the object, you can
87 create a dummy function that causes the check to assume the object was
88 reinitialized:
89
90 .. code-block:: c++
91
92 template <class T>
93 void IS_INITIALIZED(T&) {}
94
95 You can use this as follows:
96
97 .. code-block:: c++
98
99 if (i == 1) {
100 messages.emplace_back(std::move(str));
101 }
102 if (i == 2) {
103 IS_INITIALIZED(str);
104 std::cout << str;
105 }
106
107 The check will not output a warning in this case because passing the object to a
108 function as a non-const pointer or reference counts as a reinitialization (see section
109 `Reinitialization`_ below).
110
111 Unsequenced moves, uses, and reinitializations
112 ----------------------------------------------
113
114 In many cases, C++ does not make any guarantees about the order in which
115 sub-expressions of a statement are evaluated. This means that in code like the
116 following, it is not guaranteed whether the use will happen before or after the
117 move:
118
119 .. code-block:: c++
120
121 void f(int i, std::vector<int> v);
122 std::vector<int> v = { 1, 2, 3 };
123 f(v[1], std::move(v));
124
125 In this kind of situation, the check will note that the use and move are
126 unsequenced.
127
128 The check will also take sequencing rules into account when reinitializations
129 occur in the same statement as moves or uses. A reinitialization is only
130 considered to reinitialize a variable if it is guaranteed to be evaluated after
131 the move and before the use.
132
133 Move
134 ----
135
136 The check currently only considers calls of ``std::move`` on local variables or
137 function parameters. It does not check moves of member variables or global
138 variables.
139
140 Any call of ``std::move`` on a variable is considered to cause a move of that
141 variable, even if the result of ``std::move`` is not passed to an rvalue
142 reference parameter.
143
144 This means that the check will flag a use-after-move even on a type that does
145 not define a move constructor or move assignment operator. This is intentional.
146 Developers may use ``std::move`` on such a type in the expectation that the type
147 will add move semantics in the future. If such a ``std::move`` has the potential
148 to cause a use-after-move, we want to warn about it even if the type does not
149 implement move semantics yet.
150
151 Furthermore, if the result of ``std::move`` *is* passed to an rvalue reference
152 parameter, this will always be considered to cause a move, even if the function
153 that consumes this parameter does not move from it, or if it does so only
154 conditionally. For example, in the following situation, the check will assume
155 that a move always takes place:
156
157 .. code-block:: c++
158
159 std::vector<std::string> messages;
160 void f(std::string &&str) {
161 // Only remember the message if it isn't empty.
162 if (!str.empty()) {
163 messages.emplace_back(std::move(str));
164 }
165 }
166 std::string str = "";
167 f(std::move(str));
168
169 The check will assume that the last line causes a move, even though, in this
170 particular case, it does not. Again, this is intentional.
171
172 There is one special case: A call to ``std::move`` inside a ``try_emplace`` call
173 is conservatively assumed not to move. This is to avoid spurious warnings, as
174 the check has no way to reason about the ``bool`` returned by ``try_emplace``.
175
176 When analyzing the order in which moves, uses and reinitializations happen (see
177 section `Unsequenced moves, uses, and reinitializations`_), the move is assumed
178 to occur in whichever function the result of the ``std::move`` is passed to.
179
180 The check also handles perfect-forwarding with ``std::forward`` so the
181 following code will also trigger a use-after-move warning.
182
183 .. code-block:: c++
184
185 void consume(int);
186
187 void f(int&& i) {
188 consume(std::forward<int>(i));
189 consume(std::forward<int>(i)); // use-after-move
190 }
191
192 Use
193 ---
194
195 Any occurrence of the moved variable that is not a reinitialization (see below)
196 is considered to be a use.
197
198 An exception to this are objects of type ``std::unique_ptr``,
199 ``std::shared_ptr`` and ``std::weak_ptr``, which have defined move behavior
200 (objects of these classes are guaranteed to be empty after they have been moved
201 from). Therefore, an object of these classes will only be considered to be used
202 if it is dereferenced, i.e. if ``operator*``, ``operator->`` or ``operator[]``
203 (in the case of ``std::unique_ptr<T []>``) is called on it.
204
205 If multiple uses occur after a move, only the first of these is flagged.
206
207 Reinitialization
208 ----------------
209
210 The check considers a variable to be reinitialized in the following cases:
211
212 - The variable occurs on the left-hand side of an assignment.
213
214 - The variable is passed to a function as a non-const pointer or non-const
215 lvalue reference. (It is assumed that the variable may be an out-parameter
216 for the function.)
217
218 - ``clear()`` or ``assign()`` is called on the variable and the variable is of
219 one of the standard container types ``basic_string``, ``vector``, ``deque``,
220 ``forward_list``, ``list``, ``set``, ``map``, ``multiset``, ``multimap``,
221 ``unordered_set``, ``unordered_map``, ``unordered_multiset``,
222 ``unordered_multimap``.
223
224 - ``reset()`` is called on the variable and the variable is of type
225 ``std::unique_ptr``, ``std::shared_ptr`` or ``std::weak_ptr``.
226
227 - A member function marked with the ``[[clang::reinitializes]]`` attribute is
228 called on the variable.
229
230 If the variable in question is a struct and an individual member variable of
231 that struct is written to, the check does not consider this to be a
232 reinitialization -- even if, eventually, all member variables of the struct are
233 written to. For example:
234
235 .. code-block:: c++
236
237 struct S {
238 std::string str;
239 int i;
240 };
241 S s = { "Hello, world!\n", 42 };
242 S s_other = std::move(s);
243 s.str = "Lorem ipsum";
244 s.i = 99;
245
246 The check will not consider ``s`` to be reinitialized after the last line;
247 instead, the line that assigns to ``s.str`` will be flagged as a use-after-move.
248 This is intentional as this pattern of reinitializing a struct is error-prone.
249 For example, if an additional member variable is added to ``S``, it is easy to
250 forget to add the reinitialization for this additional member. Instead, it is
251 safer to assign to the entire struct in one go, and this will also avoid the
252 use-after-move warning.