rust-lang / rust-lang/rust-clippy

clippy --fix barfs trying to convert `Into` to `From`

Open
#13,897 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary
use axum::response::sse::Event;
use serde::Serialize;

#[derive(Clone, Serialize)]
#[serde(tag = "type", rename_all = "kebab-case")]
pub enum SseMessage {
    Status { username: String, online: bool },
}

impl Into<Event> for SseMessage {
    fn into(self) -> Event {
        Event::default()
            .json_data::<SseMessage>(self.into())
            .unwrap()
    }
}

fn main() {
    let e: Event = SseMessage::Status {
        username: "marvin".to_string(),
        online: true,
    }
    .into();
    println!("{:?}", e);
}
Version
rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7
Error output
Cargo.toml

[package]
name = "clippy-bug"
version = "0.1.0"
edition = "2021"

[dependencies]
axum = { version = "0.7", features = ["json", "macros"] }
serde_json = "1"
serde = { version = "1", features = ["derive"] }

clippy output

$ cargo clippy --fix
  Checking clippy-bug v0.1.0 (/home/nirbheek/tmp/clippy-bug)
warning: failed to automatically apply fixes suggested by rustc to crate `clippy_bug`

after fixes were automatically applied the compiler reported errors within these files:

* src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0424]: expected value, found module `self`
--> src/main.rs:13:38
 |
11 |     fn from(val: SseMessage) -> Self {
 |        ---- this function doesn't have a `self` parameter
12 |         Event::default()
13 |             .json_data::<SseMessage>(self)
 |                                      ^^^^ `self` value is a keyword only available in methods with a `self` parameter
 |
help: add a `self` receiver parameter to make the associated `fn` a method
 |
11 |     fn from(&self, val: SseMessage) -> Self {
 |             ++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0424`.
Original diagnostics will follow.

warning: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true
--> src/main.rs:10:1
 |
10 | impl Into<Event> for SseMessage {
 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 |
 = 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<SseMessage>`
 |
10 ~ impl From<SseMessage> for Event {
11 ~     fn from(val: SseMessage) -> Self {
12 |         Event::default()
13 ~             .json_data::<SseMessage>(val.into())
 |

warning: useless conversion to the same type: `SseMessage`
--> src/main.rs:13:38
 |
13 |             .json_data::<SseMessage>(self.into())
 |                                      ^^^^^^^^^^^ help: consider removing `.into()`: `self`
 |
 = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion
 = note: `#[warn(clippy::useless_conversion)]` on by default


Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reduced src/main.rs reproduction and the from_over_into lint, then inspect how its suggested Into-to-From rewrite interacts with the existing self.into() expression. Run cargo clippy --fix and confirm the rewritten code compiles without E0424 while retaining the intended conversion behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.