From f3a27d20f60a7b0f23b9256ef6f405c4da8106fd Mon Sep 17 00:00:00 2001 From: jonboh Date: Fri, 22 Sep 2023 17:18:49 +0200 Subject: [PATCH 1/3] prevent ice when threshold is 0 and enum has no variants --- clippy_lints/src/enum_variants.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/enum_variants.rs b/clippy_lints/src/enum_variants.rs index d4df6f7aa2d0..85334d09d4b4 100644 --- a/clippy_lints/src/enum_variants.rs +++ b/clippy_lints/src/enum_variants.rs @@ -167,7 +167,10 @@ fn check_variant(cx: &LateContext<'_>, threshold: u64, def: &EnumDef<'_>, item_n return; } - let first = &def.variants[0].ident.name.as_str(); + let first = match &def.variants.get(0) { + Some(variant) => variant.ident.name.as_str(), + None => return, + }; let mut pre = camel_case_split(first); let mut post = pre.clone(); post.reverse(); From b3e262acfd711f27c7450379d85092ec169388a7 Mon Sep 17 00:00:00 2001 From: jonboh Date: Sun, 24 Sep 2023 18:25:20 +0200 Subject: [PATCH 2/3] add ui-toml test --- tests/ui-toml/enum_variants_threshold0/clippy.toml | 1 + .../enum_variants_threshold0/enum_variants_name_threshold.rs | 3 +++ 2 files changed, 4 insertions(+) create mode 100644 tests/ui-toml/enum_variants_threshold0/clippy.toml create mode 100644 tests/ui-toml/enum_variants_threshold0/enum_variants_name_threshold.rs diff --git a/tests/ui-toml/enum_variants_threshold0/clippy.toml b/tests/ui-toml/enum_variants_threshold0/clippy.toml new file mode 100644 index 000000000000..f85aade6ae87 --- /dev/null +++ b/tests/ui-toml/enum_variants_threshold0/clippy.toml @@ -0,0 +1 @@ +enum-variant-name-threshold = 0 diff --git a/tests/ui-toml/enum_variants_threshold0/enum_variants_name_threshold.rs b/tests/ui-toml/enum_variants_threshold0/enum_variants_name_threshold.rs new file mode 100644 index 000000000000..6918d7528c16 --- /dev/null +++ b/tests/ui-toml/enum_variants_threshold0/enum_variants_name_threshold.rs @@ -0,0 +1,3 @@ +enum Actions {} + +fn main() {} From 0433e458dadc927e645f6307c0253a4b241dfd5b Mon Sep 17 00:00:00 2001 From: Jon Date: Sun, 24 Sep 2023 21:37:56 +0200 Subject: [PATCH 3/3] use first instead of get(0) Co-authored-by: Timo <30553356+y21@users.noreply.github.com> --- 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 85334d09d4b4..e332f681b6db 100644 --- a/clippy_lints/src/enum_variants.rs +++ b/clippy_lints/src/enum_variants.rs @@ -167,7 +167,7 @@ fn check_variant(cx: &LateContext<'_>, threshold: u64, def: &EnumDef<'_>, item_n return; } - let first = match &def.variants.get(0) { + let first = match def.variants.first() { Some(variant) => variant.ident.name.as_str(), None => return, };