Skip to content

clippy::from_over_into produces incorrect suggested code for unit type #12138

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

Closed
ctz opened this issue Jan 12, 2024 · 0 comments · Fixed by #12141
Closed

clippy::from_over_into produces incorrect suggested code for unit type #12138

ctz opened this issue Jan 12, 2024 · 0 comments · Fixed by #12141
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@ctz
Copy link

ctz commented Jan 12, 2024

Summary

struct Hello;

impl Into<()> for Hello {
    fn into(self) {
    }
}

Triggers clippy::from_over_into but the suggested fix is syntactically invalid.

Reproducer

I tried this code:

struct Hello;

impl Into<()> for Hello {
    fn into(self) {
    }
}

fn main() {
    Hello.into()
}

I expected to see this happen -- perhaps:

warning: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true
 --> src/main.rs:3:1
  |
3 | impl Into<()> for Hello {
  | ^^^^^^^^^^^^^^^^^^^^^^^
  |
  = 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: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#from_over_into
  = note: `#[warn(clippy::from_over_into)]` on by default
help: replace the `Into` implementation with `From<Hello>`
  |
3 ~ impl From<Hello> for () {
4 ~     fn from(_: Hello) {
  |

Instead, this happened:

warning: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true
 --> src/main.rs:3:1
  |
3 | impl Into<()> for Hello {
  | ^^^^^^^^^^^^^^^^^^^^^^^
  |
  = 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: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#from_over_into
  = note: `#[warn(clippy::from_over_into)]` on by default
help: replace the `Into` implementation with `From<Hello>`
  |
3 ~ impl From<Hello> for () {
4 ~     fn from(val: Hello)Self {
  |

Note the last line: fn from(val: Hello)Self { is not valid rust syntax.

Version

rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: x86_64-unknown-linux-gnu
release: 1.75.0
LLVM version: 17.0.6

Additional Labels

@rustbot label +I-suggestion-causes-error

@ctz ctz added the C-bug Category: Clippy is not doing the correct thing label Jan 12, 2024
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jan 12, 2024
bors added a commit that referenced this issue Jan 15, 2024
from_over_into: suggest a correct conversion to ()

changelog: [`from_over_into`]: suggest a correct conversion to `()`

Fix #12138
@bors bors closed this as completed in d6ff2d2 Jan 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants