Skip to content

[clang-tidy]fix false positives of the result of std::move() is used as rvalue for performance-move-const-arg #95633

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

Merged
merged 2 commits into from
Jun 15, 2024
Merged
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 @@ -44,7 +44,13 @@ void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) {
unless(isInTemplateInstantiation()))
.bind("call-move");

Finder->addMatcher(MoveCallMatcher, this);
Finder->addMatcher(
expr(anyOf(
castExpr(hasSourceExpression(MoveCallMatcher)),
cxxConstructExpr(hasDeclaration(cxxConstructorDecl(anyOf(
isCopyConstructor(), isMoveConstructor()))),
hasArgument(0, MoveCallMatcher)))),
this);

auto ConstTypeParmMatcher =
qualType(references(isConstQualified())).bind("invocation-parm-type");
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ Changes in existing checks
- Improved :doc:`modernize-use-using <clang-tidy/checks/modernize/use-using>`
check by adding support for detection of typedefs declared on function level.

- Improved :doc:`performance-move-const-arg
<clang-tidy/checks/performance/move-const-arg>` check by ignoring
``std::move()`` calls when their target is used as an rvalue.

- Improved :doc:`performance-unnecessary-copy-initialization
<clang-tidy/checks/performance/unnecessary-copy-initialization>` check by
detecting more cases of constant access. In particular, pointers can be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,18 @@ void f8() {
int f9() { return M2(1); }

template <typename T>
T f10(const int x10) {
T f_unknown_target(const int x10) {
return std::move(x10);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the const variable 'x10' of the trivially-copyable type 'const int' has no effect; remove std::move() [performance-move-const-arg]
// CHECK-FIXES: return x10;
}

void f11() {
f10<int>(1);
f10<double>(1);
f_unknown_target<int>(1);
f_unknown_target<double>(1);
}

A&& f_return_right_ref() {
static A a{};
return std::move(a);
}

class NoMoveSemantics {
Expand Down
Loading