|
1 | 1 | //! lint on enum variants that are prefixed or suffixed by the same characters
|
2 | 2 |
|
3 | 3 | use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_hir};
|
4 |
| -use clippy_utils::is_bool; |
5 | 4 | use clippy_utils::macros::span_is_local;
|
6 | 5 | use clippy_utils::source::is_present_in_source;
|
7 | 6 | use clippy_utils::str_utils::{camel_case_split, count_match_end, count_match_start, to_camel_case, to_snake_case};
|
| 7 | +use clippy_utils::{any_impl_has_lint_allowed, is_bool}; |
8 | 8 | use rustc_hir::{EnumDef, FieldDef, Item, ItemKind, OwnerId, Variant, VariantData};
|
9 | 9 | use rustc_lint::{LateContext, LateLintPass};
|
10 | 10 | use rustc_session::impl_lint_pass;
|
| 11 | +use rustc_span::def_id::DefId; |
11 | 12 | use rustc_span::symbol::Symbol;
|
12 | 13 | use rustc_span::Span;
|
13 | 14 |
|
@@ -180,7 +181,8 @@ fn have_no_extra_prefix(prefixes: &[&str]) -> bool {
|
180 | 181 | }
|
181 | 182 |
|
182 | 183 | fn check_fields(cx: &LateContext<'_>, threshold: u64, item: &Item<'_>, fields: &[FieldDef<'_>]) {
|
183 |
| - if (fields.len() as u64) < threshold { |
| 184 | + if (fields.len() as u64) < threshold || any_impl_has_lint_allowed(cx, STRUCT_FIELD_NAMES, item.owner_id.to_def_id()) |
| 185 | + { |
184 | 186 | return;
|
185 | 187 | }
|
186 | 188 |
|
@@ -320,8 +322,15 @@ fn check_enum_end(cx: &LateContext<'_>, item_name: &str, variant: &Variant<'_>)
|
320 | 322 | }
|
321 | 323 | }
|
322 | 324 |
|
323 |
| -fn check_variant(cx: &LateContext<'_>, threshold: u64, def: &EnumDef<'_>, item_name: &str, span: Span) { |
324 |
| - if (def.variants.len() as u64) < threshold { |
| 325 | +fn check_variant( |
| 326 | + cx: &LateContext<'_>, |
| 327 | + threshold: u64, |
| 328 | + def: &EnumDef<'_>, |
| 329 | + item_name: &str, |
| 330 | + item_did: DefId, |
| 331 | + span: Span, |
| 332 | +) { |
| 333 | + if (def.variants.len() as u64) < threshold || any_impl_has_lint_allowed(cx, ENUM_VARIANT_NAMES, item_did) { |
325 | 334 | return;
|
326 | 335 | }
|
327 | 336 |
|
@@ -388,6 +397,7 @@ impl LateLintPass<'_> for ItemNameRepetitions {
|
388 | 397 | #[expect(clippy::similar_names)]
|
389 | 398 | fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
|
390 | 399 | let item_name = item.ident.name.as_str();
|
| 400 | + |
391 | 401 | let item_camel = to_camel_case(item_name);
|
392 | 402 | if !item.span.from_expansion() && is_present_in_source(cx, item.span) {
|
393 | 403 | if let [.., (mod_name, mod_camel, owner_id)] = &*self.modules {
|
@@ -440,7 +450,14 @@ impl LateLintPass<'_> for ItemNameRepetitions {
|
440 | 450 | && span_is_local(item.span)
|
441 | 451 | {
|
442 | 452 | match item.kind {
|
443 |
| - ItemKind::Enum(def, _) => check_variant(cx, self.enum_threshold, &def, item_name, item.span), |
| 453 | + ItemKind::Enum(def, _) => check_variant( |
| 454 | + cx, |
| 455 | + self.enum_threshold, |
| 456 | + &def, |
| 457 | + item_name, |
| 458 | + item.owner_id.to_def_id(), |
| 459 | + item.span, |
| 460 | + ), |
444 | 461 | ItemKind::Struct(VariantData::Struct { fields, .. }, _) => {
|
445 | 462 | check_fields(cx, self.struct_threshold, item, fields);
|
446 | 463 | },
|
|
0 commit comments