Skip to content

clippy --fix barfs trying to convert Into to From #13897

@nirbheek

Description

@nirbheek

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


Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions