From 4a324c9dffa87a802a3e976976020ed605eeb1bc Mon Sep 17 00:00:00 2001 From: KonaeAkira Date: Sat, 15 Mar 2025 00:05:32 +0100 Subject: [PATCH] Fix from_over_into lint suggesting invalid code --- clippy_lints/src/from_over_into.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/clippy_lints/src/from_over_into.rs b/clippy_lints/src/from_over_into.rs index 6da5567d9c70..58a3f9288c0c 100644 --- a/clippy_lints/src/from_over_into.rs +++ b/clippy_lints/src/from_over_into.rs @@ -176,8 +176,8 @@ fn convert_to_from( return None; }; let body = cx.tcx.hir_body(body_id); - let [input] = body.params else { return None }; - let PatKind::Binding(.., self_ident, None) = input.pat.kind else { + let [self_param] = body.params else { return None }; + let PatKind::Binding(.., self_ident, None) = self_param.pat.kind else { return None; }; @@ -197,11 +197,21 @@ fn convert_to_from( // fn into(self) -> T -> fn from(self) -> T // ~~~~ ~~~~ (impl_item.ident.span, String::from("from")), - // fn into([mut] self) -> T -> fn into([mut] v: T) -> T - // ~~~~ ~~~~ - (self_ident.span, format!("val: {from}")), ]; + if self_ident.span.overlaps(self_param.ty_span) { + // fn into([mut] self) -> T -> fn into([mut] val: T) -> T + // ~~~~ ~~~~~~ + suggestions.push((self_ident.span, format!("val: {from}"))); + } else { + // fn into([mut] self: U) -> T -> fn into([mut] val: U) -> T + // ~~~~ ~~~ + suggestions.push((self_ident.span, String::from("val"))); + // fn into([mut] val: U) -> T -> fn into([mut] val: T) -> T + // ~ ~ + suggestions.push((self_param.ty_span, from.to_owned())); + } + if let FnRetTy::Return(_) = sig.decl.output { // fn into(self) -> T -> fn into(self) -> Self // ~ ~~~~