11use clippy_utils:: diagnostics:: span_lint_and_then;
22use clippy_utils:: source:: snippet_opt;
3- use rustc_ast:: ast:: { Item , ItemKind , VariantData } ;
3+ use rustc_ast:: ast:: { Item , ItemKind , Variant , VariantData } ;
44use rustc_errors:: Applicability ;
55use rustc_lexer:: TokenKind ;
66use rustc_lint:: { EarlyContext , EarlyLintPass } ;
@@ -27,9 +27,38 @@ declare_clippy_lint! {
2727 restriction,
2828 "finds struct declarations with empty brackets"
2929}
30- declare_lint_pass ! ( EmptyStructsWithBrackets => [ EMPTY_STRUCTS_WITH_BRACKETS ] ) ;
3130
32- impl EarlyLintPass for EmptyStructsWithBrackets {
31+ declare_clippy_lint ! {
32+ /// ### What it does
33+ /// Finds enum variants without fields that are declared with empty brackets.
34+ ///
35+ /// ### Why is this bad?
36+ /// Empty brackets while defining enum variants are redundant and can be omitted.
37+ ///
38+ /// ### Example
39+ /// ```no_run
40+ /// enum MyEnum {
41+ /// HasData(u8),
42+ /// HasNoData(), // redundant parentheses
43+ /// }
44+ /// ```
45+ ///
46+ /// Use instead:
47+ /// ```no_run
48+ /// enum MyEnum {
49+ /// HasData(u8),
50+ /// HasNoData,
51+ /// }
52+ /// ```
53+ #[ clippy:: version = "1.77.0" ]
54+ pub EMPTY_ENUM_VARIANTS_WITH_BRACKETS ,
55+ restriction,
56+ "finds enum variants with empty brackets"
57+ }
58+
59+ declare_lint_pass ! ( EmptyWithBrackets => [ EMPTY_STRUCTS_WITH_BRACKETS , EMPTY_ENUM_VARIANTS_WITH_BRACKETS ] ) ;
60+
61+ impl EarlyLintPass for EmptyWithBrackets {
3362 fn check_item ( & mut self , cx : & EarlyContext < ' _ > , item : & Item ) {
3463 let span_after_ident = item. span . with_lo ( item. ident . span . hi ( ) ) ;
3564
@@ -53,6 +82,27 @@ impl EarlyLintPass for EmptyStructsWithBrackets {
5382 ) ;
5483 }
5584 }
85+
86+ fn check_variant ( & mut self , cx : & EarlyContext < ' _ > , variant : & Variant ) {
87+ let span_after_ident = variant. span . with_lo ( variant. ident . span . hi ( ) ) ;
88+
89+ if has_brackets ( & variant. data ) && has_no_fields ( cx, & variant. data , span_after_ident) {
90+ span_lint_and_then (
91+ cx,
92+ EMPTY_ENUM_VARIANTS_WITH_BRACKETS ,
93+ span_after_ident,
94+ "enum variant has empty brackets" ,
95+ |diagnostic| {
96+ diagnostic. span_suggestion_hidden (
97+ span_after_ident,
98+ "remove the brackets" ,
99+ "" ,
100+ Applicability :: MachineApplicable ,
101+ ) ;
102+ } ,
103+ ) ;
104+ }
105+ }
56106}
57107
58108fn has_no_ident_token ( braces_span_str : & str ) -> bool {
0 commit comments