@@ -16,8 +16,9 @@ declare_clippy_lint! {
16
16
/// containing zero sized values is effectively a set. Using a set in that case improves
17
17
/// readability and communicates intent more clearly.
18
18
///
19
- /// **Known problems:** A zero-sized type cannot be recovered later if it contains private
20
- /// fields.
19
+ /// **Known problems:**
20
+ /// * A zero-sized type cannot be recovered later if it contains private fields.
21
+ /// * This lints the signature of public items
21
22
///
22
23
/// **Example:**
23
24
///
@@ -33,7 +34,7 @@ declare_clippy_lint! {
33
34
/// }
34
35
/// ```
35
36
pub ZERO_SIZED_MAP_VALUES ,
36
- nursery ,
37
+ pedantic ,
37
38
"usage of map with zero-sized value type"
38
39
}
39
40
@@ -46,107 +47,35 @@ impl LateLintPass<'_> for ZeroSizedMapValues {
46
47
self . check_ty ( cx, ty, local. pat . span ) ;
47
48
}
48
49
49
- fn check_fn ( & mut self , cx : & LateContext < ' _ > , _: FnKind < ' _ > , decl : & FnDecl < ' _ > , _: & Body < ' _ > , _: Span , id : HirId ) {
50
- if let Some ( Node :: Item ( item) ) = cx. tcx . hir ( ) . find ( cx. tcx . hir ( ) . get_parent_item ( id) ) {
50
+ fn check_ty ( & mut self , cx : & LateContext < ' _ > , hir_ty : & hir:: Ty < ' _ > ) {
51
+ let parent_id = cx. tcx . hir ( ) . get_parent_item ( hir_ty. hir_id ) ;
52
+ if let Some ( Node :: Item ( item) ) = cx. tcx . hir ( ) . find ( cx. tcx . hir ( ) . get_parent_item ( parent_id) ) {
51
53
if let ItemKind :: Impl { of_trait : Some ( _) , .. } = item. kind {
52
54
return ;
53
55
}
54
56
}
55
57
56
- self . check_fn_decl ( cx, decl) ;
57
- }
58
-
59
- fn check_struct_field ( & mut self , cx : & LateContext < ' _ > , field : & StructField < ' _ > ) {
60
- self . check_hir_ty ( cx, field. ty ) ;
61
- }
62
-
63
- fn check_trait_item ( & mut self , cx : & LateContext < ' _ > , item : & TraitItem < ' _ > ) {
64
- match item. kind {
65
- TraitItemKind :: Const ( ref ty, _) | TraitItemKind :: Type ( _, Some ( ref ty) ) => {
66
- self . check_hir_ty ( cx, ty) ;
67
- } ,
68
- TraitItemKind :: Fn ( ref sig, _) => {
69
- self . check_fn_decl ( cx, & sig. decl ) ;
70
- } ,
71
- _ => ( ) ,
72
- }
73
- }
74
- }
75
-
76
- impl ZeroSizedMapValues {
77
- fn check_fn_decl ( & mut self , cx : & LateContext < ' _ > , decl : & FnDecl < ' _ > ) {
78
- for ty in decl. inputs {
79
- self . check_hir_ty ( cx, ty) ;
80
- }
81
-
82
- if let FnRetTy :: Return ( ty) = decl. output {
83
- self . check_hir_ty ( cx, ty) ;
84
- }
85
- }
86
-
87
- fn check_hir_ty ( & mut self , cx : & LateContext < ' _ > , hir_ty : & hir:: Ty < ' _ > ) {
58
+ // TODO this causes errors in some cases
88
59
let ty = hir_ty_to_ty ( cx. tcx , hir_ty) ;
89
60
self . check_ty ( cx, ty, hir_ty. span ) ;
90
-
91
- match hir_ty. kind {
92
- TyKind :: Path ( ref qpath) => match * qpath {
93
- QPath :: Resolved ( ty, ref p) => {
94
- if let Some ( ty) = ty {
95
- self . check_hir_ty ( cx, ty) ;
96
- }
97
-
98
- for ty in p. segments . iter ( ) . flat_map ( |seg| {
99
- seg. args
100
- . as_ref ( )
101
- . map_or_else ( || [ ] . iter ( ) , |params| params. args . iter ( ) )
102
- . filter_map ( |arg| match arg {
103
- GenericArg :: Type ( ty) => Some ( ty) ,
104
- _ => None ,
105
- } )
106
- } ) {
107
- self . check_hir_ty ( cx, ty) ;
108
- }
109
- } ,
110
- QPath :: TypeRelative ( ty, ref seg) => {
111
- self . check_hir_ty ( cx, ty) ;
112
-
113
- if let Some ( ref params) = seg. args {
114
- for ty in params. args . iter ( ) . filter_map ( |arg| match arg {
115
- GenericArg :: Type ( ty) => Some ( ty) ,
116
- _ => None ,
117
- } ) {
118
- self . check_hir_ty ( cx, ty) ;
119
- }
120
- }
121
- } ,
122
- QPath :: LangItem ( ..) => { } ,
123
- } ,
124
- TyKind :: Slice ( ref ty) | TyKind :: Array ( ref ty, _) | TyKind :: Ptr ( MutTy { ref ty, .. } ) => {
125
- self . check_hir_ty ( cx, ty) ;
126
- } ,
127
- TyKind :: Tup ( tys) => {
128
- for ty in tys {
129
- self . check_hir_ty ( cx, ty) ;
130
- }
131
- } ,
132
- _ => { } ,
133
- }
134
61
}
62
+ }
135
63
64
+ impl ZeroSizedMapValues {
136
65
fn check_ty < ' tcx > ( & mut self , cx : & LateContext < ' tcx > , ty : Ty < ' tcx > , span : Span ) {
137
66
if span. from_expansion ( ) {
138
67
return ;
139
68
}
140
69
141
70
if_chain ! {
142
- if match_type( cx, ty, & paths:: HASHMAP ) || match_type( cx, ty, & paths:: BTREEMAP ) ;
143
- if let Adt ( _, ref substs) = ty. kind( ) ;
144
- let ty = substs. type_at( 1 ) ;
145
- if let Ok ( layout) = cx. layout_of( ty) ;
146
- if layout. is_zst( ) ;
147
- then {
148
- span_lint_and_help( cx, ZERO_SIZED_MAP_VALUES , span, "map with zero-sized value type" , None , "consider using a set instead" ) ;
149
- }
71
+ if match_type( cx, ty, & paths:: HASHMAP ) || match_type( cx, ty, & paths:: BTREEMAP ) ;
72
+ if let Adt ( _, ref substs) = ty. kind( ) ;
73
+ let ty = substs. type_at( 1 ) ;
74
+ if let Ok ( layout) = cx. layout_of( ty) ;
75
+ if layout. is_zst( ) ;
76
+ then {
77
+ span_lint_and_help( cx, ZERO_SIZED_MAP_VALUES , span, "map with zero-sized value type" , None , "consider using a set instead" ) ;
78
+ }
150
79
}
151
80
}
152
81
}
0 commit comments