1 .. title:: clang-tidy - performance-no-automatic-move
3 performance-no-automatic-move
4 =============================
6 Finds local variables that cannot be automatically moved due to constness.
9 `certain conditions <https://en.cppreference.com/w/cpp/language/return#automatic_move_from_local_variables_and_parameters>`_,
10 local values are automatically moved out when returning from a function. A
11 common mistake is to declare local ``lvalue`` variables ``const``, which
14 Example `[1] <https://godbolt.org/z/x7SYYA>`_:
18 StatusOr<std::vector<int>> Cool() {
19 std::vector<int> obj = ...;
20 return obj; // calls StatusOr::StatusOr(std::vector<int>&&)
23 StatusOr<std::vector<int>> NotCool() {
24 const std::vector<int> obj = ...;
25 return obj; // calls `StatusOr::StatusOr(const std::vector<int>&)`
28 The former version (``Cool``) should be preferred over the latter (``Uncool``)
29 as it will avoid allocations and potentially large memory copies.
34 In the example above, ``StatusOr::StatusOr(T&&)`` have the same semantics as
35 long as the copy and move constructors for ``T`` have the same semantics. Note
36 that there is no guarantee that ``S::S(T&&)`` and ``S::S(const T&)`` have the
37 same semantics for any single ``S``, so we're not providing automated fixes for
38 this check, and judgement should be exerted when making the suggested changes.
43 Another case where the move cannot happen is the following:
47 StatusOr<std::vector<int>> Uncool() {
48 std::vector<int>&& obj = ...;
49 return obj; // calls `StatusOr::StatusOr(const std::vector<int>&)`
52 In that case the fix is more consensual: just `return std::move(obj)`.
53 This is handled by the `-Wreturn-std-move` warning.