[clang-tools-extra] [clang-tidy] Fix performance-move-const-arg false negative in ternary… (PR #128402)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Feb 22 21:42:37 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tools-extra
Author: David Rivera (RiverDave)
<details>
<summary>Changes</summary>
This PR aims to fix `performance-move-const-arg` #<!-- -->126515
## Changes
Enhanced the `performance-move-arg` check in Clang-Tidy to detect cases where std::move is used
in **ternary expressions which was not being detected before**
## Testing
- A new mock class has been where the changes have been tested & all tests pass
---
Full diff: https://github.com/llvm/llvm-project/pull/128402.diff
3 Files Affected:
- (modified) clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp (+11-2)
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
- (modified) clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp (+23)
``````````diff
diff --git a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp
index 421ce003975bc..3de41e707cfd7 100644
--- a/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp
+++ b/clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp
@@ -44,6 +44,12 @@ void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) {
unless(isInTemplateInstantiation()))
.bind("call-move");
+ // Match ternary expressions where either branch contains std::move
+ auto TernaryWithMoveMatcher =
+ conditionalOperator(
+ hasDescendant(MoveCallMatcher)
+ ).bind("ternary-move");
+
Finder->addMatcher(
expr(anyOf(
castExpr(hasSourceExpression(MoveCallMatcher)),
@@ -58,13 +64,16 @@ void MoveConstArgCheck::registerMatchers(MatchFinder *Finder) {
qualType(rValueReferenceType()).bind("invocation-parm-type");
// Matches respective ParmVarDecl for a CallExpr or CXXConstructExpr.
auto ArgumentWithParamMatcher = forEachArgumentWithParam(
- MoveCallMatcher, parmVarDecl(anyOf(hasType(ConstTypeParmMatcher),
+ anyOf(MoveCallMatcher, TernaryWithMoveMatcher),
+ parmVarDecl(anyOf(hasType(ConstTypeParmMatcher),
hasType(RValueTypeParmMatcher)))
.bind("invocation-parm"));
// Matches respective types of arguments for a CallExpr or CXXConstructExpr
// and it works on calls through function pointers as well.
auto ArgumentWithParamTypeMatcher = forEachArgumentWithParamType(
- MoveCallMatcher, anyOf(ConstTypeParmMatcher, RValueTypeParmMatcher));
+ anyOf(MoveCallMatcher, TernaryWithMoveMatcher),
+ anyOf(ConstTypeParmMatcher, RValueTypeParmMatcher));
+
Finder->addMatcher(
invocation(anyOf(ArgumentWithParamMatcher, ArgumentWithParamTypeMatcher))
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 41ff1c1016f25..2eb65d61f5e78 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -115,6 +115,10 @@ Changes in existing checks
<clang-tidy/checks/misc/redundant-expression>` check by providing additional
examples and fixing some macro related false positives.
+- Improved :doc:`performance-move-const-arg
+ <clang-tidy/checks/performance/move-const-arg>` check by fixing false negatives
+ on ternary operators calling ``std::move``.
+
Removed checks
^^^^^^^^^^^^^^
diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp
index 8e325b0ae6ca3..e616cbe78bc3a 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp
@@ -560,3 +560,26 @@ struct Result {
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg]
};
} // namespace GH111450
+
+namespace GH126515 {
+
+struct TernaryMoveCall {
+TernaryMoveCall();
+TernaryMoveCall(const TernaryMoveCall&);
+TernaryMoveCall operator=(const TernaryMoveCall&);
+
+void TernaryCheckTriviallyCopyable(const char * c) {}
+
+void testTernaryMove() {
+ TernaryMoveCall t1;
+ TernaryMoveCall other(false ? TernaryMoveCall() : TernaryMoveCall(std::move(t1)) );
+ // CHECK-MESSAGES: :[[@LINE-1]]:69: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg]
+ // CHECK-MESSAGES: :[[@LINE-11]]:8: note: 'TernaryMoveCall' is not move assignable/constructible
+
+ const char* a = "a";
+ TernaryCheckTriviallyCopyable(true ? std::move(a) : "" );
+ // CHECK-MESSAGES: :[[@LINE-1]]:40: warning: std::move of the variable 'a' of the trivially-copyable type 'const char *' has no effect; remove std::move() [performance-move-const-arg]
+}
+
+};
+} // namespace GH126515
``````````
</details>
https://github.com/llvm/llvm-project/pull/128402
More information about the cfe-commits
mailing list