From c911f26a53e240033815c0637d6ad09e89db8f6f Mon Sep 17 00:00:00 2001 From: Jonas Bushart Date: Tue, 5 Jan 2021 23:17:13 +0100 Subject: [PATCH] Warn when encountering a serde_as attribute on a field with no values --- serde_with_macros/src/lib.rs | 5 +++++ serde_with_macros/tests/compile-fail/serde_as.stderr | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/serde_with_macros/src/lib.rs b/serde_with_macros/src/lib.rs index eeccf568..bf81f49b 100644 --- a/serde_with_macros/src/lib.rs +++ b/serde_with_macros/src/lib.rs @@ -499,6 +499,7 @@ fn serde_as_add_attr_to_field( let serde_as_options = SerdeAsOptions::from_field(field)?; let serde_with_options = SerdeWithOptions::from_field(field)?; + // TODO allow multiple attributes // Find index of serde_as attribute let serde_as_idx = field.attrs.iter().enumerate().find_map(|(idx, attr)| { if attr.path.is_ident("serde_as") { @@ -515,6 +516,10 @@ fn serde_as_add_attr_to_field( // serde_as Attribute field.attrs.remove(serde_as_idx); + if !serde_as_options.has_any_set() { + return Err(DarlingError::custom("An empty `serde_as` attribute on a field has no effect. You are missing an `as`, `serialize_as`, or `deserialize_as` parameter.")); + } + // Check if there are any conflicting attributes if serde_as_options.has_any_set() && serde_with_options.has_any_set() { return Err(DarlingError::custom("Cannot combine `serde_as` with serde's `with`, `deserialize_with`, or `serialize_with`.")); diff --git a/serde_with_macros/tests/compile-fail/serde_as.stderr b/serde_with_macros/tests/compile-fail/serde_as.stderr index 49612513..84c5aa8d 100644 --- a/serde_with_macros/tests/compile-fail/serde_as.stderr +++ b/serde_with_macros/tests/compile-fail/serde_as.stderr @@ -93,3 +93,15 @@ error: Unable to parse attribute: expected literal | 67 | #[serde_as(serialize_as = "123", deserialize_as = DisplayFromStr)] | ^^^^^^^^^^^^^^ + +error: An empty `serde_as` attribute on a field has no effect. You are missing an `as`, `serialize_as`, or `deserialize_as` parameter. + --> $DIR/serde_as.rs:70:5 + | +70 | no_entries: u32, + | ^^^^^^^^^^ + +error: An empty `serde_as` attribute on a field has no effect. You are missing an `as`, `serialize_as`, or `deserialize_as` parameter. + --> $DIR/serde_as.rs:72:5 + | +72 | no_entries_brackets: u32, + | ^^^^^^^^^^^^^^^^^^^