diff options
Diffstat (limited to 'clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp')
-rw-r--r-- | clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp b/clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp index e0040094259..67a62022766 100644 --- a/clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/ForRangeCopyCheck.cpp @@ -13,6 +13,7 @@ #include "../utils/OptionsUtils.h" #include "../utils/TypeTraits.h" #include "clang/Analysis/Analyses/ExprMutationAnalyzer.h" +#include "clang/Basic/Diagnostic.h" using namespace clang::ast_matchers; @@ -77,8 +78,11 @@ bool ForRangeCopyCheck::handleConstValueCopy(const VarDecl &LoopVar, "the loop variable's type is not a reference type; this creates a " "copy in each iteration; consider making this a reference") << utils::fixit::changeVarDeclToReference(LoopVar, Context); - if (!LoopVar.getType().isConstQualified()) - Diagnostic << utils::fixit::changeVarDeclToConst(LoopVar); + if (!LoopVar.getType().isConstQualified()) { + if (llvm::Optional<FixItHint> Fix = utils::fixit::addQualifierToVarDecl( + LoopVar, Context, DeclSpec::TQ::TQ_const)) + Diagnostic << *Fix; + } return true; } @@ -101,11 +105,15 @@ bool ForRangeCopyCheck::handleCopyIsOnlyConstReferenced( !utils::decl_ref_expr::allDeclRefExprs(LoopVar, *ForRange.getBody(), Context) .empty()) { - diag(LoopVar.getLocation(), - "loop variable is copied but only used as const reference; consider " - "making it a const reference") - << utils::fixit::changeVarDeclToConst(LoopVar) - << utils::fixit::changeVarDeclToReference(LoopVar, Context); + auto Diag = diag( + LoopVar.getLocation(), + "loop variable is copied but only used as const reference; consider " + "making it a const reference"); + + if (llvm::Optional<FixItHint> Fix = utils::fixit::addQualifierToVarDecl( + LoopVar, Context, DeclSpec::TQ::TQ_const)) + Diag << *Fix << utils::fixit::changeVarDeclToReference(LoopVar, Context); + return true; } return false; |