void UnnecessaryValueParamCheck::check(const MatchFinder::MatchResult &Result) { const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param"); const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("functionDecl"); const size_t Index = std::find(Function->parameters().begin(), Function->parameters().end(), Param) - Function->parameters().begin(); bool IsConstQualified = Param->getType().getCanonicalType().isConstQualified(); // Skip declarations delayed by late template parsing without a body. if (!Function->getBody()) return; // Do not trigger on non-const value parameters when: // 1. they are in a constructor definition since they can likely trigger // misc-move-constructor-init which will suggest to move the argument. // 2. they are not only used as const. if (!IsConstQualified && (llvm::isa<CXXConstructorDecl>(Function) || !Function->doesThisDeclarationHaveABody() || !decl_ref_expr_utils::isOnlyUsedAsConst( *Param, *Function->getBody(), *Result.Context))) return; auto Diag = diag(Param->getLocation(), IsConstQualified ? "the const qualified parameter %0 is " "copied for each invocation; consider " "making it a reference" : "the parameter %0 is copied for each " "invocation but only used as a const reference; " "consider making it a const reference") << paramNameOrIndex(Param->getName(), Index); // Do not propose fixes in macros since we cannot place them correctly. if (Param->getLocStart().isMacroID()) return; for (const auto *FunctionDecl = Function; FunctionDecl != nullptr; FunctionDecl = FunctionDecl->getPreviousDecl()) { const auto &CurrentParam = *FunctionDecl->getParamDecl(Index); Diag << utils::create_fix_it::changeVarDeclToReference(CurrentParam, *Result.Context); if (!IsConstQualified) Diag << utils::create_fix_it::changeVarDeclToConst(CurrentParam); } }
void UnnecessaryValueParamCheck::check(const MatchFinder::MatchResult &Result) { const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param"); const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("functionDecl"); const size_t Index = std::find(Function->parameters().begin(), Function->parameters().end(), Param) - Function->parameters().begin(); bool IsConstQualified = Param->getType().getCanonicalType().isConstQualified(); // Skip declarations delayed by late template parsing without a body. if (!Function->getBody()) return; // Do not trigger on non-const value parameters when: // 1. they are in a constructor definition since they can likely trigger // misc-move-constructor-init which will suggest to move the argument. if (!IsConstQualified && (llvm::isa<CXXConstructorDecl>(Function) || !Function->doesThisDeclarationHaveABody())) return; auto AllDeclRefExprs = utils::decl_ref_expr::allDeclRefExprs( *Param, *Function->getBody(), *Result.Context); auto ConstDeclRefExprs = utils::decl_ref_expr::constReferenceDeclRefExprs( *Param, *Function->getBody(), *Result.Context); // 2. they are not only used as const. if (!isSubset(AllDeclRefExprs, ConstDeclRefExprs)) return; // If the parameter is non-const, check if it has a move constructor and is // only referenced once to copy-construct another object or whether it has a // move assignment operator and is only referenced once when copy-assigned. // In this case wrap DeclRefExpr with std::move() to avoid the unnecessary // copy. if (!IsConstQualified) { auto CanonicalType = Param->getType().getCanonicalType(); if (AllDeclRefExprs.size() == 1 && ((utils::type_traits::hasNonTrivialMoveConstructor(CanonicalType) && utils::decl_ref_expr::isCopyConstructorArgument( **AllDeclRefExprs.begin(), *Function->getBody(), *Result.Context)) || (utils::type_traits::hasNonTrivialMoveAssignment(CanonicalType) && utils::decl_ref_expr::isCopyAssignmentArgument( **AllDeclRefExprs.begin(), *Function->getBody(), *Result.Context)))) { handleMoveFix(*Param, **AllDeclRefExprs.begin(), *Result.Context); return; } } auto Diag = diag(Param->getLocation(), IsConstQualified ? "the const qualified parameter %0 is " "copied for each invocation; consider " "making it a reference" : "the parameter %0 is copied for each " "invocation but only used as a const reference; " "consider making it a const reference") << paramNameOrIndex(Param->getName(), Index); // Do not propose fixes when: // 1. the ParmVarDecl is in a macro, since we cannot place them correctly // 2. the function is virtual as it might break overrides // 3. the function is referenced outside of a call expression within the // compilation unit as the signature change could introduce build errors. const auto *Method = llvm::dyn_cast<CXXMethodDecl>(Function); if (Param->getLocStart().isMacroID() || (Method && Method->isVirtual()) || isReferencedOutsideOfCallExpr(*Function, *Result.Context)) return; for (const auto *FunctionDecl = Function; FunctionDecl != nullptr; FunctionDecl = FunctionDecl->getPreviousDecl()) { const auto &CurrentParam = *FunctionDecl->getParamDecl(Index); Diag << utils::fixit::changeVarDeclToReference(CurrentParam, *Result.Context); // The parameter of each declaration needs to be checked individually as to // whether it is const or not as constness can differ between definition and // declaration. if (!CurrentParam.getType().getCanonicalType().isConstQualified()) Diag << utils::fixit::changeVarDeclToConst(CurrentParam); } }