Skip to content

Commit 7f185bd

Browse files
committed
Auto merge of #12047 - ARandomDev99:12007-empty-enum-variants-with-brackets, r=Jarcho
New Lint: empty_enum_variants_with_brackets This PR: - adds a new early pass lint that checks for enum variants with no fields that were defined using brackets. **Category: Restriction** - adds relevant UI tests for the new lint. Closes #12007 ``` changelog: New lint: [`empty_enum_variants_with_brackets`] ```
2 parents 0cc53f4 + 5f17188 commit 7f185bd

7 files changed

+148
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5105,6 +5105,7 @@ Released 2018-09-13
51055105
[`else_if_without_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#else_if_without_else
51065106
[`empty_drop`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_drop
51075107
[`empty_enum`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_enum
5108+
[`empty_enum_variants_with_brackets`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_enum_variants_with_brackets
51085109
[`empty_line_after_doc_comments`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_line_after_doc_comments
51095110
[`empty_line_after_outer_attr`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_line_after_outer_attr
51105111
[`empty_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#empty_loop

clippy_lints/src/declared_lints.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
150150
crate::else_if_without_else::ELSE_IF_WITHOUT_ELSE_INFO,
151151
crate::empty_drop::EMPTY_DROP_INFO,
152152
crate::empty_enum::EMPTY_ENUM_INFO,
153-
crate::empty_structs_with_brackets::EMPTY_STRUCTS_WITH_BRACKETS_INFO,
153+
crate::empty_with_brackets::EMPTY_ENUM_VARIANTS_WITH_BRACKETS_INFO,
154+
crate::empty_with_brackets::EMPTY_STRUCTS_WITH_BRACKETS_INFO,
154155
crate::endian_bytes::BIG_ENDIAN_BYTES_INFO,
155156
crate::endian_bytes::HOST_ENDIAN_BYTES_INFO,
156157
crate::endian_bytes::LITTLE_ENDIAN_BYTES_INFO,

clippy_lints/src/empty_structs_with_brackets.rs renamed to clippy_lints/src/empty_with_brackets.rs

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::snippet_opt;
3-
use rustc_ast::ast::{Item, ItemKind, VariantData};
3+
use rustc_ast::ast::{Item, ItemKind, Variant, VariantData};
44
use rustc_errors::Applicability;
55
use rustc_lexer::TokenKind;
66
use 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::MaybeIncorrect,
101+
);
102+
},
103+
);
104+
}
105+
}
56106
}
57107

58108
fn has_no_ident_token(braces_span_str: &str) -> bool {

clippy_lints/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ mod duplicate_mod;
115115
mod else_if_without_else;
116116
mod empty_drop;
117117
mod empty_enum;
118-
mod empty_structs_with_brackets;
118+
mod empty_with_brackets;
119119
mod endian_bytes;
120120
mod entry;
121121
mod enum_clike;
@@ -949,7 +949,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
949949
})
950950
});
951951
store.register_early_pass(|| Box::new(crate_in_macro_def::CrateInMacroDef));
952-
store.register_early_pass(|| Box::new(empty_structs_with_brackets::EmptyStructsWithBrackets));
952+
store.register_early_pass(|| Box::new(empty_with_brackets::EmptyWithBrackets));
953953
store.register_late_pass(|_| Box::new(unnecessary_owned_empty_strings::UnnecessaryOwnedEmptyStrings));
954954
store.register_early_pass(|| Box::new(pub_use::PubUse));
955955
store.register_late_pass(|_| Box::new(format_push_string::FormatPushString));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![warn(clippy::empty_enum_variants_with_brackets)]
2+
#![allow(dead_code)]
3+
4+
pub enum PublicTestEnum {
5+
NonEmptyBraces { x: i32, y: i32 }, // No error
6+
NonEmptyParentheses(i32, i32), // No error
7+
EmptyBraces, //~ ERROR: enum variant has empty brackets
8+
EmptyParentheses, //~ ERROR: enum variant has empty brackets
9+
}
10+
11+
enum TestEnum {
12+
NonEmptyBraces { x: i32, y: i32 }, // No error
13+
NonEmptyParentheses(i32, i32), // No error
14+
EmptyBraces, //~ ERROR: enum variant has empty brackets
15+
EmptyParentheses, //~ ERROR: enum variant has empty brackets
16+
AnotherEnum, // No error
17+
}
18+
19+
enum TestEnumWithFeatures {
20+
NonEmptyBraces {
21+
#[cfg(feature = "thisisneverenabled")]
22+
x: i32,
23+
}, // No error
24+
NonEmptyParentheses(#[cfg(feature = "thisisneverenabled")] i32), // No error
25+
}
26+
27+
fn main() {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![warn(clippy::empty_enum_variants_with_brackets)]
2+
#![allow(dead_code)]
3+
4+
pub enum PublicTestEnum {
5+
NonEmptyBraces { x: i32, y: i32 }, // No error
6+
NonEmptyParentheses(i32, i32), // No error
7+
EmptyBraces {}, //~ ERROR: enum variant has empty brackets
8+
EmptyParentheses(), //~ ERROR: enum variant has empty brackets
9+
}
10+
11+
enum TestEnum {
12+
NonEmptyBraces { x: i32, y: i32 }, // No error
13+
NonEmptyParentheses(i32, i32), // No error
14+
EmptyBraces {}, //~ ERROR: enum variant has empty brackets
15+
EmptyParentheses(), //~ ERROR: enum variant has empty brackets
16+
AnotherEnum, // No error
17+
}
18+
19+
enum TestEnumWithFeatures {
20+
NonEmptyBraces {
21+
#[cfg(feature = "thisisneverenabled")]
22+
x: i32,
23+
}, // No error
24+
NonEmptyParentheses(#[cfg(feature = "thisisneverenabled")] i32), // No error
25+
}
26+
27+
fn main() {}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
error: enum variant has empty brackets
2+
--> $DIR/empty_enum_variants_with_brackets.rs:7:16
3+
|
4+
LL | EmptyBraces {},
5+
| ^^^
6+
|
7+
= note: `-D clippy::empty-enum-variants-with-brackets` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::empty_enum_variants_with_brackets)]`
9+
= help: remove the brackets
10+
11+
error: enum variant has empty brackets
12+
--> $DIR/empty_enum_variants_with_brackets.rs:8:21
13+
|
14+
LL | EmptyParentheses(),
15+
| ^^
16+
|
17+
= help: remove the brackets
18+
19+
error: enum variant has empty brackets
20+
--> $DIR/empty_enum_variants_with_brackets.rs:14:16
21+
|
22+
LL | EmptyBraces {},
23+
| ^^^
24+
|
25+
= help: remove the brackets
26+
27+
error: enum variant has empty brackets
28+
--> $DIR/empty_enum_variants_with_brackets.rs:15:21
29+
|
30+
LL | EmptyParentheses(),
31+
| ^^
32+
|
33+
= help: remove the brackets
34+
35+
error: aborting due to 4 previous errors
36+

0 commit comments

Comments
 (0)