@@ -5,6 +5,8 @@ use clippy_utils::source::snippet_opt;
55use clippy_utils:: ty:: {
66 contains_ty, get_associated_type, get_iterator_item_ty, implements_trait, is_copy, peel_mid_ty_refs,
77} ;
8+ use clippy_utils:: { meets_msrv, msrvs} ;
9+
810use clippy_utils:: { fn_def_id, get_parent_expr, is_diag_item_method, is_diag_trait_item} ;
911use rustc_errors:: Applicability ;
1012use rustc_hir:: { def_id:: DefId , BorrowKind , Expr , ExprKind } ;
@@ -13,12 +15,19 @@ use rustc_middle::mir::Mutability;
1315use rustc_middle:: ty:: adjustment:: { Adjust , Adjustment , OverloadedDeref } ;
1416use rustc_middle:: ty:: subst:: { GenericArg , GenericArgKind , SubstsRef } ;
1517use rustc_middle:: ty:: { self , PredicateKind , ProjectionPredicate , TraitPredicate , Ty } ;
18+ use rustc_semver:: RustcVersion ;
1619use rustc_span:: { sym, Symbol } ;
1720use std:: cmp:: max;
1821
1922use super :: UNNECESSARY_TO_OWNED ;
2023
21- pub fn check < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' tcx > , method_name : Symbol , args : & ' tcx [ Expr < ' tcx > ] ) {
24+ pub fn check < ' tcx > (
25+ cx : & LateContext < ' tcx > ,
26+ expr : & ' tcx Expr < ' tcx > ,
27+ method_name : Symbol ,
28+ args : & ' tcx [ Expr < ' tcx > ] ,
29+ msrv : Option < & RustcVersion > ,
30+ ) {
2231 if_chain ! {
2332 if let Some ( method_def_id) = cx. typeck_results( ) . type_dependent_def_id( expr. hir_id) ;
2433 if let [ receiver] = args;
@@ -33,7 +42,7 @@ pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, method_name:
3342 if check_addr_of_expr( cx, expr, method_name, method_def_id, receiver) {
3443 return ;
3544 }
36- if check_into_iter_call_arg( cx, expr, method_name, receiver) {
45+ if check_into_iter_call_arg( cx, expr, method_name, receiver, msrv ) {
3746 return ;
3847 }
3948 check_other_call_arg( cx, expr, method_name, receiver) ;
@@ -178,7 +187,13 @@ fn check_addr_of_expr(
178187
179188/// Checks whether `expr` is an argument in an `into_iter` call and, if so, determines whether its
180189/// call of a `to_owned`-like function is unnecessary.
181- fn check_into_iter_call_arg ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , method_name : Symbol , receiver : & Expr < ' _ > ) -> bool {
190+ fn check_into_iter_call_arg (
191+ cx : & LateContext < ' _ > ,
192+ expr : & Expr < ' _ > ,
193+ method_name : Symbol ,
194+ receiver : & Expr < ' _ > ,
195+ msrv : Option < & RustcVersion > ,
196+ ) -> bool {
182197 if_chain ! {
183198 if let Some ( parent) = get_parent_expr( cx, expr) ;
184199 if let Some ( callee_def_id) = fn_def_id( cx, parent) ;
@@ -192,7 +207,7 @@ fn check_into_iter_call_arg(cx: &LateContext<'_>, expr: &Expr<'_>, method_name:
192207 if unnecessary_iter_cloned:: check_for_loop_iter( cx, parent, method_name, receiver, true ) {
193208 return true ;
194209 }
195- let cloned_or_copied = if is_copy( cx, item_ty) { "copied" } else { "cloned" } ;
210+ let cloned_or_copied = if is_copy( cx, item_ty) && meets_msrv ( msrv , & msrvs :: ITERATOR_COPIED ) { "copied" } else { "cloned" } ;
196211 // The next suggestion may be incorrect because the removal of the `to_owned`-like
197212 // function could cause the iterator to hold a reference to a resource that is used
198213 // mutably. See https://github.com/rust-lang/rust-clippy/issues/8148.
0 commit comments