Skip to content

Commit 4886937

Browse files
committed
Auto merge of #10853 - MarcusGrass:fix-from-over-into-self, r=Alexendoo
Ignore fix for `from_over_into` if the target type contains a `Self` reference Fixes #10838. This is my first time contributing here, and the fix is kind of ugly. I've worked a bit with `quote` and was trying to figure out a way to replace the type in a better way than just a raw string-replace but couldn't quite figure out how to. The only thing really required to fix this, is to replace all `Self` references with the type stated in the `from` variable, this isn't entirely simple to do with raw strings without creating a mess though. We need to find and replace all `Self`'s in a variable with `from` but there could be an arbitrary amount, in a lot of different positions. As well as some type that contains the name self, like `SelfVarSelf` which shouldn't be replaced. The strategy is essentially, if `"Self"` is surrounded on both sides by something that isn't alphanumeric, then we're golden, then trying to make that reasonably efficient. I would not be offended if the solution is too messy to accept! changelog: [from_over_into]: Replace Self with the indicated variable in suggestion and fix.
2 parents 58befc7 + b2c85b3 commit 4886937

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

clippy_lints/src/from_over_into.rs

+5
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ fn convert_to_from(
156156
self_ty: &Ty<'_>,
157157
impl_item_ref: &ImplItemRef,
158158
) -> Option<Vec<(Span, String)>> {
159+
if !target_ty.find_self_aliases().is_empty() {
160+
// It's tricky to expand self-aliases correctly, we'll ignore it to not cause a
161+
// bad suggestion/fix.
162+
return None;
163+
}
159164
let impl_item = cx.tcx.hir().impl_item(impl_item_ref.id);
160165
let ImplItemKind::Fn(ref sig, body_id) = impl_item.kind else { return None };
161166
let body = cx.tcx.hir().body(body_id);

tests/ui/from_over_into_unfixable.rs

+10
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,14 @@ impl Into<u8> for ContainsVal {
3232
}
3333
}
3434

35+
pub struct Lval<T>(T);
36+
37+
pub struct Rval<T>(T);
38+
39+
impl<T> Into<Rval<Self>> for Lval<T> {
40+
fn into(self) -> Rval<Self> {
41+
Rval(self)
42+
}
43+
}
44+
3545
fn main() {}

tests/ui/from_over_into_unfixable.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,13 @@ LL | impl Into<u8> for ContainsVal {
2525
https://doc.rust-lang.org/reference/items/implementations.html#trait-implementation-coherence
2626
= help: replace the `Into` implementation with `From<ContainsVal>`
2727

28-
error: aborting due to 3 previous errors
28+
error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true
29+
--> $DIR/from_over_into_unfixable.rs:39:1
30+
|
31+
LL | impl<T> Into<Rval<Self>> for Lval<T> {
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
33+
|
34+
= help: replace the `Into` implementation with `From<Lval<T>>`
35+
36+
error: aborting due to 4 previous errors
2937

0 commit comments

Comments
 (0)