11use clippy_utils:: attrs:: span_contains_cfg;
22use clippy_utils:: diagnostics:: { span_lint_and_then, span_lint_hir_and_then} ;
3- use rustc_data_structures:: fx:: FxIndexMap ;
3+ use clippy_utils:: source:: SpanRangeExt ;
4+ use clippy_utils:: span_contains_non_whitespace;
5+ use rustc_data_structures:: fx:: { FxIndexMap , IndexEntry } ;
46use rustc_errors:: Applicability ;
57use rustc_hir:: def:: DefKind :: Ctor ;
68use rustc_hir:: def:: Res :: Def ;
@@ -10,7 +12,7 @@ use rustc_hir::{Expr, ExprKind, Item, ItemKind, Node, Pat, PatKind, Path, QPath,
1012use rustc_lint:: { LateContext , LateLintPass } ;
1113use rustc_middle:: ty:: { self , TyCtxt } ;
1214use rustc_session:: impl_lint_pass;
13- use rustc_span:: Span ;
15+ use rustc_span:: { BytePos , Span } ;
1416
1517declare_clippy_lint ! {
1618 /// ### What it does
@@ -118,98 +120,80 @@ impl LateLintPass<'_> for EmptyWithBrackets {
118120 }
119121
120122 fn check_variant ( & mut self , cx : & LateContext < ' _ > , variant : & Variant < ' _ > ) {
121- // FIXME: handle `$name {}`
122123 if !variant. span . from_expansion ( )
123124 && !variant. ident . span . from_expansion ( )
124125 && let span_after_ident = variant. span . with_lo ( variant. ident . span . hi ( ) )
125126 && has_no_fields ( cx, & variant. data , span_after_ident)
126127 {
127128 match variant. data {
128129 VariantData :: Struct { .. } => {
129- // Empty struct variants can be linted immediately
130- span_lint_and_then (
131- cx,
132- EMPTY_ENUM_VARIANTS_WITH_BRACKETS ,
133- span_after_ident,
134- "enum variant has empty brackets" ,
135- |diagnostic| {
136- diagnostic. span_suggestion_hidden (
137- span_after_ident,
138- "remove the brackets" ,
139- "" ,
140- Applicability :: MaybeIncorrect ,
141- ) ;
142- } ,
143- ) ;
130+ self . add_enum_variant ( variant. def_id ) ;
144131 } ,
145132 VariantData :: Tuple ( .., local_def_id) => {
146133 // Don't lint reachable tuple enums
147134 if cx. effective_visibilities . is_reachable ( variant. def_id ) {
148135 return ;
149136 }
150- if let Some ( entry) = self . empty_tuple_enum_variants . get_mut ( & local_def_id) {
151- // empty_tuple_enum_variants contains Usage::NoDefinition if the variant was called before the
152- // definition was encountered. Now that there's a definition, convert it
153- // to Usage::Unused.
154- if let Usage :: NoDefinition { redundant_use_sites } = entry {
155- * entry = Usage :: Unused {
156- redundant_use_sites : redundant_use_sites. clone ( ) ,
157- } ;
158- }
159- } else {
160- self . empty_tuple_enum_variants . insert (
161- local_def_id,
162- Usage :: Unused {
163- redundant_use_sites : vec ! [ ] ,
164- } ,
165- ) ;
166- }
137+ self . add_enum_variant ( local_def_id) ;
167138 } ,
168139 VariantData :: Unit ( ..) => { } ,
169140 }
170141 }
171142 }
172143
173144 fn check_expr ( & mut self , cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) {
174- if let Some ( def_id) = check_expr_for_enum_as_function ( expr) {
175- if let Some ( parentheses_span) = call_parentheses_span ( cx. tcx , expr) {
145+ if let Some ( ( def_id, mut span) ) = check_expr_for_enum_as_function ( cx, expr) {
146+ if span. is_empty ( )
147+ && let Some ( parentheses_span) = call_parentheses_span ( cx. tcx , expr)
148+ {
149+ span = parentheses_span;
150+ }
151+
152+ if span. is_empty ( ) {
153+ // The parentheses are not redundant.
154+ self . empty_tuple_enum_variants . insert ( def_id, Usage :: Used ) ;
155+ } else {
176156 // Do not count expressions from macro expansion as a redundant use site.
177157 if expr. span . from_expansion ( ) {
178158 return ;
179159 }
180- self . update_enum_variant_usage ( def_id, parentheses_span) ;
181- } else {
182- // The parentheses are not redundant.
183- self . empty_tuple_enum_variants . insert ( def_id, Usage :: Used ) ;
160+ self . update_enum_variant_usage ( def_id, span) ;
184161 }
185162 }
186163 }
187164
188165 fn check_pat ( & mut self , cx : & LateContext < ' _ > , pat : & Pat < ' _ > ) {
189- if let Some ( ( def_id, parentheses_span) ) = check_pat_for_enum_as_function ( cx, pat) {
190- if pat. span . from_expansion ( ) {
191- return ;
192- }
193-
194- self . update_enum_variant_usage ( def_id, parentheses_span) ;
166+ if !pat. span . from_expansion ( )
167+ && let Some ( ( def_id, span) ) = check_pat_for_enum_as_function ( cx, pat)
168+ {
169+ self . update_enum_variant_usage ( def_id, span) ;
195170 }
196171 }
197172
198173 fn check_crate_post ( & mut self , cx : & LateContext < ' _ > ) {
199- for ( local_def_id, usage) in & self . empty_tuple_enum_variants {
174+ for ( & local_def_id, usage) in & self . empty_tuple_enum_variants {
200175 // Ignore all variants with Usage::Used or Usage::NoDefinition
201176 let Usage :: Unused { redundant_use_sites } = usage else {
202177 continue ;
203178 } ;
179+
204180 // Attempt to fetch the Variant from LocalDefId.
205- let Node :: Variant ( variant) = cx. tcx . hir_node (
206- cx. tcx
207- . local_def_id_to_hir_id ( cx. tcx . parent ( local_def_id. to_def_id ( ) ) . expect_local ( ) ) ,
208- ) else {
181+ let variant = if let Node :: Variant ( variant) = cx. tcx . hir_node_by_def_id ( local_def_id) {
182+ variant
183+ } else if let Node :: Variant ( variant) = cx. tcx . hir_node_by_def_id ( cx. tcx . local_parent ( local_def_id) ) {
184+ variant
185+ } else {
209186 continue ;
210187 } ;
188+
211189 // Span of the parentheses in variant definition
212190 let span = variant. span . with_lo ( variant. ident . span . hi ( ) ) ;
191+ let span_inner = span
192+ . with_lo ( SpanRangeExt :: trim_start ( span, cx) . start + BytePos ( 1 ) )
193+ . with_hi ( span. hi ( ) - BytePos ( 1 ) ) ;
194+ if span_contains_non_whitespace ( cx, span_inner, false ) {
195+ continue ;
196+ }
213197 span_lint_hir_and_then (
214198 cx,
215199 EMPTY_ENUM_VARIANTS_WITH_BRACKETS ,
@@ -242,28 +226,38 @@ impl LateLintPass<'_> for EmptyWithBrackets {
242226}
243227
244228impl EmptyWithBrackets {
229+ fn add_enum_variant ( & mut self , local_def_id : LocalDefId ) {
230+ self . empty_tuple_enum_variants
231+ . entry ( local_def_id)
232+ . and_modify ( |entry| {
233+ // empty_tuple_enum_variants contains Usage::NoDefinition if the variant was called before
234+ // the definition was encountered. Now that there's a
235+ // definition, convert it to Usage::Unused.
236+ if let Usage :: NoDefinition { redundant_use_sites } = entry {
237+ * entry = Usage :: Unused {
238+ redundant_use_sites : redundant_use_sites. clone ( ) ,
239+ } ;
240+ }
241+ } )
242+ . or_insert_with ( || Usage :: Unused {
243+ redundant_use_sites : vec ! [ ] ,
244+ } ) ;
245+ }
246+
245247 fn update_enum_variant_usage ( & mut self , def_id : LocalDefId , parentheses_span : Span ) {
246- match self . empty_tuple_enum_variants . get_mut ( & def_id) {
247- Some (
248- & mut ( Usage :: Unused {
249- ref mut redundant_use_sites,
248+ match self . empty_tuple_enum_variants . entry ( def_id) {
249+ IndexEntry :: Occupied ( mut e) => {
250+ if let Usage :: Unused { redundant_use_sites } | Usage :: NoDefinition { redundant_use_sites } = e. get_mut ( )
251+ {
252+ redundant_use_sites. push ( parentheses_span) ;
250253 }
251- | Usage :: NoDefinition {
252- ref mut redundant_use_sites,
253- } ) ,
254- ) => {
255- redundant_use_sites. push ( parentheses_span) ;
256254 } ,
257- None => {
255+ IndexEntry :: Vacant ( e ) => {
258256 // The variant isn't in the IndexMap which means its definition wasn't encountered yet.
259- self . empty_tuple_enum_variants . insert (
260- def_id,
261- Usage :: NoDefinition {
262- redundant_use_sites : vec ! [ parentheses_span] ,
263- } ,
264- ) ;
257+ e. insert ( Usage :: NoDefinition {
258+ redundant_use_sites : vec ! [ parentheses_span] ,
259+ } ) ;
265260 } ,
266- _ => { } ,
267261 }
268262 }
269263}
@@ -293,18 +287,27 @@ fn call_parentheses_span(tcx: TyCtxt<'_>, expr: &Expr<'_>) -> Option<Span> {
293287}
294288
295289// Returns the LocalDefId of the variant being called as a function if it exists.
296- fn check_expr_for_enum_as_function ( expr : & Expr < ' _ > ) -> Option < LocalDefId > {
297- if let ExprKind :: Path ( QPath :: Resolved (
298- _,
299- Path {
300- res : Def ( Ctor ( CtorOf :: Variant , _) , def_id) ,
301- ..
290+ fn check_expr_for_enum_as_function ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> Option < ( LocalDefId , Span ) > {
291+ match expr. kind {
292+ ExprKind :: Path ( QPath :: Resolved (
293+ _,
294+ Path {
295+ res : Def ( Ctor ( CtorOf :: Variant , _) , def_id) ,
296+ span,
297+ ..
298+ } ,
299+ ) ) => def_id. as_local ( ) . map ( |id| ( id, span. with_lo ( expr. span . hi ( ) ) ) ) ,
300+ ExprKind :: Struct ( qpath, ..)
301+ if let Def ( DefKind :: Variant , mut def_id) = cx. typeck_results ( ) . qpath_res ( qpath, expr. hir_id ) =>
302+ {
303+ let ty = cx. tcx . type_of ( def_id) . instantiate_identity ( ) ;
304+ if let ty:: FnDef ( ctor_def_id, _) = ty. kind ( ) {
305+ def_id = * ctor_def_id;
306+ }
307+
308+ def_id. as_local ( ) . map ( |id| ( id, qpath. span ( ) . with_lo ( expr. span . hi ( ) ) ) )
302309 } ,
303- ) ) = expr. kind
304- {
305- def_id. as_local ( )
306- } else {
307- None
310+ _ => None ,
308311 }
309312}
310313
@@ -316,10 +319,13 @@ fn check_pat_for_enum_as_function(cx: &LateContext<'_>, pat: &Pat<'_>) -> Option
316319 def_id. as_local ( ) . map ( |id| ( id, qpath. span ( ) . with_lo ( pat. span . hi ( ) ) ) )
317320 } ,
318321 PatKind :: Struct ( qpath, ..)
319- if let Def ( DefKind :: Variant , def_id) = cx. typeck_results ( ) . qpath_res ( & qpath, pat. hir_id )
320- && let ty = cx. tcx . type_of ( def_id) . instantiate_identity ( )
321- && let ty:: FnDef ( def_id, _) = ty. kind ( ) =>
322+ if let Def ( DefKind :: Variant , mut def_id) = cx. typeck_results ( ) . qpath_res ( & qpath, pat. hir_id ) =>
322323 {
324+ let ty = cx. tcx . type_of ( def_id) . instantiate_identity ( ) ;
325+ if let ty:: FnDef ( ctor_def_id, _) = ty. kind ( ) {
326+ def_id = * ctor_def_id;
327+ }
328+
323329 def_id. as_local ( ) . map ( |id| ( id, qpath. span ( ) . with_lo ( pat. span . hi ( ) ) ) )
324330 } ,
325331 _ => None ,
0 commit comments