Skip to content

Commit a78c104

Browse files
[clang-tidy]fix false positives of the result of std::move() is used as rvalue for performance-move-const-arg (#95633)
Fixes: #86404 --------- Co-authored-by: Danny Mösch <[email protected]>
1 parent 5e9fcb9 commit a78c104

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) {
4444
unless(isInTemplateInstantiation()))
4545
.bind("call-move");
4646

47-
Finder->addMatcher(MoveCallMatcher, this);
47+
Finder->addMatcher(
48+
expr(anyOf(
49+
castExpr(hasSourceExpression(MoveCallMatcher)),
50+
cxxConstructExpr(hasDeclaration(cxxConstructorDecl(anyOf(
51+
isCopyConstructor(), isMoveConstructor()))),
52+
hasArgument(0, MoveCallMatcher)))),
53+
this);
4854

4955
auto ConstTypeParmMatcher =
5056
qualType(references(isConstQualified())).bind("invocation-parm-type");

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,10 @@ Changes in existing checks
387387
- Improved :doc:`modernize-use-using <clang-tidy/checks/modernize/use-using>`
388388
check by adding support for detection of typedefs declared on function level.
389389

390+
- Improved :doc:`performance-move-const-arg
391+
<clang-tidy/checks/performance/move-const-arg>` check by ignoring
392+
``std::move()`` calls when their target is used as an rvalue.
393+
390394
- Improved :doc:`performance-unnecessary-copy-initialization
391395
<clang-tidy/checks/performance/unnecessary-copy-initialization>` check by
392396
detecting more cases of constant access. In particular, pointers can be

clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,18 @@ void f8() {
114114
int f9() { return M2(1); }
115115

116116
template <typename T>
117-
T f10(const int x10) {
117+
T f_unknown_target(const int x10) {
118118
return std::move(x10);
119-
// 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]
120-
// CHECK-FIXES: return x10;
121119
}
120+
122121
void f11() {
123-
f10<int>(1);
124-
f10<double>(1);
122+
f_unknown_target<int>(1);
123+
f_unknown_target<double>(1);
124+
}
125+
126+
A&& f_return_right_ref() {
127+
static A a{};
128+
return std::move(a);
125129
}
126130

127131
class NoMoveSemantics {

0 commit comments

Comments
 (0)