Skip to content

Commit e025356

Browse files
committed
from_over_into: suggest a correct conversion to ()
1 parent a71211d commit e025356

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

clippy_lints/src/from_over_into.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use clippy_utils::source::snippet_opt;
66
use rustc_errors::Applicability;
77
use rustc_hir::intravisit::{walk_path, Visitor};
88
use rustc_hir::{
9-
GenericArg, GenericArgs, HirId, Impl, ImplItemKind, ImplItemRef, Item, ItemKind, PatKind, Path, PathSegment, Ty,
10-
TyKind,
9+
FnRetTy, GenericArg, GenericArgs, HirId, Impl, ImplItemKind, ImplItemRef, Item, ItemKind, PatKind, Path,
10+
PathSegment, Ty, TyKind,
1111
};
1212
use rustc_lint::{LateContext, LateLintPass};
1313
use rustc_middle::hir::nested_filter::OnlyBodies;
@@ -181,6 +181,9 @@ fn convert_to_from(
181181
let from = snippet_opt(cx, self_ty.span)?;
182182
let into = snippet_opt(cx, target_ty.span)?;
183183

184+
let return_type = matches!(sig.decl.output, FnRetTy::Return(_))
185+
.then_some(String::from("Self"))
186+
.unwrap_or_default();
184187
let mut suggestions = vec![
185188
// impl Into<T> for U -> impl From<T> for U
186189
// ~~~~ ~~~~
@@ -199,7 +202,7 @@ fn convert_to_from(
199202
(self_ident.span, format!("val: {from}")),
200203
// fn into(self) -> T -> fn into(self) -> Self
201204
// ~ ~~~~
202-
(sig.decl.output.span(), String::from("Self")),
205+
(sig.decl.output.span(), return_type),
203206
];
204207

205208
let mut finder = SelfFinder {

tests/ui/from_over_into.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,12 @@ fn msrv_1_41() {
8989
}
9090
}
9191

92+
fn issue_12138() {
93+
struct Hello;
94+
95+
impl From<Hello> for () {
96+
fn from(val: Hello) {}
97+
}
98+
}
99+
92100
fn main() {}

tests/ui/from_over_into.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,12 @@ fn msrv_1_41() {
8989
}
9090
}
9191

92+
fn issue_12138() {
93+
struct Hello;
94+
95+
impl Into<()> for Hello {
96+
fn into(self) {}
97+
}
98+
}
99+
92100
fn main() {}

tests/ui/from_over_into.stderr

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,19 @@ LL ~ fn from(val: Vec<T>) -> Self {
8686
LL ~ FromOverInto(val)
8787
|
8888

89-
error: aborting due to 6 previous errors
89+
error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true
90+
--> $DIR/from_over_into.rs:95:5
91+
|
92+
LL | impl Into<()> for Hello {
93+
| ^^^^^^^^^^^^^^^^^^^^^^^
94+
|
95+
= help: `impl From<Local> for Foreign` is allowed by the orphan rules, for more information see
96+
https://doc.rust-lang.org/reference/items/implementations.html#trait-implementation-coherence
97+
help: replace the `Into` implementation with `From<issue_12138::Hello>`
98+
|
99+
LL ~ impl From<Hello> for () {
100+
LL ~ fn from(val: Hello) {}
101+
|
102+
103+
error: aborting due to 7 previous errors
90104

0 commit comments

Comments
 (0)