Skip to content

Commit 286d528

Browse files
committed
Auto merge of #4345 - phansch:enum_variants_fix, r=flip1995
Don't emit enum_variant_names if remainder starts with a numeric changelog: Fix false positive in `pub_enum_variant_names` and `enum_variant_names` As [per the reference](https://doc.rust-lang.org/reference/identifiers.html), identifiers must start with a letter. So we don't suggest a better variant naming in case the remainder would start with a numeric. Fixes #739
2 parents 8fed08d + 0a988c6 commit 286d528

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

clippy_lints/src/enum_variants.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ fn check_variant(
160160
let name = var2str(var);
161161
if partial_match(item_name, &name) == item_name_chars
162162
&& name.chars().nth(item_name_chars).map_or(false, |c| !c.is_lowercase())
163+
&& name.chars().nth(item_name_chars + 1).map_or(false, |c| !c.is_numeric())
163164
{
164165
span_lint(cx, lint, var.span, "Variant name starts with the enum's name");
165166
}
@@ -178,6 +179,9 @@ fn check_variant(
178179
let pre_camel = camel_case::until(pre);
179180
pre = &pre[..pre_camel];
180181
while let Some((next, last)) = name[pre.len()..].chars().zip(pre.chars().rev()).next() {
182+
if next.is_numeric() {
183+
return;
184+
}
181185
if next.is_lowercase() {
182186
let last = pre.len() - last.len_utf8();
183187
let last_camel = camel_case::until(&pre[..last]);

tests/ui/enum_variants.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![feature(non_ascii_idents)]
2-
#![warn(clippy::all, clippy::pub_enum_variant_names)]
2+
#![warn(clippy::enum_variant_names, clippy::pub_enum_variant_names)]
33
#![allow(non_camel_case_types)]
44

55
enum FakeCallType {
@@ -120,4 +120,17 @@ enum N {
120120
Float,
121121
}
122122

123+
// should not lint
124+
enum Peek {
125+
Peek1,
126+
Peek2,
127+
Peek3,
128+
}
129+
130+
// should not lint
131+
pub enum NetworkLayer {
132+
Layer2,
133+
Layer3,
134+
}
135+
123136
fn main() {}

0 commit comments

Comments
 (0)