Index: clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.h =================================================================== --- clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.h +++ clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.h @@ -10,6 +10,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MOVECONSTANTARGUMENTCHECK_H #include "../ClangTidyCheck.h" +#include "llvm/ADT/DenseSet.h" namespace clang { namespace tidy { @@ -36,6 +37,7 @@ private: const bool CheckTriviallyCopyableMove; + llvm::DenseSet AlreadyCheckedMoves; }; } // namespace performance Index: clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp +++ clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp @@ -50,7 +50,9 @@ Finder->addMatcher( invocation(forEachArgumentWithParam( MoveCallMatcher, - parmVarDecl(hasType(references(isConstQualified()))))) + parmVarDecl(anyOf(hasType(references(isConstQualified())), + hasType(rValueReferenceType()))) + .bind("invocation-parm"))) .bind("receiving-expr"), this); } @@ -58,6 +60,15 @@ void MoveConstArgCheck::check(const MatchFinder::MatchResult &Result) { const auto *CallMove = Result.Nodes.getNodeAs("call-move"); const auto *ReceivingExpr = Result.Nodes.getNodeAs("receiving-expr"); + const auto *InvocationParm = + Result.Nodes.getNodeAs("invocation-parm"); + + if (!ReceivingExpr && AlreadyCheckedMoves.contains(CallMove)) + return; + + if (ReceivingExpr) + AlreadyCheckedMoves.insert(CallMove); + const Expr *Arg = CallMove->getArg(0); SourceManager &SM = Result.Context->getSourceManager(); @@ -90,20 +101,78 @@ return; bool IsVariable = isa(Arg); + bool IsRValueReferenceArg = false; const auto *Var = IsVariable ? dyn_cast(Arg)->getDecl() : nullptr; - auto Diag = diag(FileMoveRange.getBegin(), - "std::move of the %select{|const }0" - "%select{expression|variable %4}1 " - "%select{|of the trivially-copyable type %5 }2" - "has no effect; remove std::move()" - "%select{| or make the variable non-const}3") - << IsConstArg << IsVariable << IsTriviallyCopyable - << (IsConstArg && IsVariable && !IsTriviallyCopyable) << Var - << Arg->getType(); - replaceCallWithArg(CallMove, Diag, SM, getLangOpts()); + if (ReceivingExpr && + InvocationParm->getOriginalType()->isRValueReferenceType() && + Arg->isLValue()) { + if (ReceivingExpr->getType()->isRecordType()) { + const auto *ConstructCallExpr = + dyn_cast(ReceivingExpr); + if (!ConstructCallExpr) + return; + const auto *ConstructorDecl = ConstructCallExpr->getConstructor(); + if (!ConstructorDecl) + return; + if (!ConstructorDecl->isCopyOrMoveConstructor() && + !ConstructorDecl->isDefaultConstructor()) + IsRValueReferenceArg = true; + } else + IsRValueReferenceArg = true; + } + + { + auto Diag = diag(FileMoveRange.getBegin(), + "std::move of the %select{|const }0" + "%select{expression|variable %5}1 " + "%select{|of the trivially-copyable type %6 }2" + "has no effect%select{; remove std::move()|}3" + "%select{| or make the variable non-const}4") + << IsConstArg << IsVariable << IsTriviallyCopyable + << IsRValueReferenceArg + << (IsConstArg && IsVariable && !IsTriviallyCopyable) << Var + << Arg->getType(); + if (!IsRValueReferenceArg) + replaceCallWithArg(CallMove, Diag, SM, getLangOpts()); + } + if (IsRValueReferenceArg) { + const auto *ReceivingCallExpr = dyn_cast(ReceivingExpr); + const auto *ReceivingConstructExpr = + dyn_cast(ReceivingExpr); + if ((!ReceivingCallExpr || + ReceivingCallExpr->getDirectCallee()->isTemplateInstantiation()) && + (!ReceivingConstructExpr || + ReceivingConstructExpr->getConstructor()->isTemplateInstantiation())) + return; + StringRef FunctionName; + if (ReceivingCallExpr) + FunctionName = ReceivingCallExpr->getDirectCallee()->getName(); + else + FunctionName = StringRef( + ReceivingConstructExpr->getConstructor()->getNameAsString()); + auto NoRefType = InvocationParm->getType()->getPointeeType(); + PrintingPolicy PolicyWithSupressedTag(getLangOpts()); + PolicyWithSupressedTag.SuppressTagKeyword = true; + PolicyWithSupressedTag.SuppressUnwrittenScope = true; + std::string ExpectParmTypeName = + NoRefType.getAsString(PolicyWithSupressedTag); + if (!NoRefType->isPointerType()) { + NoRefType.addConst(); + ExpectParmTypeName = + NoRefType.getAsString(PolicyWithSupressedTag) + " &"; + } + diag(InvocationParm->getLocation(), + "consider changing the %ordinal0 parameter of %1 from %2 to '%3'", + DiagnosticIDs::Note) + << (InvocationParm->getFunctionScopeIndex() + 1) << FunctionName + << InvocationParm->getOriginalType() << ExpectParmTypeName; + } } else if (ReceivingExpr) { + if (InvocationParm->getOriginalType()->isRValueReferenceType()) + return; + auto Diag = diag(FileMoveRange.getBegin(), "passing result of std::move() as a const reference " "argument; no move will actually happen"); Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -113,6 +113,10 @@ Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ +- Improved :doc:`performance-move-const-arg` check. + + Removed wrong FixIt for trivially-copyable objects wrapped by ``std::move()`` and passed to an rvalue reference parameter. Removal of ``std::move()`` would break the code. + Removed checks ^^^^^^^^^^^^^^ Index: clang-tools-extra/test/clang-tidy/checkers/performance-move-const-arg.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/performance-move-const-arg.cpp +++ clang-tools-extra/test/clang-tidy/checkers/performance-move-const-arg.cpp @@ -246,3 +246,87 @@ }; f(MoveSemantics()); } + +void showInt(int &&v); +void showInt(int v1, int &&v2); +void showPointer(const char *&&s); +void showPointer2(const char *const &&s); +void showTriviallyCopyable(TriviallyCopyable &&obj); +void showTriviallyCopyablePointer(const TriviallyCopyable *&&obj); +void testFunctions() { + int a = 10; + showInt(std::move(a)); + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: std::move of the variable 'a' of the trivially-copyable type 'int' has no effect [performance-move-const-arg] + // CHECK-MESSAGES: :[[@LINE-10]]:20: note: consider changing the 1st parameter of showInt from 'int &&' to 'const int &' + showInt(int()); + showInt(a, std::move(a)); + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: std::move of the variable 'a' of the trivially-copyable type 'int' has no effect [performance-move-const-arg] + // CHECK-MESSAGES: :[[@LINE-13]]:28: note: consider changing the 2nd parameter of showInt from 'int &&' to 'const int &' + const char* s = ""; + showPointer(std::move(s)); + // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: std::move of the variable 's' of the trivially-copyable type 'const char *' has no effect [performance-move-const-arg] + // CHECK-MESSAGES: :[[@LINE-16]]:32: note: consider changing the 1st parameter of showPointer from 'const char *&&' to 'const char *' + showPointer2(std::move(s)); + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: std::move of the variable 's' of the trivially-copyable type 'const char *' has no effect [performance-move-const-arg] + // CHECK-MESSAGES: :[[@LINE-18]]:39: note: consider changing the 1st parameter of showPointer2 from 'const char *const &&' to 'const char *const' + TriviallyCopyable *obj = new TriviallyCopyable(); + showTriviallyCopyable(std::move(*obj)); + // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: std::move of the expression of the trivially-copyable type 'TriviallyCopyable' has no effect [performance-move-const-arg] + // CHECK-MESSAGES: :[[@LINE-21]]:48: note: consider changing the 1st parameter of showTriviallyCopyable from 'TriviallyCopyable &&' to 'const TriviallyCopyable &' + showTriviallyCopyablePointer(std::move(obj)); + // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: std::move of the variable 'obj' of the trivially-copyable type 'TriviallyCopyable *' has no effect [performance-move-const-arg] + // CHECK-MESSAGES: :[[@LINE-23]]:62: note: consider changing the 1st parameter of showTriviallyCopyablePointer from 'const TriviallyCopyable *&&' to 'const TriviallyCopyable *' +} +template +void forwardToShowInt(T && t) { + showInt(static_cast(t)); +} +void testTemplate() { + int a = 10; + forwardToShowInt(std::move(a)); + // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: std::move of the variable 'a' of the trivially-copyable type 'int' has no effect [performance-move-const-arg] +} + +struct Tmp { + Tmp(); + Tmp(int &&a); + Tmp(int v1, int &&a); + Tmp(const char *&&s); + Tmp(TriviallyCopyable&& obj); + Tmp(const TriviallyCopyable *&&obj); + void showTmp(TriviallyCopyable&& t); + static void showTmpStatic(TriviallyCopyable&& t); +}; +void testMethods() { + Tmp t; + int a = 10; + Tmp t1(std::move(a)); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the variable 'a' of the trivially-copyable type 'int' has no effect [performance-move-const-arg] + // CHECK-MESSAGES: :[[@LINE-13]]:13: note: consider changing the 1st parameter of Tmp from 'int &&' to 'const int &' + Tmp t2(a, std::move(a)); + // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: std::move of the variable 'a' of the trivially-copyable type 'int' has no effect [performance-move-const-arg] + // CHECK-MESSAGES: :[[@LINE-15]]:21: note: consider changing the 2nd parameter of Tmp from 'int &&' to 'const int &' + const char* s = ""; + Tmp t3(std::move(s)); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the variable 's' of the trivially-copyable type 'const char *' has no effect [performance-move-const-arg] + // CHECK-MESSAGES: :[[@LINE-18]]:21: note: consider changing the 1st parameter of Tmp from 'const char *&&' to 'const char *' + TriviallyCopyable *obj = new TriviallyCopyable(); + Tmp t4(std::move(*obj)); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the expression of the trivially-copyable type 'TriviallyCopyable' has no effect [performance-move-const-arg] + // CHECK-MESSAGES: :[[@LINE-21]]:27: note: consider changing the 1st parameter of Tmp from 'TriviallyCopyable &&' to 'const TriviallyCopyable &' + Tmp t5(std::move(obj)); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the variable 'obj' of the trivially-copyable type 'TriviallyCopyable *' has no effect [performance-move-const-arg] + // CHECK-MESSAGES: :[[@LINE-23]]:34: note: consider changing the 1st parameter of Tmp from 'const TriviallyCopyable *&&' to 'const TriviallyCopyable *' + t.showTmp(std::move(*obj)); + // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: std::move of the expression of the trivially-copyable type 'TriviallyCopyable' has no effect [performance-move-const-arg] + // CHECK-MESSAGES: :[[@LINE-25]]:36: note: consider changing the 1st parameter of showTmp from 'TriviallyCopyable &&' to 'const TriviallyCopyable &' + Tmp::showTmpStatic(std::move(*obj)); + // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: std::move of the expression of the trivially-copyable type 'TriviallyCopyable' has no effect [performance-move-const-arg] + // CHECK-MESSAGES: :[[@LINE-27]]:49: note: consider changing the 1st parameter of showTmpStatic from 'TriviallyCopyable &&' to 'const TriviallyCopyable &' +} + +void showA(A && v) {} +void testA() { + A a; + showA(std::move(a)); +}