From 8764353fe2f52000e40db29dd42e312dfcae918e Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 7 Aug 2024 18:16:48 -0700 Subject: [PATCH 1/4] Enable collection_is_never_read nursury lint in test 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) --- test_suite/tests/test_gen.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/test_suite/tests/test_gen.rs b/test_suite/tests/test_gen.rs index f8d577bb8..0459691bb 100644 --- a/test_suite/tests/test_gen.rs +++ b/test_suite/tests/test_gen.rs @@ -19,6 +19,7 @@ clippy::trivially_copy_pass_by_ref, clippy::type_repetition_in_bounds )] +#![deny(clippy::collection_is_never_read)] use serde::de::{Deserialize, DeserializeOwned, Deserializer}; use serde::ser::{Serialize, Serializer}; From c3df3372a18a7c0964b55bd495e52d666680a9c4 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 7 Aug 2024 18:17:50 -0700 Subject: [PATCH 2/4] Add test of flatten in enum 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) --- test_suite/tests/test_gen.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test_suite/tests/test_gen.rs b/test_suite/tests/test_gen.rs index 0459691bb..0119584c3 100644 --- a/test_suite/tests/test_gen.rs +++ b/test_suite/tests/test_gen.rs @@ -725,6 +725,19 @@ fn test_gen() { flat: T, } + #[derive(Serialize, Deserialize)] + #[serde(untagged)] + pub enum Inner { + Builder { + s: T, + #[serde(flatten)] + o: T, + }, + Default { + s: T, + }, + } + // https://github.com/serde-rs/serde/issues/1804 #[derive(Serialize, Deserialize)] pub enum Message { From d64a97ba1e099fc8033c9eea6fec14e1a71c0303 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 7 Aug 2024 18:19:46 -0700 Subject: [PATCH 3/4] Ignore confusable_idents warning in test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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)]` --- test_suite/tests/test_gen.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/test_suite/tests/test_gen.rs b/test_suite/tests/test_gen.rs index 0119584c3..3dacf00ab 100644 --- a/test_suite/tests/test_gen.rs +++ b/test_suite/tests/test_gen.rs @@ -4,6 +4,7 @@ #![deny(warnings)] #![allow( + confusable_idents, unknown_lints, mixed_script_confusables, clippy::derive_partial_eq_without_eq, From 32958dec3bf3886961166ec651194d6f0243a497 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 7 Aug 2024 18:43:47 -0700 Subject: [PATCH 4/4] Skip collecting unmatched fields in variants that do not use flatten --- serde_derive/src/de.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/serde_derive/src/de.rs b/serde_derive/src/de.rs index e3b737c61..c5861d9bc 100644 --- a/serde_derive/src/de.rs +++ b/serde_derive/src/de.rs @@ -2480,7 +2480,10 @@ fn deserialize_map( }); // Collect contents for flatten fields into a buffer - let let_collect = if cattrs.has_flatten() { + let has_flatten = fields + .iter() + .any(|field| field.attrs.flatten() && !field.attrs.skip_deserializing()); + let let_collect = if has_flatten { Some(quote! { let mut __collect = _serde::__private::Vec::<_serde::__private::Option<( _serde::__private::de::Content, @@ -2532,7 +2535,7 @@ fn deserialize_map( }); // Visit ignored values to consume them - let ignored_arm = if cattrs.has_flatten() { + let ignored_arm = if has_flatten { Some(quote! { __Field::__other(__name) => { __collect.push(_serde::__private::Some(( @@ -2602,7 +2605,7 @@ fn deserialize_map( } }); - let collected_deny_unknown_fields = if cattrs.has_flatten() && cattrs.deny_unknown_fields() { + let collected_deny_unknown_fields = if has_flatten && cattrs.deny_unknown_fields() { Some(quote! { if let _serde::__private::Some(_serde::__private::Some((__key, _))) = __collect.into_iter().filter(_serde::__private::Option::is_some).next()