From 2526765fc83d0591828e95c7a621ee672244de12 Mon Sep 17 00:00:00 2001 From: Seo Sanghyeon Date: Thu, 15 Feb 2024 06:22:15 +0900 Subject: [PATCH] Check trait items --- clippy_lints/src/min_ident_chars.rs | 12 +++++-- tests/ui/min_ident_chars.rs | 6 ++++ tests/ui/min_ident_chars.stderr | 56 +++++++++++++++++++---------- 3 files changed, 53 insertions(+), 21 deletions(-) diff --git a/clippy_lints/src/min_ident_chars.rs b/clippy_lints/src/min_ident_chars.rs index 79883c4ec77..ec07f283c46 100644 --- a/clippy_lints/src/min_ident_chars.rs +++ b/clippy_lints/src/min_ident_chars.rs @@ -2,8 +2,8 @@ use clippy_utils::is_from_proc_macro; use rustc_data_structures::fx::FxHashSet; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::intravisit::{walk_item, Visitor}; -use rustc_hir::{GenericParamKind, HirId, Item, ItemKind, ItemLocalId, Node, Pat, PatKind, UsePath}; +use rustc_hir::intravisit::{walk_item, walk_trait_item, Visitor}; +use rustc_hir::{GenericParamKind, HirId, Item, ItemKind, ItemLocalId, Node, Pat, PatKind, TraitItem, UsePath}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; use rustc_session::impl_lint_pass; @@ -66,6 +66,14 @@ fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { walk_item(&mut IdentVisitor { conf: self, cx }, item); } + fn check_trait_item(&mut self, cx: &LateContext<'_>, item: &TraitItem<'_>) { + if self.min_ident_chars_threshold == 0 { + return; + } + + walk_trait_item(&mut IdentVisitor { conf: self, cx }, item); + } + // This is necessary as `Node::Pat`s are not visited in `visit_id`. :/ fn check_pat(&mut self, cx: &LateContext<'_>, pat: &Pat<'_>) { if let PatKind::Binding(_, _, ident, ..) = pat.kind diff --git a/tests/ui/min_ident_chars.rs b/tests/ui/min_ident_chars.rs index f99c35d5c57..1d89f26b6ee 100644 --- a/tests/ui/min_ident_chars.rs +++ b/tests/ui/min_ident_chars.rs @@ -37,6 +37,12 @@ struct Vec4 { struct AA(T, E); +trait Trait { + const A: u32 = 0; + type A; + fn a() {} +} + fn main() { // Allowed idents let w = 1; diff --git a/tests/ui/min_ident_chars.stderr b/tests/ui/min_ident_chars.stderr index e4181157ea2..7490878d572 100644 --- a/tests/ui/min_ident_chars.stderr +++ b/tests/ui/min_ident_chars.stderr @@ -68,112 +68,130 @@ LL | F, | ^ error: this ident consists of a single char - --> $DIR/min_ident_chars.rs:51:9 + --> $DIR/min_ident_chars.rs:41:11 + | +LL | const A: u32 = 0; + | ^ + +error: this ident consists of a single char + --> $DIR/min_ident_chars.rs:42:10 + | +LL | type A; + | ^ + +error: this ident consists of a single char + --> $DIR/min_ident_chars.rs:43:8 + | +LL | fn a() {} + | ^ + +error: this ident consists of a single char + --> $DIR/min_ident_chars.rs:57:9 | LL | let h = 1; | ^ error: this ident consists of a single char - --> $DIR/min_ident_chars.rs:52:9 + --> $DIR/min_ident_chars.rs:58:9 | LL | let e = 2; | ^ error: this ident consists of a single char - --> $DIR/min_ident_chars.rs:53:9 + --> $DIR/min_ident_chars.rs:59:9 | LL | let l = 3; | ^ error: this ident consists of a single char - --> $DIR/min_ident_chars.rs:54:9 + --> $DIR/min_ident_chars.rs:60:9 | LL | let l = 4; | ^ error: this ident consists of a single char - --> $DIR/min_ident_chars.rs:55:9 + --> $DIR/min_ident_chars.rs:61:9 | LL | let o = 6; | ^ error: this ident consists of a single char - --> $DIR/min_ident_chars.rs:59:10 + --> $DIR/min_ident_chars.rs:65:10 | LL | let (h, o, w) = (1, 2, 3); | ^ error: this ident consists of a single char - --> $DIR/min_ident_chars.rs:59:13 + --> $DIR/min_ident_chars.rs:65:13 | LL | let (h, o, w) = (1, 2, 3); | ^ error: this ident consists of a single char - --> $DIR/min_ident_chars.rs:60:10 + --> $DIR/min_ident_chars.rs:66:10 | LL | for (a, (r, e)) in (0..1000).enumerate().enumerate() {} | ^ error: this ident consists of a single char - --> $DIR/min_ident_chars.rs:60:14 + --> $DIR/min_ident_chars.rs:66:14 | LL | for (a, (r, e)) in (0..1000).enumerate().enumerate() {} | ^ error: this ident consists of a single char - --> $DIR/min_ident_chars.rs:60:17 + --> $DIR/min_ident_chars.rs:66:17 | LL | for (a, (r, e)) in (0..1000).enumerate().enumerate() {} | ^ error: this ident consists of a single char - --> $DIR/min_ident_chars.rs:62:16 + --> $DIR/min_ident_chars.rs:68:16 | LL | while let (d, o, _i, n, g) = (true, true, false, false, true) {} | ^ error: this ident consists of a single char - --> $DIR/min_ident_chars.rs:62:19 + --> $DIR/min_ident_chars.rs:68:19 | LL | while let (d, o, _i, n, g) = (true, true, false, false, true) {} | ^ error: this ident consists of a single char - --> $DIR/min_ident_chars.rs:62:29 + --> $DIR/min_ident_chars.rs:68:29 | LL | while let (d, o, _i, n, g) = (true, true, false, false, true) {} | ^ error: this ident consists of a single char - --> $DIR/min_ident_chars.rs:66:9 + --> $DIR/min_ident_chars.rs:72:9 | LL | let o = 1; | ^ error: this ident consists of a single char - --> $DIR/min_ident_chars.rs:67:9 + --> $DIR/min_ident_chars.rs:73:9 | LL | let o = O { o }; | ^ error: this ident consists of a single char - --> $DIR/min_ident_chars.rs:81:4 + --> $DIR/min_ident_chars.rs:87:4 | LL | fn b() {} | ^ error: this ident consists of a single char - --> $DIR/min_ident_chars.rs:82:21 + --> $DIR/min_ident_chars.rs:88:21 | LL | fn wrong_pythagoras(a: f32, b: f32) -> f32 { | ^ error: this ident consists of a single char - --> $DIR/min_ident_chars.rs:82:29 + --> $DIR/min_ident_chars.rs:88:29 | LL | fn wrong_pythagoras(a: f32, b: f32) -> f32 { | ^ -error: aborting due to 29 previous errors +error: aborting due to 32 previous errors