Index: clang-tidy/performance/ForRangeCopyCheck.cpp =================================================================== --- clang-tidy/performance/ForRangeCopyCheck.cpp +++ clang-tidy/performance/ForRangeCopyCheck.cpp @@ -135,7 +135,9 @@ } else if (!LoopVar.getType().isConstQualified()) { return false; } - if (!type_traits::isExpensiveToCopy(LoopVar.getType(), Context)) + llvm::Optional Expensive = + type_traits::isExpensiveToCopy(LoopVar.getType(), Context); + if (!Expensive || !*Expensive) return false; auto Diagnostic = diag(LoopVar.getLocation(), @@ -150,8 +152,9 @@ bool ForRangeCopyCheck::handleCopyIsOnlyConstReferenced( const VarDecl &LoopVar, const CXXForRangeStmt &ForRange, ASTContext &Context) { - if (LoopVar.getType().isConstQualified() || - !type_traits::isExpensiveToCopy(LoopVar.getType(), Context)) { + llvm::Optional Expensive = + type_traits::isExpensiveToCopy(LoopVar.getType(), Context); + if (LoopVar.getType().isConstQualified() || !Expensive || !*Expensive) { return false; } // Collect all DeclRefExprs to the loop variable and all CallExprs and Index: test/clang-tidy/performance-for-range-copy.cpp =================================================================== --- test/clang-tidy/performance-for-range-copy.cpp +++ test/clang-tidy/performance-for-range-copy.cpp @@ -127,6 +127,7 @@ } void use(const Mutable &M); +void use(int I); void useTwice(const Mutable &M1, const Mutable &M2); void useByValue(Mutable M); void useByConstValue(const Mutable M); @@ -170,6 +171,30 @@ } } +void negativeConstCheapToCopy() { + for (const int I : View>()) { + } +} + +void negativeConstCheapToCopyTypedef() { + typedef const int ConstInt; + for (ConstInt C : View>()) { + } +} + +void negativeCheapToCopy() { + for (int I : View>()) { + use(I); + } +} + +void negativeCheapToCopyTypedef() { + typedef int Int; + for (Int I : View>()) { + use(I); + } +} + void positiveOnlyConstMethodInvoked() { for (auto M : View>()) { // CHECK-MESSAGES: [[@LINE-1]]:13: warning: loop variable is copied but only used as const reference; consider making it a const reference [performance-for-range-copy]