Skip to content

Commit be5707c

Browse files
committed
lint on .map(|&x| x.clone())
1 parent 153b83f commit be5707c

File tree

4 files changed

+27
-23
lines changed

4 files changed

+27
-23
lines changed

clippy_lints/src/methods/useless_asref.rs

+10-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_applicability;
33
use clippy_utils::ty::walk_ptrs_ty_depth;
4-
use clippy_utils::{get_parent_expr, is_diag_trait_item, match_def_path, path_to_local_id, paths, peel_blocks};
4+
use clippy_utils::{
5+
get_parent_expr, is_diag_trait_item, match_def_path, path_to_local_id, paths, peel_blocks, strip_pat_refs,
6+
};
57
use rustc_errors::Applicability;
68
use rustc_hir as hir;
79
use rustc_lint::LateContext;
@@ -108,26 +110,12 @@ fn check_qpath(cx: &LateContext<'_>, qpath: hir::QPath<'_>, hir_id: hir::HirId)
108110

109111
fn is_calling_clone(cx: &LateContext<'_>, arg: &hir::Expr<'_>) -> bool {
110112
match arg.kind {
111-
hir::ExprKind::Closure(&hir::Closure { body, .. }) => {
113+
hir::ExprKind::Closure(&hir::Closure { body, .. })
112114
// If it's a closure, we need to check what is called.
113-
let closure_body = cx.tcx.hir().body(body);
114-
115-
// |x| ...
116-
// ^
117-
let [
118-
hir::Param {
119-
pat:
120-
hir::Pat {
121-
kind: hir::PatKind::Binding(_, local_id, ..),
122-
..
123-
},
124-
..
125-
},
126-
] = closure_body.params
127-
else {
128-
return false;
129-
};
130-
115+
if let closure_body = cx.tcx.hir().body(body)
116+
&& let [param] = closure_body.params
117+
&& let hir::PatKind::Binding(_, local_id, ..) = strip_pat_refs(param.pat).kind =>
118+
{
131119
let closure_expr = peel_blocks(closure_body.value);
132120
match closure_expr.kind {
133121
hir::ExprKind::MethodCall(method, obj, [], _) => {
@@ -139,7 +127,7 @@ fn is_calling_clone(cx: &LateContext<'_>, arg: &hir::Expr<'_>) -> bool {
139127
// no autoderefs
140128
&& !cx.typeck_results().expr_adjustments(obj).iter()
141129
.any(|a| matches!(a.kind, Adjust::Deref(Some(..))))
142-
&& path_to_local_id(obj, *local_id)
130+
&& path_to_local_id(obj, local_id)
143131
{
144132
true
145133
} else {
@@ -148,7 +136,7 @@ fn is_calling_clone(cx: &LateContext<'_>, arg: &hir::Expr<'_>) -> bool {
148136
},
149137
hir::ExprKind::Call(call, [recv]) => {
150138
if let hir::ExprKind::Path(qpath) = call.kind
151-
&& path_to_local_id(recv, *local_id)
139+
&& path_to_local_id(recv, local_id)
152140
{
153141
check_qpath(cx, qpath, call.hir_id)
154142
} else {

tests/ui/useless_asref.fixed

+5
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ mod issue12135 {
171171
x.field.clone();
172172
//~^ ERROR: this call to `as_ref.map(...)` does nothing
173173

174+
// https://github.com/rust-lang/rust-clippy/pull/12136#discussion_r1451565223
175+
#[allow(clippy::clone_on_copy)]
176+
Some(1).clone();
177+
//~^ ERROR: this call to `as_ref.map(...)` does nothing
178+
174179
x.field.as_ref().map(|v| v.method().clone())
175180
}
176181
}

tests/ui/useless_asref.rs

+5
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ mod issue12135 {
171171
x.field.as_ref().map(|v| Clone::clone(v));
172172
//~^ ERROR: this call to `as_ref.map(...)` does nothing
173173

174+
// https://github.com/rust-lang/rust-clippy/pull/12136#discussion_r1451565223
175+
#[allow(clippy::clone_on_copy)]
176+
Some(1).as_ref().map(|&x| x.clone());
177+
//~^ ERROR: this call to `as_ref.map(...)` does nothing
178+
174179
x.field.as_ref().map(|v| v.method().clone())
175180
}
176181
}

tests/ui/useless_asref.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,11 @@ error: this call to `as_ref.map(...)` does nothing
106106
LL | x.field.as_ref().map(|v| Clone::clone(v));
107107
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `x.field.clone()`
108108

109-
error: aborting due to 17 previous errors
109+
error: this call to `as_ref.map(...)` does nothing
110+
--> $DIR/useless_asref.rs:176:9
111+
|
112+
LL | Some(1).as_ref().map(|&x| x.clone());
113+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(1).clone()`
114+
115+
error: aborting due to 18 previous errors
110116

0 commit comments

Comments
 (0)