From ab645bb08177723bf926a6ee2724b5b653fb5d4e Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Wed, 22 Jun 2022 07:59:14 +0900 Subject: [PATCH 1/3] enum_variant_names should ignore when all prefixes are _ --- clippy_lints/src/enum_variants.rs | 7 ++++++- tests/ui/enum_variants.rs | 21 +++++++++++++++++++++ tests/ui/enum_variants.stderr | 27 ++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/enum_variants.rs b/clippy_lints/src/enum_variants.rs index 23b751045..517e11b4e 100644 --- a/clippy_lints/src/enum_variants.rs +++ b/clippy_lints/src/enum_variants.rs @@ -190,7 +190,7 @@ fn check_variant(cx: &LateContext<'_>, threshold: u64, def: &EnumDef<'_>, item_n .map(|e| *e.0) .collect(); } - let (what, value) = match (pre.is_empty(), post.is_empty()) { + let (what, value) = match (have_no_extra_prefix(&pre), post.is_empty()) { (true, true) => return, (false, _) => ("pre", pre.join("")), (true, false) => { @@ -212,6 +212,11 @@ fn check_variant(cx: &LateContext<'_>, threshold: u64, def: &EnumDef<'_>, item_n ); } +#[must_use] +fn have_no_extra_prefix(prefixes: &Vec<&str>) -> bool { + prefixes.is_empty() || prefixes.join("") == "_" +} + #[must_use] fn to_camel_case(item_name: &str) -> String { let mut s = String::new(); diff --git a/tests/ui/enum_variants.rs b/tests/ui/enum_variants.rs index b2bf7c4e3..efed12ee2 100644 --- a/tests/ui/enum_variants.rs +++ b/tests/ui/enum_variants.rs @@ -158,4 +158,25 @@ enum Phase { PostLookup, } +mod issue9018 { + enum DoLint { + _TypeCreate, + _TypeRead, + _TypeUpdate, + _TypeDestroy, + } + + enum DoLintToo { + _CreateType, + _UpdateType, + _DeleteType, + } + + enum DoNotLint { + _Foo, + _Bar, + _Baz, + } +} + fn main() {} diff --git a/tests/ui/enum_variants.stderr b/tests/ui/enum_variants.stderr index 8a3265086..7342aff80 100644 --- a/tests/ui/enum_variants.stderr +++ b/tests/ui/enum_variants.stderr @@ -120,5 +120,30 @@ LL | | } | = help: remove the postfixes and use full paths to the variants instead of glob imports -error: aborting due to 12 previous errors +error: all variants have the same prefix: `_Type` + --> $DIR/enum_variants.rs:162:5 + | +LL | / enum DoLint { +LL | | _TypeCreate, +LL | | _TypeRead, +LL | | _TypeUpdate, +LL | | _TypeDestroy, +LL | | } + | |_____^ + | + = help: remove the prefixes and use full paths to the variants instead of glob imports + +error: all variants have the same postfix: `Type` + --> $DIR/enum_variants.rs:169:5 + | +LL | / enum DoLintToo { +LL | | _CreateType, +LL | | _UpdateType, +LL | | _DeleteType, +LL | | } + | |_____^ + | + = help: remove the postfixes and use full paths to the variants instead of glob imports + +error: aborting due to 14 previous errors From bf317a83cf2c409548168a13f243973389bbb851 Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Sun, 26 Jun 2022 15:55:44 +0900 Subject: [PATCH 2/3] treat argument as a slice. Co-authored-by: llogiq --- clippy_lints/src/enum_variants.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/enum_variants.rs b/clippy_lints/src/enum_variants.rs index 517e11b4e..99d5da2c2 100644 --- a/clippy_lints/src/enum_variants.rs +++ b/clippy_lints/src/enum_variants.rs @@ -213,7 +213,7 @@ fn check_variant(cx: &LateContext<'_>, threshold: u64, def: &EnumDef<'_>, item_n } #[must_use] -fn have_no_extra_prefix(prefixes: &Vec<&str>) -> bool { +fn have_no_extra_prefix(prefixes: &[&str]) -> bool { prefixes.is_empty() || prefixes.join("") == "_" } From d827b834d91383620d587fb4a1e74d8579e3a280 Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Sun, 26 Jun 2022 16:11:04 +0900 Subject: [PATCH 3/3] use all instead of join --- clippy_lints/src/enum_variants.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/enum_variants.rs b/clippy_lints/src/enum_variants.rs index 99d5da2c2..cd36f9fcd 100644 --- a/clippy_lints/src/enum_variants.rs +++ b/clippy_lints/src/enum_variants.rs @@ -214,7 +214,7 @@ fn check_variant(cx: &LateContext<'_>, threshold: u64, def: &EnumDef<'_>, item_n #[must_use] fn have_no_extra_prefix(prefixes: &[&str]) -> bool { - prefixes.is_empty() || prefixes.join("") == "_" + prefixes.iter().all(|p| p == &"" || p == &"_") } #[must_use]