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

No unreachable warning when duplicate field names on enum after rename attributes #2308

Closed
seam345 opened this issue Nov 2, 2022 · 2 comments · Fixed by #2562 or #2855
Closed

No unreachable warning when duplicate field names on enum after rename attributes #2308

seam345 opened this issue Nov 2, 2022 · 2 comments · Fixed by #2562 or #2855

Comments

@seam345
Copy link

seam345 commented Nov 2, 2022

Looking into the rename feature I discovered that rustc does not gives an unreachable warning with serde rename collisions with enums whereas it does on structs as mentioned in #754

Would you still be open to a PR to make clashing renames an error? If so I'm happy to give it a go.

Example

use serde::{Deserialize};

#[derive(Deserialize)]
enum Message {
    #[serde(rename = "Response")]
    Request { id: String},
    #[serde(rename = "Response")]
    Response { id: String},
}

fn main() {
    let json = "{\"Response\": {\"id\": \"...\"}}";
    
    let parsed: Message = match serde_json::from_str(&json) {
        Ok(contact) => contact,
        Err(err) => {
            println!("{:?}", err);
            unimplemented!()
        }
    };

    match parsed {
        Message::Request { id } => println!("request {}", id),
        Message::Response { id } => println!("response {}", id)
    }
    
}

Output

request ...
with no compiler warnings

playgrounds link https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c6a787d51f1290af999a0e36b9a6d366

@seam345 seam345 changed the title Duplicate field names after rename attributes are applied nolonger results in unreachable warning No unreachable warning when duplicate field names after rename attributes Nov 3, 2022
@seam345 seam345 changed the title No unreachable warning when duplicate field names after rename attributes No unreachable warning when duplicate field names on enum after rename attributes May 13, 2023
@seam345
Copy link
Author

seam345 commented May 13, 2023

or maybe instead of an error it should follow rustc and display a warning

@dtolnay
Copy link
Member

dtolnay commented Nov 11, 2024

This was not fixed by #2562. The code above still compiles with no error or warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants