diff options
| author | Centri3 <114838443+Centri3@users.noreply.github.com> | 2023-06-10 09:13:04 -0500 |
|---|---|---|
| committer | Centri3 <114838443+Centri3@users.noreply.github.com> | 2023-06-12 03:22:01 -0500 |
| commit | 95d1bff2251ff33e1e8ca2bc0c1941c4e91bc2e4 (patch) | |
| tree | fb2940172986063108e19a12c161cdb15d72b350 | |
| parent | 243943ff56c755822968a48d5949f79c1d3a7dfa (diff) | |
| download | rust-95d1bff2251ff33e1e8ca2bc0c1941c4e91bc2e4.tar.gz rust-95d1bff2251ff33e1e8ca2bc0c1941c4e91bc2e4.zip | |
add to tests and configuration
| -rw-r--r-- | clippy_lints/src/min_ident_chars.rs | 40 | ||||
| -rw-r--r-- | clippy_lints/src/utils/conf.rs | 10 | ||||
| -rw-r--r-- | tests/ui/min_ident_chars.rs | 5 | ||||
| -rw-r--r-- | tests/ui/min_ident_chars.stderr | 16 |
4 files changed, 46 insertions, 25 deletions
diff --git a/clippy_lints/src/min_ident_chars.rs b/clippy_lints/src/min_ident_chars.rs index cc027b6f498..316c2bf122c 100644 --- a/clippy_lints/src/min_ident_chars.rs +++ b/clippy_lints/src/min_ident_chars.rs @@ -8,6 +8,7 @@ use rustc_hir::{ 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 @@ impl LateLintPass<'_> for MinIdentChars { 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 @@ impl Visitor<'_> for IdentVisitor<'_, '_> { 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 @@ impl Visitor<'_> for IdentVisitor<'_, '_> { 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); +} diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index f050782a16d..0a581e0ab9b 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -525,7 +525,9 @@ define_Conf! { (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 }, diff --git a/tests/ui/min_ident_chars.rs b/tests/ui/min_ident_chars.rs index 85608d030ed..c5e03468ef9 100644 --- a/tests/ui/min_ident_chars.rs +++ b/tests/ui/min_ident_chars.rs @@ -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 +} diff --git a/tests/ui/min_ident_chars.stderr b/tests/ui/min_ident_chars.stderr index cd8825634c3..66a63f65756 100644 --- a/tests/ui/min_ident_chars.stderr +++ b/tests/ui/min_ident_chars.stderr @@ -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 |
