11#include "clang/Lex/Lexer.h"
18 const SourceManager &SM,
19 const LangOptions &LangOpts) {
20 const Expr *Arg = Call->getArg(0);
22 CharSourceRange BeforeArgumentsRange = Lexer::makeFileCharRange(
23 CharSourceRange::getCharRange(Call->getBeginLoc(), Arg->getBeginLoc()),
25 CharSourceRange AfterArgumentsRange = Lexer::makeFileCharRange(
26 CharSourceRange::getCharRange(Call->getEndLoc(),
27 Call->getEndLoc().getLocWithOffset(1)),
30 if (BeforeArgumentsRange.isValid() && AfterArgumentsRange.isValid()) {
31 Diag << FixItHint::CreateRemoval(BeforeArgumentsRange)
32 << FixItHint::CreateRemoval(AfterArgumentsRange);
37 Options.
store(Opts,
"CheckTriviallyCopyableMove", CheckTriviallyCopyableMove);
38 Options.
store(Opts,
"CheckMoveToConstRef", CheckMoveToConstRef);
42 auto MoveCallMatcher =
43 callExpr(callee(functionDecl(hasName(
"::std::move"))), argumentCountIs(1),
44 unless(isInTemplateInstantiation()))
47 Finder->addMatcher(MoveCallMatcher,
this);
49 auto ConstTypeParmMatcher =
50 qualType(references(isConstQualified())).bind(
"invocation-parm-type");
51 auto RValueTypeParmMatcher =
52 qualType(rValueReferenceType()).bind(
"invocation-parm-type");
54 auto ArgumentWithParamMatcher = forEachArgumentWithParam(
55 MoveCallMatcher, parmVarDecl(anyOf(hasType(ConstTypeParmMatcher),
56 hasType(RValueTypeParmMatcher)))
57 .bind(
"invocation-parm"));
60 auto ArgumentWithParamTypeMatcher = forEachArgumentWithParamType(
61 MoveCallMatcher, anyOf(ConstTypeParmMatcher, RValueTypeParmMatcher));
64 invocation(anyOf(ArgumentWithParamMatcher, ArgumentWithParamTypeMatcher))
65 .bind(
"receiving-expr"),
70 const QualType *InvocationParmType,
72 if (Invocation && (*InvocationParmType)->isRValueReferenceType() &&
74 if (!Invocation->getType()->isRecordType())
76 if (
const auto *ConstructCallExpr =
77 dyn_cast<CXXConstructExpr>(Invocation)) {
78 if (
const auto *ConstructorDecl = ConstructCallExpr->getConstructor()) {
79 if (!ConstructorDecl->isCopyOrMoveConstructor() &&
80 !ConstructorDecl->isDefaultConstructor())
89 const auto *CallMove = Result.Nodes.getNodeAs<CallExpr>(
"call-move");
90 const auto *ReceivingExpr = Result.Nodes.getNodeAs<Expr>(
"receiving-expr");
91 const auto *InvocationParm =
92 Result.Nodes.getNodeAs<ParmVarDecl>(
"invocation-parm");
93 const auto *InvocationParmType =
94 Result.Nodes.getNodeAs<QualType>(
"invocation-parm-type");
97 if (!ReceivingExpr && AlreadyCheckedMoves.contains(CallMove))
101 AlreadyCheckedMoves.insert(CallMove);
103 const Expr *Arg = CallMove->getArg(0);
104 const QualType ArgType = Arg->getType().getCanonicalType();
105 SourceManager &SM = Result.Context->getSourceManager();
107 CharSourceRange MoveRange =
108 CharSourceRange::getCharRange(CallMove->getSourceRange());
109 CharSourceRange FileMoveRange =
110 Lexer::makeFileCharRange(MoveRange, SM,
getLangOpts());
111 if (!FileMoveRange.isValid())
114 bool IsConstArg = ArgType.isConstQualified();
115 bool IsTriviallyCopyable = ArgType.isTriviallyCopyableType(*Result.Context);
117 if (IsConstArg || IsTriviallyCopyable) {
118 if (
const CXXRecordDecl *R = ArgType->getAsCXXRecordDecl()) {
125 for (
const auto *Ctor : R->ctors()) {
126 if (Ctor->isCopyConstructor() && Ctor->isDeleted())
131 if (!IsConstArg && IsTriviallyCopyable && !CheckTriviallyCopyableMove)
134 bool IsVariable = isa<DeclRefExpr>(Arg);
140 IsVariable ? dyn_cast<DeclRefExpr>(Arg)->getDecl() :
nullptr;
143 auto Diag =
diag(FileMoveRange.getBegin(),
144 "std::move of the %select{|const }0"
145 "%select{expression|variable %5}1 "
146 "%select{|of the trivially-copyable type %6 }2"
147 "has no effect%select{; remove std::move()|}3"
148 "%select{| or make the variable non-const}4")
149 << IsConstArg << IsVariable << IsTriviallyCopyable
151 << (IsConstArg && IsVariable && !IsTriviallyCopyable) << Var
158 const auto *ReceivingCallExpr = dyn_cast<CallExpr>(ReceivingExpr);
159 const auto *ReceivingConstructExpr =
160 dyn_cast<CXXConstructExpr>(ReceivingExpr);
162 if ((!ReceivingCallExpr || !ReceivingCallExpr->getDirectCallee() ||
163 ReceivingCallExpr->getDirectCallee()->isTemplateInstantiation()) &&
164 (!ReceivingConstructExpr ||
165 !ReceivingConstructExpr->getConstructor() ||
166 ReceivingConstructExpr->getConstructor()->isTemplateInstantiation()))
169 const NamedDecl *FunctionName =
nullptr;
172 ? ReceivingCallExpr->getDirectCallee()->getUnderlyingDecl()
173 : ReceivingConstructExpr->getConstructor()->getUnderlyingDecl();
175 QualType NoRefType = (*InvocationParmType)->getPointeeType();
176 PrintingPolicy PolicyWithSuppressedTag(
getLangOpts());
177 PolicyWithSuppressedTag.SuppressTagKeyword =
true;
178 PolicyWithSuppressedTag.SuppressUnwrittenScope =
true;
179 std::string ExpectParmTypeName =
180 NoRefType.getAsString(PolicyWithSuppressedTag);
181 if (!NoRefType->isPointerType()) {
182 NoRefType.addConst();
184 NoRefType.getAsString(PolicyWithSuppressedTag) +
" &";
187 diag(InvocationParm->getLocation(),
188 "consider changing the %ordinal0 parameter of %1 from %2 to '%3'",
190 << (InvocationParm->getFunctionScopeIndex() + 1) << FunctionName
191 << *InvocationParmType << ExpectParmTypeName;
193 }
else if (ReceivingExpr && CheckMoveToConstRef) {
194 if ((*InvocationParmType)->isRValueReferenceType())
198 auto Diag =
diag(FileMoveRange.getBegin(),
199 "passing result of std::move() as a const reference "
200 "argument; no move will actually happen");
205 if (
const CXXRecordDecl *RecordDecl = ArgType->getAsCXXRecordDecl();
206 RecordDecl && !(RecordDecl->hasMoveConstructor() &&
207 RecordDecl->hasMoveAssignment())) {
208 const bool MissingMoveAssignment = !RecordDecl->hasMoveAssignment();
209 const bool MissingMoveConstructor = !RecordDecl->hasMoveConstructor();
210 const bool MissingBoth = MissingMoveAssignment && MissingMoveConstructor;
212 diag(RecordDecl->getLocation(),
214 "%select{|assignable}1%select{|/}2%select{|constructible}3",
216 << RecordDecl << MissingMoveAssignment << MissingBoth
217 << MissingMoveConstructor;
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
llvm::StringMap< ClangTidyValue > OptionMap