|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_sugg; |
2 | | -use clippy_utils::is_trait_method; |
3 | 2 | use clippy_utils::ty::{get_iterator_item_ty, is_copy}; |
| 3 | +use clippy_utils::{is_trait_method, meets_msrv}; |
4 | 4 | use rustc_errors::Applicability; |
5 | 5 | use rustc_hir::Expr; |
6 | 6 | use rustc_lint::LateContext; |
7 | 7 | use rustc_middle::ty; |
| 8 | +use rustc_semver::RustcVersion; |
8 | 9 | use rustc_span::{sym, Span}; |
9 | 10 |
|
10 | 11 | use super::CLONED_INSTEAD_OF_COPIED; |
11 | 12 |
|
12 | | -pub fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, span: Span) { |
| 13 | +const ITERATOR_COPIED_MSRV: RustcVersion = RustcVersion::new(1, 36, 0); |
| 14 | +const OPTION_COPIED_MSRV: RustcVersion = RustcVersion::new(1, 35, 0); |
| 15 | + |
| 16 | +pub fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, span: Span, msrv: Option<&RustcVersion>) { |
13 | 17 | let recv_ty = cx.typeck_results().expr_ty_adjusted(recv); |
14 | 18 | let inner_ty = match recv_ty.kind() { |
15 | 19 | // `Option<T>` -> `T` |
16 | | - ty::Adt(adt, subst) if cx.tcx.is_diagnostic_item(sym::option_type, adt.did) => subst.type_at(0), |
17 | | - _ if is_trait_method(cx, expr, sym::Iterator) => match get_iterator_item_ty(cx, recv_ty) { |
18 | | - // <T as Iterator>::Item |
19 | | - Some(ty) => ty, |
20 | | - _ => return, |
| 20 | + ty::Adt(adt, subst) |
| 21 | + if cx.tcx.is_diagnostic_item(sym::option_type, adt.did) && meets_msrv(msrv, &OPTION_COPIED_MSRV) => |
| 22 | + { |
| 23 | + subst.type_at(0) |
| 24 | + }, |
| 25 | + _ if is_trait_method(cx, expr, sym::Iterator) && meets_msrv(msrv, &ITERATOR_COPIED_MSRV) => { |
| 26 | + match get_iterator_item_ty(cx, recv_ty) { |
| 27 | + // <T as Iterator>::Item |
| 28 | + Some(ty) => ty, |
| 29 | + _ => return, |
| 30 | + } |
21 | 31 | }, |
22 | 32 | _ => return, |
23 | 33 | }; |
|
0 commit comments