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

Skip collecting unmatched fields in variants that do not use flatten #2791

Merged
merged 4 commits into from
Aug 8, 2024

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Aug 8, 2024

Fixes #2789.

For the enum in the repro:

#[derive(Deserialize)]
#[serde(untagged)]
enum Inner<'a> {
    Default {
        s: &'a str,
    },
    Builder {
        s: &'a str,
        #[serde(flatten)]
        o: Options,
    },
}

we would previously collect a Vec<Option<(Content, Content)>> of the unrecognized keys and values in the input. These would be used for deserializing o inside Builder, but when deserializing Default, the vector would not be used and would correctly trigger the "collection is never read" warning.

dtolnay added 4 commits August 7, 2024 18:16
    error: collection is never read
       --> test_suite/tests/test_gen.rs:722:25
        |
    722 |     #[derive(Serialize, Deserialize)]
        |                         ^^^^^^^^^^^
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#collection_is_never_read
    note: the lint level is defined here
       --> test_suite/tests/test_gen.rs:22:9
        |
    22  | #![deny(clippy::collection_is_never_read)]
        |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        = note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)
    error: collection is never read
       --> test_suite/tests/test_gen.rs:728:25
        |
    728 |     #[derive(Serialize, Deserialize)]
        |                         ^^^^^^^^^^^
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#collection_is_never_read
        = note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)
    error: found both `σ` and `o` as identifiers, which look alike
       --> test_suite/tests/test_gen.rs:734:13
        |
    292 |         σ: f64,
        |         - other identifier used here
    ...
    734 |             o: T,
        |             ^ this identifier can be confused with `σ`
        |
    note: the lint level is defined here
       --> test_suite/tests/test_gen.rs:5:9
        |
    5   | #![deny(warnings)]
        |         ^^^^^^^^
        = note: `#[deny(confusable_idents)]` implied by `#[deny(warnings)]`
@dtolnay dtolnay merged commit c3eaf76 into serde-rs:master Aug 8, 2024
15 checks passed
@dtolnay dtolnay deleted the flatten branch August 8, 2024 01:52
@Mingun
Copy link
Contributor

Mingun commented Aug 9, 2024

A more complete fix for this is #2567. Please consider to merge it

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

Successfully merging this pull request may close these issues.

error: collection is never read on #[derive(Deserialize)]
2 participants