1
1
use clippy_utils:: diagnostics:: span_lint_and_then;
2
2
use clippy_utils:: source:: snippet_opt;
3
- use rustc_ast:: ast:: { Item , ItemKind , VariantData } ;
3
+ use rustc_ast:: ast:: { Item , ItemKind , Variant , VariantData } ;
4
4
use rustc_errors:: Applicability ;
5
5
use rustc_lexer:: TokenKind ;
6
6
use rustc_lint:: { EarlyContext , EarlyLintPass } ;
@@ -27,9 +27,38 @@ declare_clippy_lint! {
27
27
restriction,
28
28
"finds struct declarations with empty brackets"
29
29
}
30
- declare_lint_pass ! ( EmptyStructsWithBrackets => [ EMPTY_STRUCTS_WITH_BRACKETS ] ) ;
31
30
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 {
33
62
fn check_item ( & mut self , cx : & EarlyContext < ' _ > , item : & Item ) {
34
63
let span_after_ident = item. span . with_lo ( item. ident . span . hi ( ) ) ;
35
64
@@ -53,6 +82,27 @@ impl EarlyLintPass for EmptyStructsWithBrackets {
53
82
) ;
54
83
}
55
84
}
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
+ }
56
106
}
57
107
58
108
fn has_no_ident_token ( braces_span_str : & str ) -> bool {
0 commit comments