Skip to content

Commit 9a3cdbe

Browse files
author
Alexis Langlet
committed
feat: prevent lint triggering when impl allows it
1 parent c9c49e3 commit 9a3cdbe

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

clippy_lints/src/item_name_repetitions.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
//! lint on enum variants that are prefixed or suffixed by the same characters
22
33
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_hir};
4-
use clippy_utils::is_bool;
54
use clippy_utils::macros::span_is_local;
65
use clippy_utils::source::is_present_in_source;
76
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};
88
use rustc_hir::{EnumDef, FieldDef, Item, ItemKind, OwnerId, Variant, VariantData};
99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_session::impl_lint_pass;
11+
use rustc_span::def_id::DefId;
1112
use rustc_span::symbol::Symbol;
1213
use rustc_span::Span;
1314

@@ -180,7 +181,8 @@ fn have_no_extra_prefix(prefixes: &[&str]) -> bool {
180181
}
181182

182183
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+
{
184186
return;
185187
}
186188

@@ -320,8 +322,15 @@ fn check_enum_end(cx: &LateContext<'_>, item_name: &str, variant: &Variant<'_>)
320322
}
321323
}
322324

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) {
325334
return;
326335
}
327336

@@ -388,6 +397,7 @@ impl LateLintPass<'_> for ItemNameRepetitions {
388397
#[expect(clippy::similar_names)]
389398
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
390399
let item_name = item.ident.name.as_str();
400+
391401
let item_camel = to_camel_case(item_name);
392402
if !item.span.from_expansion() && is_present_in_source(cx, item.span) {
393403
if let [.., (mod_name, mod_camel, owner_id)] = &*self.modules {
@@ -440,7 +450,14 @@ impl LateLintPass<'_> for ItemNameRepetitions {
440450
&& span_is_local(item.span)
441451
{
442452
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+
),
444461
ItemKind::Struct(VariantData::Struct { fields, .. }, _) => {
445462
check_fields(cx, self.struct_threshold, item, fields);
446463
},

tests/ui-toml/item_name_repetitions/threshold5/item_name_repetitions.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,28 @@ enum Foo2 {
2929
EFoo,
3030
}
3131

32+
// This should not trigger the lint as one of it's impl has allow attribute
33+
struct Data3 {
34+
a_data: u8,
35+
b_data: u8,
36+
c_data: u8,
37+
d_data: u8,
38+
e_data: u8,
39+
}
40+
41+
#[allow(clippy::struct_field_names)]
42+
impl Data3 {}
43+
44+
// This should not trigger the lint as one of it's impl has allow attribute
45+
enum Foo3 {
46+
AFoo,
47+
BFoo,
48+
CFoo,
49+
DFoo,
50+
EFoo,
51+
}
52+
53+
#[allow(clippy::enum_variant_names)]
54+
impl Foo3 {}
55+
3256
fn main() {}

0 commit comments

Comments
 (0)