@@ -18,35 +18,49 @@ pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) {
18
18
check_from_and_try_from ( cx, cont) ;
19
19
}
20
20
21
+ fn check_default_on_tuple ( cx : & Ctxt , cont : & Container ) {
22
+ match & cont. data {
23
+ Data :: Enum ( variants) => {
24
+ for variant in variants {
25
+ if let Style :: Tuple = variant. style {
26
+ check_default_on_tuple_fields ( cx, & variant. fields ) ;
27
+ }
28
+ }
29
+ }
30
+ Data :: Struct ( Style :: Tuple , fields) => {
31
+ if let Default :: None = cont. attrs . default ( ) {
32
+ check_default_on_tuple_fields ( cx, & fields) ;
33
+ }
34
+ }
35
+ _ => { }
36
+ }
37
+ }
38
+
21
39
// If some field of a tuple struct is marked #[serde(default)] then all fields
22
40
// after it must also be marked with that attribute, or the struct must have a
23
41
// container-level serde(default) attribute. A field's default value is only
24
42
// used for tuple fields if the sequence is exhausted at that point; that means
25
43
// all subsequent fields will fail to deserialize if they don't have their own
26
44
// default.
27
- fn check_default_on_tuple ( cx : & Ctxt , cont : & Container ) {
28
- if let Default :: None = cont. attrs . default ( ) {
29
- if let Data :: Struct ( Style :: Tuple , fields) = & cont. data {
30
- let mut first_default_index = None ;
31
- for ( i, field) in fields. iter ( ) . enumerate ( ) {
32
- // Skipped fields automatically get the #[serde(default)]
33
- // attribute. We are interested only on non-skipped fields here.
34
- if field. attrs . skip_deserializing ( ) {
35
- continue ;
36
- }
37
- if let Default :: None = field. attrs . default ( ) {
38
- if let Some ( first) = first_default_index {
39
- cx. error_spanned_by (
40
- field. ty ,
41
- format ! ( "field must have #[serde(default)] because previous field {} has #[serde(default)]" , first) ,
42
- ) ;
43
- }
44
- continue ;
45
- }
46
- if first_default_index. is_none ( ) {
47
- first_default_index = Some ( i) ;
48
- }
45
+ fn check_default_on_tuple_fields ( cx : & Ctxt , fields : & [ Field ] ) {
46
+ let mut first_default_index = None ;
47
+ for ( i, field) in fields. iter ( ) . enumerate ( ) {
48
+ // Skipped fields automatically get the #[serde(default)]
49
+ // attribute. We are interested only on non-skipped fields here.
50
+ if field. attrs . skip_deserializing ( ) {
51
+ continue ;
52
+ }
53
+ if let Default :: None = field. attrs . default ( ) {
54
+ if let Some ( first) = first_default_index {
55
+ cx. error_spanned_by (
56
+ field. ty ,
57
+ format ! ( "field must have #[serde(default)] because previous field {} has #[serde(default)]" , first) ,
58
+ ) ;
49
59
}
60
+ continue ;
61
+ }
62
+ if first_default_index. is_none ( ) {
63
+ first_default_index = Some ( i) ;
50
64
}
51
65
}
52
66
}
0 commit comments