Skip to content

[clang-tidy] unnecessary-value-param: Allow moving of value arguments. #145871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ bool hasLoopStmtAncestor(const DeclRefExpr &DeclRef, const Decl &Decl,
return Matches.empty();
}

bool isArgOfStdMove(const DeclRefExpr &DeclRef, const Decl &Decl,
ASTContext &Context) {
return !match(traverse(TK_AsIs,
decl(hasDescendant(callExpr(
callee(functionDecl(hasName("std::move"))),
hasArgument(0, ignoringParenImpCasts(declRefExpr(
equalsNode(&DeclRef)))))))),
Decl, Context)
.empty();
}

} // namespace

UnnecessaryValueParamCheck::UnnecessaryValueParamCheck(
Expand Down Expand Up @@ -100,6 +111,11 @@ void UnnecessaryValueParamCheck::check(const MatchFinder::MatchResult &Result) {
auto CanonicalType = Param->getType().getCanonicalType();
const auto &DeclRefExpr = **AllDeclRefExprs.begin();

// The reference is in a call to `std::move`, do not warn.
if (isArgOfStdMove(DeclRefExpr, *Function, *Result.Context)) {
return;
}

if (!hasLoopStmtAncestor(DeclRefExpr, *Function, *Result.Context) &&
((utils::type_traits::hasNonTrivialMoveConstructor(CanonicalType) &&
utils::decl_ref_expr::isCopyConstructorArgument(
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ Changes in existing checks
to avoid matching usage of functions within the current compilation unit.
Added an option `IgnoreCoroutines` with the default value `true` to
suppress this check for coroutines where passing by reference may be unsafe.
Fix false positive on by-value parameters that are only moved.

- Improved :doc:`readability-convert-member-functions-to-static
<clang-tidy/checks/readability/convert-member-functions-to-static>` check by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ void ReferenceFunctionByCallingIt() {
PositiveMessageAndFixAsFunctionIsCalled(ExpensiveToCopyType());
}

void NegativeMoved(ExpensiveToCopyType A) {
ExpensiveToCopyType Copy = std::move(A);
}

// Virtual method overrides of dependent types cannot be recognized unless they
// are marked as override or final. Test that check is not triggered on methods
// marked with override or final.
Expand Down