Commit Graph

9 Commits

Author SHA1 Message Date
JJ Marr
7ddd4ff02e Allow partial moving
The official C++ core guidelines policy is you must move rvalues.

This is inflexible in practice in the Wesnoth codebase.
2025-01-25 08:58:45 -05:00
JJ Marr
0b46bd3044 Enable performance-no-automatic-move
Variables can be automatically moved when a function returns. However,
declaring a variable as const prevents that move from occuring (as the
move modifies the moved-from variable).
2025-01-25 08:58:45 -05:00
JJ Marr
15acd0c00a Lint for performance-move-constructor-init
This checks when a move constructor for an object copy constructs its
member classes or parent classes. Copying unnecessary as all of the
members of the moved-from object can be clobbered by the move.
2025-01-25 08:58:45 -05:00
JJ Marr
259160e1e4 Lint for performance-move-const-arg
These are cases in which something is std::moved and no move actually
occurs.
2025-01-25 08:58:45 -05:00
JJ Marr
c34d689726 Warn against not moving rvalue parameter
If a function takes in an rvalue, it's taking ownership of the object
that's being moved into it. If the object never moves that rvalue param,
then there was no point to moving it into the function.

As an example of a problem this catches, in context_manager.cpp,
replace_map_context_with took in an rvalue unique_ptr "mc" to a map
context. The function then swapped it with a given value in
map_contexts_. This swap was unnecessary because "mc" expired after the
function call was complete. It is more optimal to std::move(mc) into
the value in map_context_ that we were previously swapping with.
2025-01-25 08:58:45 -05:00
JJ Marr
fc1ac27b48 Enable bugprone-move-forwarding reference
A forwarding reference is one of the form (note the template parameter):

    template<class X>
    void foo( X&& x );

The difference between this and a regular rvalue reference (where X is
not a template) is that foo() also accept lvalues. So it is possible to
call:

    std::string s = "Example";
    foo(s)

And if the body of foo(X&& x) std::moves x, then s will be invalidated.

This check warns on usage of std::move in this context and recommends
std::forward, which automatically determines whether to std::move
depending on whether foo() is called with an rvalue or an lvalue.
2025-01-25 08:58:45 -05:00
JJ Marr
af82672ca9 Add bugprone-use-after-move
It's generally bad practice to use something after it's been std::moved
from.
2025-01-25 08:58:45 -05:00
JJ Marr
7cbe381faa Enable performance-unnecessary-value-param 2024-12-01 23:19:53 -05:00
JJ Marr
d8d92b5d28 Add clang-tidy support to cmake
Also, enables it in CI.
2024-11-30 13:24:45 +01:00