-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #9649 - Alexendoo:from-over-into-suggestion, r=llogiq
Add a suggestion and a note about orphan rules for `from_over_into` Adds a machine applicable suggestion to convert the `Into` impl into a `From` one to `from_over_into` Also adds a note explaining that `impl From<Local> for Foreign` is fine if the `Into` type is foreign Closes #7444 Addresses half of #9638 changelog: [`from_over_into`] Add a suggestion and a note about orphan rules
- Loading branch information
Showing
6 changed files
with
375 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::from_over_into)] | ||
#![allow(unused)] | ||
|
||
// this should throw an error | ||
struct StringWrapper(String); | ||
|
||
impl From<String> for StringWrapper { | ||
fn from(val: String) -> Self { | ||
StringWrapper(val) | ||
} | ||
} | ||
|
||
struct SelfType(String); | ||
|
||
impl From<String> for SelfType { | ||
fn from(val: String) -> Self { | ||
SelfType(String::new()) | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
struct X; | ||
|
||
impl X { | ||
const FOO: &'static str = "a"; | ||
} | ||
|
||
struct SelfKeywords; | ||
|
||
impl From<X> for SelfKeywords { | ||
fn from(val: X) -> Self { | ||
let _ = X::default(); | ||
let _ = X::FOO; | ||
let _: X = val; | ||
|
||
SelfKeywords | ||
} | ||
} | ||
|
||
struct ExplicitPaths(bool); | ||
|
||
impl core::convert::From<crate::ExplicitPaths> for bool { | ||
fn from(mut val: crate::ExplicitPaths) -> Self { | ||
let in_closure = || val.0; | ||
|
||
val.0 = false; | ||
val.0 | ||
} | ||
} | ||
|
||
// this is fine | ||
struct A(String); | ||
|
||
impl From<String> for A { | ||
fn from(s: String) -> A { | ||
A(s) | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,62 @@ | ||
error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true | ||
--> $DIR/from_over_into.rs:6:1 | ||
--> $DIR/from_over_into.rs:9:1 | ||
| | ||
LL | impl Into<StringWrapper> for String { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider to implement `From<std::string::String>` instead | ||
= note: `-D clippy::from-over-into` implied by `-D warnings` | ||
help: replace the `Into` implentation with `From<std::string::String>` | ||
| | ||
LL ~ impl From<String> for StringWrapper { | ||
LL ~ fn from(val: String) -> Self { | ||
LL ~ StringWrapper(val) | ||
| | ||
|
||
error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true | ||
--> $DIR/from_over_into.rs:17:1 | ||
| | ||
LL | impl Into<SelfType> for String { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: replace the `Into` implentation with `From<std::string::String>` | ||
| | ||
LL ~ impl From<String> for SelfType { | ||
LL ~ fn from(val: String) -> Self { | ||
LL ~ SelfType(String::new()) | ||
| | ||
|
||
error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true | ||
--> $DIR/from_over_into.rs:32:1 | ||
| | ||
LL | impl Into<SelfKeywords> for X { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: replace the `Into` implentation with `From<X>` | ||
| | ||
LL ~ impl From<X> for SelfKeywords { | ||
LL ~ fn from(val: X) -> Self { | ||
LL ~ let _ = X::default(); | ||
LL ~ let _ = X::FOO; | ||
LL ~ let _: X = val; | ||
| | ||
|
||
error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true | ||
--> $DIR/from_over_into.rs:44:1 | ||
| | ||
LL | impl core::convert::Into<bool> for crate::ExplicitPaths { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= 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` implentation with `From<ExplicitPaths>` | ||
| | ||
LL ~ impl core::convert::From<crate::ExplicitPaths> for bool { | ||
LL ~ fn from(mut val: crate::ExplicitPaths) -> Self { | ||
LL ~ let in_closure = || val.0; | ||
LL | | ||
LL ~ val.0 = false; | ||
LL ~ val.0 | ||
| | ||
|
||
error: aborting due to previous error | ||
error: aborting due to 4 previous errors | ||
|
Oops, something went wrong.