Skip to content

Commit 4523954

Browse files
committed
Move MapClone into Methods lint pass
1 parent 5bc8813 commit 4523954

File tree

7 files changed

+159
-173
lines changed

7 files changed

+159
-173
lines changed

clippy_lints/src/lib.register_all.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
127127
LintId::of(manual_rem_euclid::MANUAL_REM_EUCLID),
128128
LintId::of(manual_retain::MANUAL_RETAIN),
129129
LintId::of(manual_strip::MANUAL_STRIP),
130-
LintId::of(map_clone::MAP_CLONE),
131130
LintId::of(map_unit_fn::OPTION_MAP_UNIT_FN),
132131
LintId::of(map_unit_fn::RESULT_MAP_UNIT_FN),
133132
LintId::of(match_result_ok::MATCH_RESULT_OK),
@@ -179,6 +178,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
179178
LintId::of(methods::MANUAL_SATURATING_ARITHMETIC),
180179
LintId::of(methods::MANUAL_SPLIT_ONCE),
181180
LintId::of(methods::MANUAL_STR_REPEAT),
181+
LintId::of(methods::MAP_CLONE),
182182
LintId::of(methods::MAP_COLLECT_RESULT_UNIT),
183183
LintId::of(methods::MAP_FLATTEN),
184184
LintId::of(methods::MAP_IDENTITY),

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ store.register_lints(&[
246246
manual_rem_euclid::MANUAL_REM_EUCLID,
247247
manual_retain::MANUAL_RETAIN,
248248
manual_strip::MANUAL_STRIP,
249-
map_clone::MAP_CLONE,
250249
map_err_ignore::MAP_ERR_IGNORE,
251250
map_unit_fn::OPTION_MAP_UNIT_FN,
252251
map_unit_fn::RESULT_MAP_UNIT_FN,
@@ -325,6 +324,7 @@ store.register_lints(&[
325324
methods::MANUAL_SATURATING_ARITHMETIC,
326325
methods::MANUAL_SPLIT_ONCE,
327326
methods::MANUAL_STR_REPEAT,
327+
methods::MAP_CLONE,
328328
methods::MAP_COLLECT_RESULT_UNIT,
329329
methods::MAP_FLATTEN,
330330
methods::MAP_IDENTITY,

clippy_lints/src/lib.register_style.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ store.register_group(true, "clippy::style", Some("clippy_style"), vec![
4545
LintId::of(manual_bits::MANUAL_BITS),
4646
LintId::of(manual_empty_string_creations::MANUAL_EMPTY_STRING_CREATIONS),
4747
LintId::of(manual_non_exhaustive::MANUAL_NON_EXHAUSTIVE),
48-
LintId::of(map_clone::MAP_CLONE),
4948
LintId::of(match_result_ok::MATCH_RESULT_OK),
5049
LintId::of(matches::COLLAPSIBLE_MATCH),
5150
LintId::of(matches::INFALLIBLE_DESTRUCTURING_MATCH),
@@ -69,6 +68,7 @@ store.register_group(true, "clippy::style", Some("clippy_style"), vec![
6968
LintId::of(methods::ITER_NTH_ZERO),
7069
LintId::of(methods::ITER_SKIP_NEXT),
7170
LintId::of(methods::MANUAL_SATURATING_ARITHMETIC),
71+
LintId::of(methods::MAP_CLONE),
7272
LintId::of(methods::MAP_COLLECT_RESULT_UNIT),
7373
LintId::of(methods::NEW_RET_NO_SELF),
7474
LintId::of(methods::OBFUSCATED_IF_ELSE),

clippy_lints/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ mod manual_non_exhaustive;
273273
mod manual_rem_euclid;
274274
mod manual_retain;
275275
mod manual_strip;
276-
mod map_clone;
277276
mod map_err_ignore;
278277
mod map_unit_fn;
279278
mod match_result_ok;
@@ -628,8 +627,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
628627
store.register_late_pass(move || Box::new(needless_question_mark::NeedlessQuestionMark));
629628
store.register_late_pass(move || Box::new(casts::Casts::new(msrv)));
630629
store.register_early_pass(move || Box::new(unnested_or_patterns::UnnestedOrPatterns::new(msrv)));
631-
store.register_late_pass(move || Box::new(map_clone::MapClone::new(msrv)));
632-
633630
store.register_late_pass(|| Box::new(size_of_in_element_count::SizeOfInElementCount));
634631
store.register_late_pass(|| Box::new(same_name_method::SameNameMethod));
635632
let max_suggested_slice_pattern_length = conf.max_suggested_slice_pattern_length;

clippy_lints/src/map_clone.rs

Lines changed: 0 additions & 167 deletions
This file was deleted.

clippy_lints/src/methods/map_clone.rs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::source::snippet_with_applicability;
3+
use clippy_utils::ty::{is_copy, is_type_diagnostic_item};
4+
use clippy_utils::{is_diag_trait_item, meets_msrv, msrvs, peel_blocks};
5+
use if_chain::if_chain;
6+
use rustc_errors::Applicability;
7+
use rustc_hir as hir;
8+
use rustc_lint::LateContext;
9+
use rustc_middle::mir::Mutability;
10+
use rustc_middle::ty;
11+
use rustc_middle::ty::adjustment::Adjust;
12+
use rustc_semver::RustcVersion;
13+
use rustc_span::symbol::Ident;
14+
use rustc_span::{sym, Span};
15+
16+
use super::MAP_CLONE;
17+
18+
pub(super) fn check<'tcx>(
19+
cx: &LateContext<'_>,
20+
e: &hir::Expr<'_>,
21+
recv: &hir::Expr<'_>,
22+
arg: &'tcx hir::Expr<'_>,
23+
msrv: Option<RustcVersion>,
24+
) {
25+
if_chain! {
26+
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(e.hir_id);
27+
if cx.tcx.impl_of_method(method_id)
28+
.map_or(false, |id| is_type_diagnostic_item(cx, cx.tcx.type_of(id), sym::Option))
29+
|| is_diag_trait_item(cx, method_id, sym::Iterator);
30+
if let hir::ExprKind::Closure(&hir::Closure{ body, .. }) = arg.kind;
31+
then {
32+
let closure_body = cx.tcx.hir().body(body);
33+
let closure_expr = peel_blocks(&closure_body.value);
34+
match closure_body.params[0].pat.kind {
35+
hir::PatKind::Ref(inner, hir::Mutability::Not) => if let hir::PatKind::Binding(
36+
hir::BindingAnnotation::Unannotated, .., name, None
37+
) = inner.kind {
38+
if ident_eq(name, closure_expr) {
39+
lint_explicit_closure(cx, e.span, recv.span, true, msrv);
40+
}
41+
},
42+
hir::PatKind::Binding(hir::BindingAnnotation::Unannotated, .., name, None) => {
43+
match closure_expr.kind {
44+
hir::ExprKind::Unary(hir::UnOp::Deref, inner) => {
45+
if ident_eq(name, inner) {
46+
if let ty::Ref(.., Mutability::Not) = cx.typeck_results().expr_ty(inner).kind() {
47+
lint_explicit_closure(cx, e.span, recv.span, true, msrv);
48+
}
49+
}
50+
},
51+
hir::ExprKind::MethodCall(method, [obj], _) => if_chain! {
52+
if ident_eq(name, obj) && method.ident.name == sym::clone;
53+
if let Some(fn_id) = cx.typeck_results().type_dependent_def_id(closure_expr.hir_id);
54+
if let Some(trait_id) = cx.tcx.trait_of_item(fn_id);
55+
if cx.tcx.lang_items().clone_trait().map_or(false, |id| id == trait_id);
56+
// no autoderefs
57+
if !cx.typeck_results().expr_adjustments(obj).iter()
58+
.any(|a| matches!(a.kind, Adjust::Deref(Some(..))));
59+
then {
60+
let obj_ty = cx.typeck_results().expr_ty(obj);
61+
if let ty::Ref(_, ty, mutability) = obj_ty.kind() {
62+
if matches!(mutability, Mutability::Not) {
63+
let copy = is_copy(cx, *ty);
64+
lint_explicit_closure(cx, e.span, recv.span, copy, msrv);
65+
}
66+
} else {
67+
lint_needless_cloning(cx, e.span, recv.span);
68+
}
69+
}
70+
},
71+
_ => {},
72+
}
73+
},
74+
_ => {},
75+
}
76+
}
77+
}
78+
}
79+
80+
fn ident_eq(name: Ident, path: &hir::Expr<'_>) -> bool {
81+
if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = path.kind {
82+
path.segments.len() == 1 && path.segments[0].ident == name
83+
} else {
84+
false
85+
}
86+
}
87+
88+
fn lint_needless_cloning(cx: &LateContext<'_>, root: Span, receiver: Span) {
89+
span_lint_and_sugg(
90+
cx,
91+
MAP_CLONE,
92+
root.trim_start(receiver).unwrap(),
93+
"you are needlessly cloning iterator elements",
94+
"remove the `map` call",
95+
String::new(),
96+
Applicability::MachineApplicable,
97+
);
98+
}
99+
100+
fn lint_explicit_closure(cx: &LateContext<'_>, replace: Span, root: Span, is_copy: bool, msrv: Option<RustcVersion>) {
101+
let mut applicability = Applicability::MachineApplicable;
102+
103+
let (message, sugg_method) = if is_copy && meets_msrv(msrv, msrvs::ITERATOR_COPIED) {
104+
("you are using an explicit closure for copying elements", "copied")
105+
} else {
106+
("you are using an explicit closure for cloning elements", "cloned")
107+
};
108+
109+
span_lint_and_sugg(
110+
cx,
111+
MAP_CLONE,
112+
replace,
113+
message,
114+
&format!("consider calling the dedicated `{}` method", sugg_method),
115+
format!(
116+
"{}.{}()",
117+
snippet_with_applicability(cx, root, "..", &mut applicability),
118+
sugg_method,
119+
),
120+
applicability,
121+
);
122+
}

0 commit comments

Comments
 (0)