Skip to content

Commit 97716b3

Browse files
authored
Merge pull request #1147 from birkenfeld/issue-1138
Add a configurable threshold for enum variants before name lints trigger
2 parents 78fa1ab + fa0df69 commit 97716b3

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

clippy_lints/src/enum_variants.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,15 @@ declare_lint! {
4444
"finds type names prefixed/postfixed with their containing module's name"
4545
}
4646

47-
#[derive(Default)]
4847
pub struct EnumVariantNames {
4948
modules: Vec<String>,
49+
threshold: u64,
50+
}
51+
52+
impl EnumVariantNames {
53+
pub fn new(threshold: u64) -> EnumVariantNames {
54+
EnumVariantNames { modules: Vec::new(), threshold: threshold }
55+
}
5056
}
5157

5258
impl LintPass for EnumVariantNames {
@@ -75,7 +81,11 @@ fn partial_rmatch(post: &str, name: &str) -> usize {
7581

7682
// FIXME: #600
7783
#[allow(while_let_on_iterator)]
78-
fn check_variant(cx: &EarlyContext, def: &EnumDef, item_name: &str, item_name_chars: usize, span: Span) {
84+
fn check_variant(cx: &EarlyContext, threshold: u64, def: &EnumDef, item_name: &str,
85+
item_name_chars: usize, span: Span) {
86+
if (def.variants.len() as u64) < threshold {
87+
return;
88+
}
7989
for var in &def.variants {
8090
let name = var2str(var);
8191
if partial_match(item_name, &name) == item_name_chars {
@@ -85,9 +95,6 @@ fn check_variant(cx: &EarlyContext, def: &EnumDef, item_name: &str, item_name_ch
8595
span_lint(cx, ENUM_VARIANT_NAMES, var.span, "Variant name ends with the enum's name");
8696
}
8797
}
88-
if def.variants.len() < 2 {
89-
return;
90-
}
9198
let first = var2str(&def.variants[0]);
9299
let mut pre = &first[..camel_case_until(&*first)];
93100
let mut post = &first[camel_case_from(&*first)..];
@@ -177,7 +184,7 @@ impl EarlyLintPass for EnumVariantNames {
177184
}
178185
}
179186
if let ItemKind::Enum(ref def, _) = item.node {
180-
check_variant(cx, def, &item_name, item_name_chars, item.span);
187+
check_variant(cx, self.threshold, def, &item_name, item_name_chars, item.span);
181188
}
182189
self.modules.push(item_camel);
183190
}

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
175175
reg.register_late_lint_pass(box misc::TopLevelRefPass);
176176
reg.register_late_lint_pass(box misc::CmpNan);
177177
reg.register_late_lint_pass(box eq_op::EqOp);
178-
reg.register_early_lint_pass(box enum_variants::EnumVariantNames::default());
178+
reg.register_early_lint_pass(box enum_variants::EnumVariantNames::new(conf.enum_variant_name_threshold));
179179
reg.register_late_lint_pass(box enum_glob_use::EnumGlobUse);
180180
reg.register_late_lint_pass(box enum_clike::UnportableVariant);
181181
reg.register_late_lint_pass(box bit_mask::BitMask);

clippy_lints/src/utils/conf.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ define_Conf! {
160160
("single-char-binding-names-threshold", max_single_char_names, 5 => u64),
161161
/// Lint: BOXED_LOCAL. The maximum size of objects (in bytes) that will be linted. Larger objects are ok on the heap
162162
("too-large-for-stack", too_large_for_stack, 200 => u64),
163+
/// Lint: ENUM_VARIANT_NAMES. The minimum number of enum variants for the lints about variant names to trigger
164+
("enum-variant-name-threshold", enum_variant_name_threshold, 3 => u64),
163165
}
164166

165167
/// Read the `toml` configuration file. The function will ignore “File not found” errors iif

tests/compile-fail/enum_variants.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@ enum FakeCallType2 {
1313
enum Foo {
1414
cFoo, //~ ERROR: Variant name ends with the enum's name
1515
cBar,
16+
cBaz,
17+
}
18+
19+
enum Fooo {
20+
cFoo, // no error, threshold is 3 variants by default
21+
cBar,
22+
}
23+
24+
enum Food { //~ ERROR: All variants have the same prefix: `Food`
25+
FoodGood, //~ ERROR: Variant name starts with the enum's name
26+
FoodMiddle, //~ ERROR: Variant name starts with the enum's name
27+
FoodBad, //~ ERROR: Variant name starts with the enum's name
28+
}
29+
30+
enum Stuff {
31+
StuffBad, // no error
1632
}
1733

1834
enum BadCallType { //~ ERROR: All variants have the same prefix: `CallType`
@@ -21,7 +37,7 @@ enum BadCallType { //~ ERROR: All variants have the same prefix: `CallType`
2137
CallTypeDestroy,
2238
}
2339

24-
enum TwoCallType { //~ ERROR: All variants have the same prefix: `CallType`
40+
enum TwoCallType { // no error
2541
CallTypeCall,
2642
CallTypeCreate,
2743
}
@@ -32,7 +48,7 @@ enum Consts { //~ ERROR: All variants have the same prefix: `Constant`
3248
ConstantLie,
3349
}
3450

35-
enum Two { //~ ERROR: All variants have the same prefix: `Constant`
51+
enum Two { // no error here
3652
ConstantInt,
3753
ConstantInfer,
3854
}
@@ -61,20 +77,14 @@ enum Sealll {
6177

6278
enum Seallll { //~ ERROR: All variants have the same prefix: `With`
6379
WithOutCake,
80+
WithOutTea,
6481
WithOut,
6582
}
6683

6784
enum NonCaps { //~ ERROR: All variants have the same prefix: `Prefix`
6885
Prefix的,
86+
PrefixTea,
6987
PrefixCake,
7088
}
7189

72-
enum Stuff {
73-
BadStuff, //~ ERROR: Variant name ends with the enum's name
74-
}
75-
76-
enum Food {
77-
FoodGood, //~ ERROR: Variant name starts with the enum's name
78-
}
79-
8090
fn main() {}

0 commit comments

Comments
 (0)