diff --git a/clang-tools-extra/clang-tidy/misc/MoveConstantArgumentCheck.cpp b/clang-tools-extra/clang-tidy/misc/MoveConstantArgumentCheck.cpp index 058ad2b8b0b2..c8ef94278e31 100644 --- a/clang-tools-extra/clang-tidy/misc/MoveConstantArgumentCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/MoveConstantArgumentCheck.cpp @@ -1,101 +1,107 @@ //===--- MoveConstantArgumentCheck.cpp - clang-tidy -----------------------===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #include "MoveConstantArgumentCheck.h" #include "clang/Lex/Lexer.h" using namespace clang::ast_matchers; namespace clang { namespace tidy { namespace misc { static void ReplaceCallWithArg(const CallExpr *Call, DiagnosticBuilder &Diag, const SourceManager &SM, const LangOptions &LangOpts) { const Expr *Arg = Call->getArg(0); CharSourceRange BeforeArgumentsRange = Lexer::makeFileCharRange( CharSourceRange::getCharRange(Call->getLocStart(), Arg->getLocStart()), SM, LangOpts); CharSourceRange AfterArgumentsRange = Lexer::makeFileCharRange( CharSourceRange::getCharRange(Call->getLocEnd(), Call->getLocEnd().getLocWithOffset(1)), SM, LangOpts); if (BeforeArgumentsRange.isValid() && AfterArgumentsRange.isValid()) { Diag << FixItHint::CreateRemoval(BeforeArgumentsRange) << FixItHint::CreateRemoval(AfterArgumentsRange); } } void MoveConstantArgumentCheck::registerMatchers(MatchFinder *Finder) { if (!getLangOpts().CPlusPlus) return; auto MoveCallMatcher = callExpr(callee(functionDecl(hasName("::std::move"))), argumentCountIs(1), unless(isInTemplateInstantiation())) .bind("call-move"); Finder->addMatcher(MoveCallMatcher, this); auto ConstParamMatcher = forEachArgumentWithParam( MoveCallMatcher, parmVarDecl(hasType(references(isConstQualified())))); Finder->addMatcher(callExpr(ConstParamMatcher).bind("receiving-expr"), this); Finder->addMatcher(cxxConstructExpr(ConstParamMatcher).bind("receiving-expr"), this); } void MoveConstantArgumentCheck::check(const MatchFinder::MatchResult &Result) { const auto *CallMove = Result.Nodes.getNodeAs("call-move"); const auto *ReceivingExpr = Result.Nodes.getNodeAs("receiving-expr"); const Expr *Arg = CallMove->getArg(0); SourceManager &SM = Result.Context->getSourceManager(); CharSourceRange MoveRange = CharSourceRange::getCharRange(CallMove->getSourceRange()); CharSourceRange FileMoveRange = Lexer::makeFileCharRange(MoveRange, SM, getLangOpts()); if (!FileMoveRange.isValid()) return; bool IsConstArg = Arg->getType().isConstQualified(); bool IsTriviallyCopyable = Arg->getType().isTriviallyCopyableType(*Result.Context); if (IsConstArg || IsTriviallyCopyable) { + if (const CXXRecordDecl *R = Arg->getType()->getAsCXXRecordDecl()) { + for (const auto *Ctor : R->ctors()) { + if (Ctor->isCopyConstructor() && Ctor->isDeleted()) + return; + } + } bool IsVariable = isa(Arg); 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()); } else if (ReceivingExpr) { auto Diag = diag(FileMoveRange.getBegin(), "passing result of std::move() as a const reference " "argument; no move will actually happen"); ReplaceCallWithArg(CallMove, Diag, SM, getLangOpts()); } } } // namespace misc } // namespace tidy } // namespace clang diff --git a/clang-tools-extra/test/clang-tidy/misc-move-const-arg.cpp b/clang-tools-extra/test/clang-tidy/misc-move-const-arg.cpp index 2db682015c17..096f2f9725c0 100644 --- a/clang-tools-extra/test/clang-tidy/misc-move-const-arg.cpp +++ b/clang-tools-extra/test/clang-tidy/misc-move-const-arg.cpp @@ -1,160 +1,173 @@ // RUN: %check_clang_tidy %s misc-move-const-arg %t namespace std { template struct remove_reference; template struct remove_reference { typedef _Tp type; }; template struct remove_reference<_Tp &> { typedef _Tp type; }; template struct remove_reference<_Tp &&> { typedef _Tp type; }; template constexpr typename std::remove_reference<_Tp>::type &&move(_Tp &&__t); } // namespace std class A { public: A() {} A(const A &rhs) {} A(A &&rhs) {} }; int f1() { return std::move(42); // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the expression of the trivially-copyable type 'int' has no effect; remove std::move() [misc-move-const-arg] // CHECK-FIXES: return 42; } int f2(int x2) { return std::move(x2); // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the variable 'x2' of the trivially-copyable type 'int' // CHECK-FIXES: return x2; } int *f3(int *x3) { return std::move(x3); // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the variable 'x3' of the trivially-copyable type 'int *' // CHECK-FIXES: return x3; } A f4(A x4) { return std::move(x4); } A f5(const A x5) { return std::move(x5); // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the const variable 'x5' has no effect; remove std::move() or make the variable non-const [misc-move-const-arg] // CHECK-FIXES: return x5; } template T f6(const T x6) { return std::move(x6); } void f7() { int a = f6(10); } #define M1(x) x void f8() { const A a; M1(A b = std::move(a);) // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: std::move of the const variable 'a' has no effect; remove std::move() or make the variable non-const // CHECK-FIXES: M1(A b = a;) } #define M2(x) std::move(x) int f9() { return M2(1); } template T f10(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() [misc-move-const-arg] // CHECK-FIXES: return x10; } void f11() { f10(1); f10(1); } class NoMoveSemantics { public: NoMoveSemantics(); NoMoveSemantics(const NoMoveSemantics &); NoMoveSemantics &operator=(const NoMoveSemantics &); }; void callByConstRef(const NoMoveSemantics &); void callByConstRef(int i, const NoMoveSemantics &); void moveToConstReferencePositives() { NoMoveSemantics obj; // Basic case. callByConstRef(std::move(obj)); // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: passing result of std::move() as // CHECK-FIXES: callByConstRef(obj); // Also works for second argument. callByConstRef(1, std::move(obj)); // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: passing result of std::move() as // CHECK-FIXES: callByConstRef(1, obj); // Works if std::move() applied to a temporary. callByConstRef(std::move(NoMoveSemantics())); // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: passing result of std::move() as // CHECK-FIXES: callByConstRef(NoMoveSemantics()); // Works if calling a copy constructor. NoMoveSemantics other(std::move(obj)); // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: passing result of std::move() as // CHECK-FIXES: NoMoveSemantics other(obj); // Works if calling assignment operator. other = std::move(obj); // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: passing result of std::move() as // CHECK-FIXES: other = obj; } class MoveSemantics { public: MoveSemantics(); MoveSemantics(MoveSemantics &&); MoveSemantics &operator=(MoveSemantics &&); }; void callByValue(MoveSemantics); void callByRValueRef(MoveSemantics &&); template void templateFunction(T obj) { T other = std::move(obj); } #define M3(T, obj) \ do { \ T other = std::move(obj); \ } while (true) #define CALL(func) (func)() void moveToConstReferenceNegatives() { // No warning when actual move takes place. MoveSemantics move_semantics; callByValue(std::move(move_semantics)); callByRValueRef(std::move(move_semantics)); MoveSemantics other(std::move(move_semantics)); other = std::move(move_semantics); // No warning if std::move() not used. NoMoveSemantics no_move_semantics; callByConstRef(no_move_semantics); // No warning if instantiating a template. templateFunction(no_move_semantics); // No warning inside of macro expansions. M3(NoMoveSemantics, no_move_semantics); // No warning inside of macro expansion, even if the macro expansion is inside // a lambda that is, in turn, an argument to a macro. CALL([no_move_semantics] { M3(NoMoveSemantics, no_move_semantics); }); } + +class MoveOnly { +public: + MoveOnly(const MoveOnly &other) = delete; + MoveOnly &operator=(const MoveOnly &other) = delete; + MoveOnly(MoveOnly &&other) = default; + MoveOnly &operator=(MoveOnly &&other) = default; +}; +template +void Q(T); +void moveOnlyNegatives(MoveOnly val) { + Q(std::move(val)); +}