Skip to content

Commit 5c031d1

Browse files
authored
Fix from_over_into lint suggesting invalid code (#14409)
fixes [#112502](rust-lang/rust#112502) changelog: [`from_over_into`]: fix invalid code suggestion when self parameter has type annotation
2 parents f85331f + 9c897a2 commit 5c031d1

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

clippy_lints/src/from_over_into.rs

Lines changed: 7 additions & 7 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

@@ -194,12 +194,12 @@ fn convert_to_from(
194194
// impl Into<T> for U -> impl Into<T> for T
195195
// ~ ~
196196
(self_ty.span, into.to_owned()),
197-
// fn into(self) -> T -> fn from(self) -> T
198-
// ~~~~ ~~~~
197+
// fn into(self: U) -> T -> fn from(self) -> T
198+
// ~~~~ ~~~~
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}")),
200+
// fn into([mut] self: U) -> T -> fn into([mut] val: T) -> T
201+
// ~~~~~~~ ~~~~~~
202+
(self_ident.span.to(self_param.ty_span), format!("val: {from}")),
203203
];
204204

205205
if let FnRetTy::Return(_) = sig.decl.output {

tests/ui/from_over_into.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,15 @@ fn issue_12138() {
105105
}
106106
}
107107

108+
fn issue_112502() {
109+
struct MyInt(i64);
110+
111+
impl From<MyInt> for i64 {
112+
//~^ from_over_into
113+
fn from(val: MyInt) -> Self {
114+
val.0
115+
}
116+
}
117+
}
118+
108119
fn main() {}

tests/ui/from_over_into.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,15 @@ fn issue_12138() {
105105
}
106106
}
107107

108+
fn issue_112502() {
109+
struct MyInt(i64);
110+
111+
impl Into<i64> for MyInt {
112+
//~^ from_over_into
113+
fn into(self: MyInt) -> i64 {
114+
self.0
115+
}
116+
}
117+
}
118+
108119
fn main() {}

tests/ui/from_over_into.stderr

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,21 @@ LL |
107107
LL ~ fn from(val: Hello) {}
108108
|
109109

110-
error: aborting due to 7 previous errors
110+
error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true
111+
--> tests/ui/from_over_into.rs:111:5
112+
|
113+
LL | impl Into<i64> for MyInt {
114+
| ^^^^^^^^^^^^^^^^^^^^^^^^
115+
|
116+
= help: `impl From<Local> for Foreign` is allowed by the orphan rules, for more information see
117+
https://doc.rust-lang.org/reference/items/implementations.html#trait-implementation-coherence
118+
help: replace the `Into` implementation with `From<issue_112502::MyInt>`
119+
|
120+
LL ~ impl From<MyInt> for i64 {
121+
LL |
122+
LL ~ fn from(val: MyInt) -> Self {
123+
LL ~ val.0
124+
|
125+
126+
error: aborting due to 8 previous errors
111127

0 commit comments

Comments
 (0)