Skip to content

Commit 4a324c9

Browse files
committed
Fix from_over_into lint suggesting invalid code
1 parent da910b1 commit 4a324c9

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

clippy_lints/src/from_over_into.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ fn convert_to_from(
176176
return None;
177177
};
178178
let body = cx.tcx.hir_body(body_id);
179-
let [input] = body.params else { return None };
180-
let PatKind::Binding(.., self_ident, None) = input.pat.kind else {
179+
let [self_param] = body.params else { return None };
180+
let PatKind::Binding(.., self_ident, None) = self_param.pat.kind else {
181181
return None;
182182
};
183183

@@ -197,11 +197,21 @@ fn convert_to_from(
197197
// fn into(self) -> T -> fn from(self) -> T
198198
// ~~~~ ~~~~
199199
(impl_item.ident.span, String::from("from")),
200-
// fn into([mut] self) -> T -> fn into([mut] v: T) -> T
201-
// ~~~~ ~~~~
202-
(self_ident.span, format!("val: {from}")),
203200
];
204201

202+
if self_ident.span.overlaps(self_param.ty_span) {
203+
// fn into([mut] self) -> T -> fn into([mut] val: T) -> T
204+
// ~~~~ ~~~~~~
205+
suggestions.push((self_ident.span, format!("val: {from}")));
206+
} else {
207+
// fn into([mut] self: U) -> T -> fn into([mut] val: U) -> T
208+
// ~~~~ ~~~
209+
suggestions.push((self_ident.span, String::from("val")));
210+
// fn into([mut] val: U) -> T -> fn into([mut] val: T) -> T
211+
// ~ ~
212+
suggestions.push((self_param.ty_span, from.to_owned()));
213+
}
214+
205215
if let FnRetTy::Return(_) = sig.decl.output {
206216
// fn into(self) -> T -> fn into(self) -> Self
207217
// ~ ~~~~

0 commit comments

Comments
 (0)