Warning C26479
Don't use std::move to return a local variable. (f.48)
Remarks
The return
statement is the last use of a local variable, so the compiler uses move semantics to return it whenever possible.
Adding a std::move
is redundant in this scenario. Moreover, redundant std::move
s can prevent copy elision.
Code analysis name: NO_MOVE_RET_ON_LOCALS
Example 1
S foo()
{
S local1{};
return std::move(local1); // Warning: C26479
}
To fix this issue, remove the redundant std::move
:
S foo()
{
S local1{};
return local1; // No warning
}
See also
F.48 - Don't return std::move(local)
ES.56 - Write std::move()
only when you need to explicitly move an object to another scope