Skip to content

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

@seam345

Description

@seam345

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions