1- use clippy_utils:: diagnostics:: span_lint_and_then;
1+ use clippy_utils:: diagnostics:: { span_lint_and_then, span_lint_hir_and_then } ;
22use clippy_utils:: source:: snippet_opt;
3- use rustc_ast :: ast :: { Item , ItemKind , Variant , VariantData } ;
3+ use rustc_data_structures :: fx :: FxIndexMap ;
44use rustc_errors:: Applicability ;
5+ use rustc_hir:: def:: CtorOf ;
6+ use rustc_hir:: def:: DefKind :: Ctor ;
7+ use rustc_hir:: def:: Res :: Def ;
8+ use rustc_hir:: def_id:: LocalDefId ;
9+ use rustc_hir:: { Expr , ExprKind , Item , ItemKind , Node , Path , QPath , Variant , VariantData } ;
510use rustc_lexer:: TokenKind ;
6- use rustc_lint:: { EarlyContext , EarlyLintPass } ;
7- use rustc_session:: declare_lint_pass;
11+ use rustc_lint:: { LateContext , LateLintPass } ;
12+ use rustc_middle:: ty:: TyCtxt ;
13+ use rustc_session:: impl_lint_pass;
814use rustc_span:: Span ;
915
1016declare_clippy_lint ! {
@@ -70,10 +76,22 @@ declare_clippy_lint! {
7076 "finds enum variants with empty brackets"
7177}
7278
73- declare_lint_pass ! ( EmptyWithBrackets => [ EMPTY_STRUCTS_WITH_BRACKETS , EMPTY_ENUM_VARIANTS_WITH_BRACKETS ] ) ;
79+ #[ derive( Debug ) ]
80+ enum Usage {
81+ Unused { redundant_use_sites : Vec < Span > } ,
82+ Used ,
83+ }
84+
85+ #[ derive( Default ) ]
86+ pub struct EmptyWithBrackets {
87+ // Value holds `true` if the empty tuple variant was used as a function
88+ empty_tuple_enum_variants : FxIndexMap < LocalDefId , Usage > ,
89+ }
90+
91+ impl_lint_pass ! ( EmptyWithBrackets => [ EMPTY_STRUCTS_WITH_BRACKETS , EMPTY_ENUM_VARIANTS_WITH_BRACKETS ] ) ;
7492
75- impl EarlyLintPass for EmptyWithBrackets {
76- fn check_item ( & mut self , cx : & EarlyContext < ' _ > , item : & Item ) {
93+ impl LateLintPass < ' _ > for EmptyWithBrackets {
94+ fn check_item ( & mut self , cx : & LateContext < ' _ > , item : & Item < ' _ > ) {
7795 let span_after_ident = item. span . with_lo ( item. ident . span . hi ( ) ) ;
7896
7997 if let ItemKind :: Struct ( var_data, _) = & item. kind
@@ -97,22 +115,94 @@ impl EarlyLintPass for EmptyWithBrackets {
97115 }
98116 }
99117
100- fn check_variant ( & mut self , cx : & EarlyContext < ' _ > , variant : & Variant ) {
118+ fn check_variant ( & mut self , cx : & LateContext < ' _ > , variant : & Variant < ' _ > ) {
119+ // Don't lint pub enums
120+ if cx. effective_visibilities . is_reachable ( variant. def_id ) {
121+ return ;
122+ }
123+
101124 let span_after_ident = variant. span . with_lo ( variant. ident . span . hi ( ) ) ;
102125
103- if has_brackets ( & variant. data ) && has_no_fields ( cx, & variant. data , span_after_ident) {
104- span_lint_and_then (
126+ if has_no_fields ( cx, & variant. data , span_after_ident) {
127+ match variant. data {
128+ VariantData :: Struct { .. } => {
129+ span_lint_and_then (
130+ cx,
131+ EMPTY_ENUM_VARIANTS_WITH_BRACKETS ,
132+ span_after_ident,
133+ "enum variant has empty brackets" ,
134+ |diagnostic| {
135+ diagnostic. span_suggestion_hidden (
136+ span_after_ident,
137+ "remove the brackets" ,
138+ "" ,
139+ Applicability :: MaybeIncorrect ,
140+ ) ;
141+ } ,
142+ ) ;
143+ } ,
144+ VariantData :: Tuple ( .., local_def_id) => {
145+ self . empty_tuple_enum_variants
146+ . entry ( local_def_id)
147+ . or_insert ( Usage :: Unused {
148+ redundant_use_sites : vec ! [ ] ,
149+ } ) ;
150+ } ,
151+ VariantData :: Unit ( ..) => { } ,
152+ }
153+ }
154+ }
155+
156+ fn check_expr ( & mut self , cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) {
157+ if let Some ( def_id) = check_expr_for_enum_as_function ( expr) {
158+ if let Some ( parentheses_span) = call_parentheses_span ( cx. tcx , expr)
159+ && let Some ( Usage :: Unused {
160+ ref mut redundant_use_sites,
161+ } ) = self . empty_tuple_enum_variants . get_mut ( & def_id)
162+ {
163+ redundant_use_sites. push ( parentheses_span) ;
164+ } else {
165+ self . empty_tuple_enum_variants . insert ( def_id, Usage :: Used ) ;
166+ }
167+ }
168+ }
169+
170+ fn check_crate_post ( & mut self , cx : & LateContext < ' _ > ) {
171+ for ( local_def_id, usage) in & self . empty_tuple_enum_variants {
172+ let Usage :: Unused { redundant_use_sites } = usage else {
173+ continue ;
174+ } ;
175+ let Node :: Variant ( variant) = cx. tcx . hir_node (
176+ cx. tcx
177+ . local_def_id_to_hir_id ( cx. tcx . parent ( local_def_id. to_def_id ( ) ) . expect_local ( ) ) ,
178+ ) else {
179+ continue ;
180+ } ;
181+ let span = variant. span . with_lo ( variant. ident . span . hi ( ) ) ;
182+ span_lint_hir_and_then (
105183 cx,
106184 EMPTY_ENUM_VARIANTS_WITH_BRACKETS ,
107- span_after_ident,
185+ variant. hir_id ,
186+ span,
108187 "enum variant has empty brackets" ,
109188 |diagnostic| {
110- diagnostic. span_suggestion_hidden (
111- span_after_ident,
112- "remove the brackets" ,
113- "" ,
114- Applicability :: MaybeIncorrect ,
115- ) ;
189+ if !redundant_use_sites. is_empty ( ) {
190+ let mut parentheses_spans: Vec < _ > =
191+ redundant_use_sites. iter ( ) . map ( |span| ( * span, String :: new ( ) ) ) . collect ( ) ;
192+ parentheses_spans. push ( ( span, String :: new ( ) ) ) ;
193+ diagnostic. multipart_suggestion (
194+ "remove the brackets" ,
195+ parentheses_spans,
196+ Applicability :: MaybeIncorrect ,
197+ ) ;
198+ } else {
199+ diagnostic. span_suggestion_hidden (
200+ span,
201+ "remove the brackets" ,
202+ "" ,
203+ Applicability :: MaybeIncorrect ,
204+ ) ;
205+ }
116206 } ,
117207 ) ;
118208 }
@@ -123,11 +213,11 @@ fn has_no_ident_token(braces_span_str: &str) -> bool {
123213 !rustc_lexer:: tokenize ( braces_span_str) . any ( |t| t. kind == TokenKind :: Ident )
124214}
125215
126- fn has_brackets ( var_data : & VariantData ) -> bool {
127- !matches ! ( var_data, VariantData :: Unit ( _ ) )
216+ fn has_brackets ( var_data : & VariantData < ' _ > ) -> bool {
217+ !matches ! ( var_data, VariantData :: Unit ( .. ) )
128218}
129219
130- fn has_no_fields ( cx : & EarlyContext < ' _ > , var_data : & VariantData , braces_span : Span ) -> bool {
220+ fn has_no_fields ( cx : & LateContext < ' _ > , var_data : & VariantData < ' _ > , braces_span : Span ) -> bool {
131221 if !var_data. fields ( ) . is_empty ( ) {
132222 return false ;
133223 }
@@ -142,6 +232,32 @@ fn has_no_fields(cx: &EarlyContext<'_>, var_data: &VariantData, braces_span: Spa
142232 has_no_ident_token ( braces_span_str. as_ref ( ) )
143233}
144234
235+ fn call_parentheses_span ( tcx : TyCtxt < ' _ > , expr : & Expr < ' _ > ) -> Option < Span > {
236+ if let Node :: Expr ( parent) = tcx. parent_hir_node ( expr. hir_id )
237+ && let ExprKind :: Call ( callee, ..) = parent. kind
238+ && callee. hir_id == expr. hir_id
239+ {
240+ Some ( parent. span . with_lo ( expr. span . hi ( ) ) )
241+ } else {
242+ None
243+ }
244+ }
245+
246+ fn check_expr_for_enum_as_function ( expr : & Expr < ' _ > ) -> Option < LocalDefId > {
247+ if let ExprKind :: Path ( QPath :: Resolved (
248+ _,
249+ Path {
250+ res : Def ( Ctor ( CtorOf :: Variant , _) , def_id) ,
251+ ..
252+ } ,
253+ ) ) = expr. kind
254+ {
255+ def_id. as_local ( )
256+ } else {
257+ None
258+ }
259+ }
260+
145261#[ cfg( test) ]
146262mod unit_test {
147263 use super :: * ;
0 commit comments