Previously, the clang-tidy modernize-pass-by-value check would only fire
for arguments of constructors that are used exactly once and in a member
or base class initializers. This commit generalizes the check to general
functions and usages in the body of the function.
We now consider the control-flow graph of the function. If every
control-flow path must transition through one final usage of a given
parameter, we fire the diagnostic. We additionally require that the
variable is not referenced or pointed to by a local variable in the
function. There are still some false-positives, however, because we do
not try to figure out whether a pointer to the local variable escapes
through other means, e.g. here:
struct HasStrPtr {
const std::string* str;
};
void set_ptr(HasStrPtr& has_ptr, const std::string& str) {
has_ptr.str = &str;
}
// Take argument by value.
void foo(std::string);
void foo(const std::string& str) {
HasStrPtr obj;
set_ptr(obj, str);
foo(str);
foo(*HasStrPtr.str);
}The example looks at first sight like we can std::move in the call to
foo, but this would change the value pointed to by the pointer in
HasStrPtr, so that the semantics of the program change.
I think you need to add clangAnalysis (for Analysis/CFG.h) to the link libraries in modernize/CMakeLists.txt.