Index: clang-tidy/performance/ForRangeCopyCheck.cpp =================================================================== --- clang-tidy/performance/ForRangeCopyCheck.cpp +++ clang-tidy/performance/ForRangeCopyCheck.cpp @@ -8,7 +8,7 @@ //===----------------------------------------------------------------------===// #include "ForRangeCopyCheck.h" -#include "../utils/DeclRefExprUtils.h" +#include "../utils/ExprMutationAnalyzer.h" #include "../utils/FixItHintUtils.h" #include "../utils/TypeTraits.h" @@ -79,8 +79,8 @@ utils::type_traits::isExpensiveToCopy(LoopVar.getType(), Context); if (LoopVar.getType().isConstQualified() || !Expensive || !*Expensive) return false; - if (!utils::decl_ref_expr::isOnlyUsedAsConst(LoopVar, *ForRange.getBody(), - Context)) + if (utils::ExprMutationAnalyzer(ForRange.getBody(), &Context) + .isMutated(&LoopVar)) return false; diag(LoopVar.getLocation(), "loop variable is copied but only used as const reference; consider " 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 @@ -117,6 +117,11 @@ ~Mutable() {} }; +struct Point { + ~Point() {} + int x, y; +}; + Mutable& operator<<(Mutable &Out, bool B) { Out.setBool(B); return Out; @@ -214,6 +219,15 @@ } } +void positiveOnlyAccessedFieldAsConst() { + for (auto UsedAsConst : 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] + // CHECK-FIXES: for (const auto& UsedAsConst : View>()) { + use(UsedAsConst.x); + use(UsedAsConst.y); + } +} + void positiveOnlyUsedInCopyConstructor() { for (auto A : 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]