Skip to content

Fix from_over_into lint suggesting invalid code #14409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions clippy_lints/src/from_over_into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand All @@ -194,12 +194,12 @@ fn convert_to_from(
// impl Into<T> for U -> impl Into<T> for T
// ~ ~
(self_ty.span, into.to_owned()),
// fn into(self) -> T -> fn from(self) -> T
// ~~~~ ~~~~
// fn into(self: U) -> 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}")),
// fn into([mut] self: U) -> T -> fn into([mut] val: T) -> T
// ~~~~~~~ ~~~~~~
(self_ident.span.to(self_param.ty_span), format!("val: {from}")),
];

if let FnRetTy::Return(_) = sig.decl.output {
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/from_over_into.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,15 @@ fn issue_12138() {
}
}

fn issue_112502() {
struct MyInt(i64);

impl From<MyInt> for i64 {
//~^ from_over_into
fn from(val: MyInt) -> Self {
val.0
}
}
}

fn main() {}
11 changes: 11 additions & 0 deletions tests/ui/from_over_into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,15 @@ fn issue_12138() {
}
}

fn issue_112502() {
struct MyInt(i64);

impl Into<i64> for MyInt {
//~^ from_over_into
fn into(self: MyInt) -> i64 {
self.0
}
}
}

fn main() {}
18 changes: 17 additions & 1 deletion tests/ui/from_over_into.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,21 @@ LL |
LL ~ fn from(val: Hello) {}
|

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

error: aborting due to 8 previous errors