add to tests and configuration
This commit is contained in:
parent
243943ff56
commit
95d1bff225
@ -8,6 +8,7 @@
|
||||
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
||||
use rustc_middle::lint::in_external_macro;
|
||||
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
||||
use rustc_span::Span;
|
||||
use std::borrow::Cow;
|
||||
|
||||
declare_clippy_lint! {
|
||||
@ -56,26 +57,18 @@ fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
|
||||
walk_item(&mut IdentVisitor { conf: self, cx }, item);
|
||||
}
|
||||
|
||||
// This is necessary as bindings are not visited in `visit_id`. :/
|
||||
// This is necessary as `Node::Pat`s are not visited in `visit_id`. :/
|
||||
#[expect(clippy::cast_possible_truncation)]
|
||||
fn check_pat(&mut self, cx: &LateContext<'_>, pat: &Pat<'_>) {
|
||||
if let PatKind::Binding(_, _, ident, ..) = pat.kind
|
||||
&& let str = ident.as_str()
|
||||
&& !in_external_macro(cx.sess(), ident.span)
|
||||
&& str.len() <= self.min_ident_chars_threshold as usize
|
||||
&& !str.starts_with('_')
|
||||
&& !str.is_empty()
|
||||
&& self.allowed_idents_below_min_chars.get(&str.to_owned()).is_none()
|
||||
{
|
||||
let help = if self.min_ident_chars_threshold == 1 {
|
||||
Cow::Borrowed("this ident consists of a single char")
|
||||
} else {
|
||||
Cow::Owned(format!(
|
||||
"this ident is too short ({} <= {})",
|
||||
str.len(),
|
||||
self.min_ident_chars_threshold,
|
||||
))
|
||||
};
|
||||
span_lint(cx, MIN_IDENT_CHARS, ident.span, &help);
|
||||
emit_min_ident_chars(self, cx, str, ident.span);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -112,6 +105,7 @@ fn visit_id(&mut self, hir_id: HirId) {
|
||||
let str = ident.as_str();
|
||||
if !in_external_macro(cx.sess(), ident.span)
|
||||
&& str.len() <= conf.min_ident_chars_threshold as usize
|
||||
&& !str.starts_with('_')
|
||||
&& !str.is_empty()
|
||||
&& conf.allowed_idents_below_min_chars.get(&str.to_owned()).is_none()
|
||||
{
|
||||
@ -141,16 +135,20 @@ fn visit_id(&mut self, hir_id: HirId) {
|
||||
return;
|
||||
}
|
||||
|
||||
let help = if conf.min_ident_chars_threshold == 1 {
|
||||
Cow::Borrowed("this ident consists of a single char")
|
||||
} else {
|
||||
Cow::Owned(format!(
|
||||
"this ident is too short ({} <= {})",
|
||||
str.len(),
|
||||
conf.min_ident_chars_threshold,
|
||||
))
|
||||
};
|
||||
span_lint(cx, MIN_IDENT_CHARS, ident.span, &help);
|
||||
emit_min_ident_chars(conf, cx, str, ident.span);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn emit_min_ident_chars(conf: &MinIdentChars, cx: &impl LintContext, ident: &str, span: Span) {
|
||||
let help = if conf.min_ident_chars_threshold == 1 {
|
||||
Cow::Borrowed("this ident consists of a single char")
|
||||
} else {
|
||||
Cow::Owned(format!(
|
||||
"this ident is too short ({} <= {})",
|
||||
ident.len(),
|
||||
conf.min_ident_chars_threshold,
|
||||
))
|
||||
};
|
||||
span_lint(cx, MIN_IDENT_CHARS, span, &help);
|
||||
}
|
||||
|
@ -525,7 +525,9 @@ pub fn get_configuration_metadata() -> Vec<ClippyConfiguration> {
|
||||
(allow_private_module_inception: bool = false),
|
||||
/// Lint: MIN_IDENT_CHARS.
|
||||
///
|
||||
/// Allowed names below the minimum allowed characters.
|
||||
/// Allowed names below the minimum allowed characters. The value `".."` can be used as part of
|
||||
/// the list to indicate, that the configured values should be appended to the default
|
||||
/// configuration of Clippy. By default, any configuration will replace the default value.
|
||||
(allowed_idents_below_min_chars: rustc_data_structures::fx::FxHashSet<String> =
|
||||
super::DEFAULT_ALLOWED_IDENTS_BELOW_MIN_CHARS.iter().map(ToString::to_string).collect()),
|
||||
/// Lint: MIN_IDENT_CHARS.
|
||||
@ -599,6 +601,12 @@ pub fn read(sess: &Session, path: &Path) -> TryConf {
|
||||
Ok(mut conf) => {
|
||||
extend_vec_if_indicator_present(&mut conf.conf.doc_valid_idents, DEFAULT_DOC_VALID_IDENTS);
|
||||
extend_vec_if_indicator_present(&mut conf.conf.disallowed_names, DEFAULT_DISALLOWED_NAMES);
|
||||
// TODO: THIS SHOULD BE TESTED, this comment will be gone soon
|
||||
if conf.conf.allowed_idents_below_min_chars.contains(&"..".to_owned()) {
|
||||
conf.conf
|
||||
.allowed_idents_below_min_chars
|
||||
.extend(DEFAULT_ALLOWED_IDENTS_BELOW_MIN_CHARS.iter().map(ToString::to_string));
|
||||
}
|
||||
|
||||
conf
|
||||
},
|
||||
|
@ -67,6 +67,7 @@ fn main() {
|
||||
let o = O { o };
|
||||
|
||||
for j in 0..1000 {}
|
||||
for _ in 0..10 {}
|
||||
|
||||
// Do not lint code from external macros
|
||||
external! { for j in 0..1000 {} }
|
||||
@ -78,4 +79,6 @@ fn main() {
|
||||
}
|
||||
|
||||
fn b() {}
|
||||
fn owo() {}
|
||||
fn wrong_pythagoras(a: f32, b: f32) -> f32 {
|
||||
a * a + a * b
|
||||
}
|
||||
|
@ -157,10 +157,22 @@ LL | let o = O { o };
|
||||
| ^
|
||||
|
||||
error: this ident consists of a single char
|
||||
--> $DIR/min_ident_chars.rs:80:4
|
||||
--> $DIR/min_ident_chars.rs:81:4
|
||||
|
|
||||
LL | fn b() {}
|
||||
| ^
|
||||
|
||||
error: aborting due to 27 previous errors
|
||||
error: this ident consists of a single char
|
||||
--> $DIR/min_ident_chars.rs:82: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
|
||||
|
|
||||
LL | fn wrong_pythagoras(a: f32, b: f32) -> f32 {
|
||||
| ^
|
||||
|
||||
error: aborting due to 29 previous errors
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user