Skip to content

Commit c4e2d36

Browse files
committed
Auto merge of #7129 - camsteffen:copied-msrv, r=Manishearth
cloned_instead_of_copied MSRV changelog: none (since the lint is still new) Fixes #7127 r? `@Manishearth`
2 parents 08e36d7 + 3f5be5e commit c4e2d36

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

clippy_lints/src/methods/cloned_instead_of_copied.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::is_trait_method;
32
use clippy_utils::ty::{get_iterator_item_ty, is_copy};
3+
use clippy_utils::{is_trait_method, meets_msrv};
44
use rustc_errors::Applicability;
55
use rustc_hir::Expr;
66
use rustc_lint::LateContext;
77
use rustc_middle::ty;
8+
use rustc_semver::RustcVersion;
89
use rustc_span::{sym, Span};
910

1011
use super::CLONED_INSTEAD_OF_COPIED;
1112

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>) {
1317
let recv_ty = cx.typeck_results().expr_ty_adjusted(recv);
1418
let inner_ty = match recv_ty.kind() {
1519
// `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+
}
2131
},
2232
_ => return,
2333
};

clippy_lints/src/methods/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1959,7 +1959,7 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
19591959
("as_mut", []) => useless_asref::check(cx, expr, "as_mut", recv),
19601960
("as_ref", []) => useless_asref::check(cx, expr, "as_ref", recv),
19611961
("assume_init", []) => uninit_assumed_init::check(cx, expr, recv),
1962-
("cloned", []) => cloned_instead_of_copied::check(cx, expr, recv, span),
1962+
("cloned", []) => cloned_instead_of_copied::check(cx, expr, recv, span, msrv),
19631963
("collect", []) => match method_call!(recv) {
19641964
Some(("cloned", [recv2], _)) => iter_cloned_collect::check(cx, expr, recv2),
19651965
Some(("map", [m_recv, m_arg], _)) => {

0 commit comments

Comments
 (0)