Skip to content
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

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

Open
nirbheek opened this issue Dec 29, 2024 · 1 comment
Open

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

nirbheek opened this issue Dec 29, 2024 · 1 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

Comments

@nirbheek
Copy link

nirbheek commented Dec 29, 2024

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


@nirbheek nirbheek added C-bug Category: Clippy is not doing the correct thing I-ICE Issue: Clippy panicked, giving an Internal Compilation Error (ICE) ❄️ labels Dec 29, 2024
@samueltardieu
Copy link
Contributor

Clippy tries to apply two fixes to the same code span in succession.

@Jarcho Jarcho added I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied and removed I-ICE Issue: Clippy panicked, giving an Internal Compilation Error (ICE) ❄️ labels Dec 30, 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

No branches or pull requests

3 participants