Skip to content

Commit 153b83f

Browse files
committed
[useless_asref]: check that the clone receiver is the local
1 parent e899684 commit 153b83f

File tree

4 files changed

+104
-4
lines changed

4 files changed

+104
-4
lines changed

clippy_lints/src/methods/useless_asref.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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, paths, peel_blocks};
4+
use clippy_utils::{get_parent_expr, is_diag_trait_item, match_def_path, path_to_local_id, paths, peel_blocks};
55
use rustc_errors::Applicability;
66
use rustc_hir as hir;
77
use rustc_lint::LateContext;
@@ -111,6 +111,23 @@ fn is_calling_clone(cx: &LateContext<'_>, arg: &hir::Expr<'_>) -> bool {
111111
hir::ExprKind::Closure(&hir::Closure { body, .. }) => {
112112
// If it's a closure, we need to check what is called.
113113
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+
114131
let closure_expr = peel_blocks(closure_body.value);
115132
match closure_expr.kind {
116133
hir::ExprKind::MethodCall(method, obj, [], _) => {
@@ -122,14 +139,17 @@ fn is_calling_clone(cx: &LateContext<'_>, arg: &hir::Expr<'_>) -> bool {
122139
// no autoderefs
123140
&& !cx.typeck_results().expr_adjustments(obj).iter()
124141
.any(|a| matches!(a.kind, Adjust::Deref(Some(..))))
142+
&& path_to_local_id(obj, *local_id)
125143
{
126144
true
127145
} else {
128146
false
129147
}
130148
},
131-
hir::ExprKind::Call(call, [_]) => {
132-
if let hir::ExprKind::Path(qpath) = call.kind {
149+
hir::ExprKind::Call(call, [recv]) => {
150+
if let hir::ExprKind::Path(qpath) = call.kind
151+
&& path_to_local_id(recv, *local_id)
152+
{
133153
check_qpath(cx, qpath, call.hir_id)
134154
} else {
135155
false

tests/ui/useless_asref.fixed

+31
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,37 @@ fn foo() {
144144
//~^ ERROR: this call to `as_ref.map(...)` does nothing
145145
}
146146

147+
mod issue12135 {
148+
pub struct Struct {
149+
field: Option<InnerStruct>,
150+
}
151+
152+
#[derive(Clone)]
153+
pub struct Foo;
154+
155+
#[derive(Clone)]
156+
struct InnerStruct {
157+
x: Foo,
158+
}
159+
160+
impl InnerStruct {
161+
fn method(&self) -> &Foo {
162+
&self.x
163+
}
164+
}
165+
166+
pub fn f(x: &Struct) -> Option<Foo> {
167+
x.field.clone();
168+
//~^ ERROR: this call to `as_ref.map(...)` does nothing
169+
x.field.clone();
170+
//~^ ERROR: this call to `as_ref.map(...)` does nothing
171+
x.field.clone();
172+
//~^ ERROR: this call to `as_ref.map(...)` does nothing
173+
174+
x.field.as_ref().map(|v| v.method().clone())
175+
}
176+
}
177+
147178
fn main() {
148179
not_ok();
149180
ok();

tests/ui/useless_asref.rs

+31
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,37 @@ fn foo() {
144144
//~^ ERROR: this call to `as_ref.map(...)` does nothing
145145
}
146146

147+
mod issue12135 {
148+
pub struct Struct {
149+
field: Option<InnerStruct>,
150+
}
151+
152+
#[derive(Clone)]
153+
pub struct Foo;
154+
155+
#[derive(Clone)]
156+
struct InnerStruct {
157+
x: Foo,
158+
}
159+
160+
impl InnerStruct {
161+
fn method(&self) -> &Foo {
162+
&self.x
163+
}
164+
}
165+
166+
pub fn f(x: &Struct) -> Option<Foo> {
167+
x.field.as_ref().map(|v| v.clone());
168+
//~^ ERROR: this call to `as_ref.map(...)` does nothing
169+
x.field.as_ref().map(Clone::clone);
170+
//~^ ERROR: this call to `as_ref.map(...)` does nothing
171+
x.field.as_ref().map(|v| Clone::clone(v));
172+
//~^ ERROR: this call to `as_ref.map(...)` does nothing
173+
174+
x.field.as_ref().map(|v| v.method().clone())
175+
}
176+
}
177+
147178
fn main() {
148179
not_ok();
149180
ok();

tests/ui/useless_asref.stderr

+19-1
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,23 @@ error: this call to `as_ref.map(...)` does nothing
8888
LL | let z = x.as_ref().map(|z| String::clone(z));
8989
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `x.clone()`
9090

91-
error: aborting due to 14 previous errors
91+
error: this call to `as_ref.map(...)` does nothing
92+
--> $DIR/useless_asref.rs:167:9
93+
|
94+
LL | x.field.as_ref().map(|v| v.clone());
95+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `x.field.clone()`
96+
97+
error: this call to `as_ref.map(...)` does nothing
98+
--> $DIR/useless_asref.rs:169:9
99+
|
100+
LL | x.field.as_ref().map(Clone::clone);
101+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `x.field.clone()`
102+
103+
error: this call to `as_ref.map(...)` does nothing
104+
--> $DIR/useless_asref.rs:171:9
105+
|
106+
LL | x.field.as_ref().map(|v| Clone::clone(v));
107+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `x.field.clone()`
108+
109+
error: aborting due to 17 previous errors
92110

0 commit comments

Comments
 (0)