Skip to content

Commit

Permalink
Warn when encountering a serde_as attribute on a field with no values
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbb committed Jan 5, 2021
1 parent 95fc97b commit c911f26
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions serde_with_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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`."));
Expand Down
12 changes: 12 additions & 0 deletions serde_with_macros/tests/compile-fail/serde_as.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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,
| ^^^^^^^^^^^^^^^^^^^

0 comments on commit c911f26

Please sign in to comment.