Skip to content

Commit 95d1bff

Browse files
committed
add to tests and configuration
1 parent 243943f commit 95d1bff

File tree

4 files changed

+46
-25
lines changed

4 files changed

+46
-25
lines changed

clippy_lints/src/min_ident_chars.rs

+19-21
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_hir::{
88
use rustc_lint::{LateContext, LateLintPass, LintContext};
99
use rustc_middle::lint::in_external_macro;
1010
use rustc_session::{declare_tool_lint, impl_lint_pass};
11+
use rustc_span::Span;
1112
use std::borrow::Cow;
1213

1314
declare_clippy_lint! {
@@ -56,26 +57,18 @@ impl LateLintPass<'_> for MinIdentChars {
5657
walk_item(&mut IdentVisitor { conf: self, cx }, item);
5758
}
5859

59-
// This is necessary as bindings are not visited in `visit_id`. :/
60+
// This is necessary as `Node::Pat`s are not visited in `visit_id`. :/
6061
#[expect(clippy::cast_possible_truncation)]
6162
fn check_pat(&mut self, cx: &LateContext<'_>, pat: &Pat<'_>) {
6263
if let PatKind::Binding(_, _, ident, ..) = pat.kind
6364
&& let str = ident.as_str()
6465
&& !in_external_macro(cx.sess(), ident.span)
6566
&& str.len() <= self.min_ident_chars_threshold as usize
67+
&& !str.starts_with('_')
6668
&& !str.is_empty()
6769
&& self.allowed_idents_below_min_chars.get(&str.to_owned()).is_none()
6870
{
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);
71+
emit_min_ident_chars(self, cx, str, ident.span);
7972
}
8073
}
8174
}
@@ -112,6 +105,7 @@ impl Visitor<'_> for IdentVisitor<'_, '_> {
112105
let str = ident.as_str();
113106
if !in_external_macro(cx.sess(), ident.span)
114107
&& str.len() <= conf.min_ident_chars_threshold as usize
108+
&& !str.starts_with('_')
115109
&& !str.is_empty()
116110
&& conf.allowed_idents_below_min_chars.get(&str.to_owned()).is_none()
117111
{
@@ -141,16 +135,20 @@ impl Visitor<'_> for IdentVisitor<'_, '_> {
141135
return;
142136
}
143137

144-
let help = if conf.min_ident_chars_threshold == 1 {
145-
Cow::Borrowed("this ident consists of a single char")
146-
} else {
147-
Cow::Owned(format!(
148-
"this ident is too short ({} <= {})",
149-
str.len(),
150-
conf.min_ident_chars_threshold,
151-
))
152-
};
153-
span_lint(cx, MIN_IDENT_CHARS, ident.span, &help);
138+
emit_min_ident_chars(conf, cx, str, ident.span);
154139
}
155140
}
156141
}
142+
143+
fn emit_min_ident_chars(conf: &MinIdentChars, cx: &impl LintContext, ident: &str, span: Span) {
144+
let help = if conf.min_ident_chars_threshold == 1 {
145+
Cow::Borrowed("this ident consists of a single char")
146+
} else {
147+
Cow::Owned(format!(
148+
"this ident is too short ({} <= {})",
149+
ident.len(),
150+
conf.min_ident_chars_threshold,
151+
))
152+
};
153+
span_lint(cx, MIN_IDENT_CHARS, span, &help);
154+
}

clippy_lints/src/utils/conf.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,9 @@ define_Conf! {
525525
(allow_private_module_inception: bool = false),
526526
/// Lint: MIN_IDENT_CHARS.
527527
///
528-
/// Allowed names below the minimum allowed characters.
528+
/// Allowed names below the minimum allowed characters. The value `".."` can be used as part of
529+
/// the list to indicate, that the configured values should be appended to the default
530+
/// configuration of Clippy. By default, any configuration will replace the default value.
529531
(allowed_idents_below_min_chars: rustc_data_structures::fx::FxHashSet<String> =
530532
super::DEFAULT_ALLOWED_IDENTS_BELOW_MIN_CHARS.iter().map(ToString::to_string).collect()),
531533
/// Lint: MIN_IDENT_CHARS.
@@ -599,6 +601,12 @@ pub fn read(sess: &Session, path: &Path) -> TryConf {
599601
Ok(mut conf) => {
600602
extend_vec_if_indicator_present(&mut conf.conf.doc_valid_idents, DEFAULT_DOC_VALID_IDENTS);
601603
extend_vec_if_indicator_present(&mut conf.conf.disallowed_names, DEFAULT_DISALLOWED_NAMES);
604+
// TODO: THIS SHOULD BE TESTED, this comment will be gone soon
605+
if conf.conf.allowed_idents_below_min_chars.contains(&"..".to_owned()) {
606+
conf.conf
607+
.allowed_idents_below_min_chars
608+
.extend(DEFAULT_ALLOWED_IDENTS_BELOW_MIN_CHARS.iter().map(ToString::to_string));
609+
}
602610

603611
conf
604612
},

tests/ui/min_ident_chars.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ fn main() {
6767
let o = O { o };
6868

6969
for j in 0..1000 {}
70+
for _ in 0..10 {}
7071

7172
// Do not lint code from external macros
7273
external! { for j in 0..1000 {} }
@@ -78,4 +79,6 @@ fn main() {
7879
}
7980

8081
fn b() {}
81-
fn owo() {}
82+
fn wrong_pythagoras(a: f32, b: f32) -> f32 {
83+
a * a + a * b
84+
}

tests/ui/min_ident_chars.stderr

+14-2
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,22 @@ LL | let o = O { o };
157157
| ^
158158

159159
error: this ident consists of a single char
160-
--> $DIR/min_ident_chars.rs:80:4
160+
--> $DIR/min_ident_chars.rs:81:4
161161
|
162162
LL | fn b() {}
163163
| ^
164164

165-
error: aborting due to 27 previous errors
165+
error: this ident consists of a single char
166+
--> $DIR/min_ident_chars.rs:82:21
167+
|
168+
LL | fn wrong_pythagoras(a: f32, b: f32) -> f32 {
169+
| ^
170+
171+
error: this ident consists of a single char
172+
--> $DIR/min_ident_chars.rs:82:29
173+
|
174+
LL | fn wrong_pythagoras(a: f32, b: f32) -> f32 {
175+
| ^
176+
177+
error: aborting due to 29 previous errors
166178

0 commit comments

Comments
 (0)