Skip to content

Commit b6cc2fb

Browse files
committed
make it work for locals as well
oopos
1 parent 3defd38 commit b6cc2fb

File tree

6 files changed

+196
-23
lines changed

6 files changed

+196
-23
lines changed

clippy_lints/src/min_ident_chars.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_data_structures::fx::FxHashSet;
33
use rustc_hir::{
44
def::{DefKind, Res},
55
intravisit::{walk_item, Visitor},
6-
GenericParamKind, HirId, Item, ItemKind, ItemLocalId, Node,
6+
GenericParamKind, HirId, Item, ItemKind, ItemLocalId, Node, Pat, PatKind,
77
};
88
use rustc_lint::{LateContext, LateLintPass, LintContext};
99
use rustc_middle::lint::in_external_macro;
@@ -55,15 +55,38 @@ impl LateLintPass<'_> for MinIdentChars {
5555

5656
walk_item(&mut IdentVisitor { conf: self, cx }, item);
5757
}
58+
59+
// This is necessary as bindings are not visited in `visit_id`. :/
60+
#[expect(clippy::cast_possible_truncation)]
61+
fn check_pat(&mut self, cx: &LateContext<'_>, pat: &Pat<'_>) {
62+
if let PatKind::Binding(_, _, ident, ..) = pat.kind
63+
&& let str = ident.as_str()
64+
&& !in_external_macro(cx.sess(), ident.span)
65+
&& str.len() <= self.min_ident_chars_threshold as usize
66+
&& !str.is_empty()
67+
&& self.allowed_idents_below_min_chars.get(&str.to_owned()).is_none()
68+
{
69+
let help = if self.min_ident_chars_threshold == 1 {
70+
Cow::Borrowed("this ident consists of a single char")
71+
} else {
72+
Cow::Owned(format!(
73+
"this ident is too short ({} <= {})",
74+
str.len(),
75+
self.min_ident_chars_threshold,
76+
))
77+
};
78+
span_lint(cx, MIN_IDENT_CHARS, ident.span, &help);
79+
}
80+
}
5881
}
5982

6083
struct IdentVisitor<'cx, 'tcx> {
6184
conf: &'cx MinIdentChars,
6285
cx: &'cx LateContext<'tcx>,
6386
}
6487

65-
#[expect(clippy::cast_possible_truncation)]
6688
impl Visitor<'_> for IdentVisitor<'_, '_> {
89+
#[expect(clippy::cast_possible_truncation)]
6790
fn visit_id(&mut self, hir_id: HirId) {
6891
let Self { conf, cx } = *self;
6992
// Reimplementation of `find`, as it uses indexing, which can (and will in async functions) panic.
@@ -122,9 +145,9 @@ impl Visitor<'_> for IdentVisitor<'_, '_> {
122145
Cow::Borrowed("this ident consists of a single char")
123146
} else {
124147
Cow::Owned(format!(
125-
"this ident is too short ({} <= {}) ",
148+
"this ident is too short ({} <= {})",
126149
str.len(),
127-
conf.min_ident_chars_threshold
150+
conf.min_ident_chars_threshold,
128151
))
129152
};
130153
span_lint(cx, MIN_IDENT_CHARS, ident.span, &help);

clippy_lints/src/utils/conf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const DEFAULT_DOC_VALID_IDENTS: &[&str] = &[
3434
"CamelCase",
3535
];
3636
const DEFAULT_DISALLOWED_NAMES: &[&str] = &["foo", "baz", "quux"];
37-
const DEFAULT_ALLOWED_IDENTS_BELOW_MIN_CHARS: &[&str] = &["i", "j", "x", "y", "z", "n"];
37+
const DEFAULT_ALLOWED_IDENTS_BELOW_MIN_CHARS: &[&str] = &["i", "j", "x", "y", "z", "w", "n"];
3838

3939
/// Holds information used by `MISSING_ENFORCED_IMPORT_RENAMES` lint.
4040
#[derive(Clone, Debug, Deserialize)]

tests/ui-toml/min_ident_chars/min_ident_chars.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ fn main() {
1414
let wha = 1;
1515
let vvv = 1;
1616
let uuu = 1;
17+
let (mut a, mut b) = (1, 2);
18+
for i in 0..1000 {}
1719
}
Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,46 @@
1-
error: this ident is too short (3 <= 3)
1+
error: this ident is too short (3 <= 3)
22
--> $DIR/min_ident_chars.rs:6:19
33
|
44
LL | use extern_types::Aaa;
55
| ^^^
66
|
77
= note: `-D clippy::min-ident-chars` implied by `-D warnings`
88

9-
error: this ident is too short (3 <= 3)
9+
error: this ident is too short (3 <= 3)
1010
--> $DIR/min_ident_chars.rs:10:5
1111
|
1212
LL | aaa: Aaa,
1313
| ^^^
1414

15-
error: aborting due to 2 previous errors
15+
error: this ident is too short (3 <= 3)
16+
--> $DIR/min_ident_chars.rs:15:9
17+
|
18+
LL | let vvv = 1;
19+
| ^^^
20+
21+
error: this ident is too short (3 <= 3)
22+
--> $DIR/min_ident_chars.rs:16:9
23+
|
24+
LL | let uuu = 1;
25+
| ^^^
26+
27+
error: this ident is too short (1 <= 3)
28+
--> $DIR/min_ident_chars.rs:17:14
29+
|
30+
LL | let (mut a, mut b) = (1, 2);
31+
| ^
32+
33+
error: this ident is too short (1 <= 3)
34+
--> $DIR/min_ident_chars.rs:17:21
35+
|
36+
LL | let (mut a, mut b) = (1, 2);
37+
| ^
38+
39+
error: this ident is too short (1 <= 3)
40+
--> $DIR/min_ident_chars.rs:18:9
41+
|
42+
LL | for i in 0..1000 {}
43+
| ^
44+
45+
error: aborting due to 7 previous errors
1646

tests/ui/min_ident_chars.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@aux-build:proc_macros.rs
2-
#![allow(nonstandard_style, unused)]
2+
#![allow(irrefutable_let_patterns, nonstandard_style, unused)]
33
#![warn(clippy::min_ident_chars)]
44

55
extern crate proc_macros;
@@ -15,6 +15,10 @@ struct A {
1515

1616
struct B(u32);
1717

18+
struct O {
19+
o: u32,
20+
}
21+
1822
struct i;
1923

2024
enum C {
@@ -38,11 +42,29 @@ fn main() {
3842
let w = 1;
3943
// Ok, not this one
4044
// let i = 1;
41-
let jz = 1;
42-
let nz = 1;
43-
let zx = 1;
44-
let yz = 1;
45-
let zz = 1;
45+
let j = 1;
46+
let n = 1;
47+
let z = 1;
48+
let y = 1;
49+
let z = 1;
50+
// Implicitly disallowed idents
51+
let h = 1;
52+
let e = 2;
53+
let l = 3;
54+
let l = 4;
55+
let o = 6;
56+
// 2 len does not lint
57+
let hi = 0;
58+
// Lint
59+
let (h, o, w) = (1, 2, 3);
60+
for (a, (r, e)) in (0..1000).enumerate().enumerate() {}
61+
let you = Vec4 { x: 1, y: 2, z: 3, w: 4 };
62+
while let (d, o, _i, n, g) = (true, true, false, false, true) {}
63+
let today = true;
64+
// Ideally this wouldn't lint, but this would (likely) require global analysis, outta scope
65+
// of this lint regardless
66+
let o = 1;
67+
let o = O { o };
4668

4769
for j in 0..1000 {}
4870

tests/ui/min_ident_chars.stderr

Lines changed: 105 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,40 +31,136 @@ LL | struct B(u32);
3131
| ^
3232

3333
error: this ident consists of a single char
34-
--> $DIR/min_ident_chars.rs:20:6
34+
--> $DIR/min_ident_chars.rs:18:8
35+
|
36+
LL | struct O {
37+
| ^
38+
39+
error: this ident consists of a single char
40+
--> $DIR/min_ident_chars.rs:19:5
41+
|
42+
LL | o: u32,
43+
| ^
44+
45+
error: this ident consists of a single char
46+
--> $DIR/min_ident_chars.rs:24:6
3547
|
3648
LL | enum C {
3749
| ^
3850

3951
error: this ident consists of a single char
40-
--> $DIR/min_ident_chars.rs:21:5
52+
--> $DIR/min_ident_chars.rs:25:5
4153
|
4254
LL | D,
4355
| ^
4456

4557
error: this ident consists of a single char
46-
--> $DIR/min_ident_chars.rs:22:5
58+
--> $DIR/min_ident_chars.rs:26:5
4759
|
4860
LL | E,
4961
| ^
5062

5163
error: this ident consists of a single char
52-
--> $DIR/min_ident_chars.rs:23:5
64+
--> $DIR/min_ident_chars.rs:27:5
5365
|
5466
LL | F,
5567
| ^
5668

5769
error: this ident consists of a single char
58-
--> $DIR/min_ident_chars.rs:31:5
70+
--> $DIR/min_ident_chars.rs:51:9
5971
|
60-
LL | w: u32,
61-
| ^
72+
LL | let h = 1;
73+
| ^
74+
75+
error: this ident consists of a single char
76+
--> $DIR/min_ident_chars.rs:52:9
77+
|
78+
LL | let e = 2;
79+
| ^
80+
81+
error: this ident consists of a single char
82+
--> $DIR/min_ident_chars.rs:53:9
83+
|
84+
LL | let l = 3;
85+
| ^
86+
87+
error: this ident consists of a single char
88+
--> $DIR/min_ident_chars.rs:54:9
89+
|
90+
LL | let l = 4;
91+
| ^
92+
93+
error: this ident consists of a single char
94+
--> $DIR/min_ident_chars.rs:55:9
95+
|
96+
LL | let o = 6;
97+
| ^
98+
99+
error: this ident consists of a single char
100+
--> $DIR/min_ident_chars.rs:59:10
101+
|
102+
LL | let (h, o, w) = (1, 2, 3);
103+
| ^
104+
105+
error: this ident consists of a single char
106+
--> $DIR/min_ident_chars.rs:59:13
107+
|
108+
LL | let (h, o, w) = (1, 2, 3);
109+
| ^
110+
111+
error: this ident consists of a single char
112+
--> $DIR/min_ident_chars.rs:60:10
113+
|
114+
LL | for (a, (r, e)) in (0..1000).enumerate().enumerate() {}
115+
| ^
116+
117+
error: this ident consists of a single char
118+
--> $DIR/min_ident_chars.rs:60:14
119+
|
120+
LL | for (a, (r, e)) in (0..1000).enumerate().enumerate() {}
121+
| ^
122+
123+
error: this ident consists of a single char
124+
--> $DIR/min_ident_chars.rs:60:17
125+
|
126+
LL | for (a, (r, e)) in (0..1000).enumerate().enumerate() {}
127+
| ^
128+
129+
error: this ident consists of a single char
130+
--> $DIR/min_ident_chars.rs:62:16
131+
|
132+
LL | while let (d, o, _i, n, g) = (true, true, false, false, true) {}
133+
| ^
134+
135+
error: this ident consists of a single char
136+
--> $DIR/min_ident_chars.rs:62:19
137+
|
138+
LL | while let (d, o, _i, n, g) = (true, true, false, false, true) {}
139+
| ^
140+
141+
error: this ident consists of a single char
142+
--> $DIR/min_ident_chars.rs:62:29
143+
|
144+
LL | while let (d, o, _i, n, g) = (true, true, false, false, true) {}
145+
| ^
146+
147+
error: this ident consists of a single char
148+
--> $DIR/min_ident_chars.rs:66:9
149+
|
150+
LL | let o = 1;
151+
| ^
152+
153+
error: this ident consists of a single char
154+
--> $DIR/min_ident_chars.rs:67:9
155+
|
156+
LL | let o = O { o };
157+
| ^
62158

63159
error: this ident consists of a single char
64-
--> $DIR/min_ident_chars.rs:58:4
160+
--> $DIR/min_ident_chars.rs:80:4
65161
|
66162
LL | fn b() {}
67163
| ^
68164

69-
error: aborting due to 11 previous errors
165+
error: aborting due to 27 previous errors
70166

0 commit comments

Comments
 (0)