@@ -5,6 +5,8 @@ use clippy_utils::source::snippet_opt;
5
5
use clippy_utils:: ty:: {
6
6
contains_ty, get_associated_type, get_iterator_item_ty, implements_trait, is_copy, peel_mid_ty_refs,
7
7
} ;
8
+ use clippy_utils:: { meets_msrv, msrvs} ;
9
+
8
10
use clippy_utils:: { fn_def_id, get_parent_expr, is_diag_item_method, is_diag_trait_item} ;
9
11
use rustc_errors:: Applicability ;
10
12
use rustc_hir:: { def_id:: DefId , BorrowKind , Expr , ExprKind } ;
@@ -13,12 +15,19 @@ use rustc_middle::mir::Mutability;
13
15
use rustc_middle:: ty:: adjustment:: { Adjust , Adjustment , OverloadedDeref } ;
14
16
use rustc_middle:: ty:: subst:: { GenericArg , GenericArgKind , SubstsRef } ;
15
17
use rustc_middle:: ty:: { self , PredicateKind , ProjectionPredicate , TraitPredicate , Ty } ;
18
+ use rustc_semver:: RustcVersion ;
16
19
use rustc_span:: { sym, Symbol } ;
17
20
use std:: cmp:: max;
18
21
19
22
use super :: UNNECESSARY_TO_OWNED ;
20
23
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
+ ) {
22
31
if_chain ! {
23
32
if let Some ( method_def_id) = cx. typeck_results( ) . type_dependent_def_id( expr. hir_id) ;
24
33
if let [ receiver] = args;
@@ -33,7 +42,7 @@ pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, method_name:
33
42
if check_addr_of_expr( cx, expr, method_name, method_def_id, receiver) {
34
43
return ;
35
44
}
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 ) {
37
46
return ;
38
47
}
39
48
check_other_call_arg( cx, expr, method_name, receiver) ;
@@ -178,7 +187,13 @@ fn check_addr_of_expr(
178
187
179
188
/// Checks whether `expr` is an argument in an `into_iter` call and, if so, determines whether its
180
189
/// 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 {
182
197
if_chain ! {
183
198
if let Some ( parent) = get_parent_expr( cx, expr) ;
184
199
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:
192
207
if unnecessary_iter_cloned:: check_for_loop_iter( cx, parent, method_name, receiver, true ) {
193
208
return true ;
194
209
}
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" } ;
196
211
// The next suggestion may be incorrect because the removal of the `to_owned`-like
197
212
// function could cause the iterator to hold a reference to a resource that is used
198
213
// mutably. See https://github.com/rust-lang/rust-clippy/issues/8148.
0 commit comments