Skip to content

Commit

Permalink
add to tests and configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Centri3 committed Jun 10, 2023
1 parent 3d0adf9 commit c98e566
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 25 deletions.
40 changes: 19 additions & 21 deletions clippy_lints/src/min_ident_chars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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);
}
10 changes: 9 additions & 1 deletion clippy_lints/src/utils/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,9 @@ define_Conf! {
(unnecessary_box_size: u64 = 128),
/// 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.
Expand Down Expand Up @@ -591,6 +593,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
},
Expand Down
5 changes: 4 additions & 1 deletion tests/ui/min_ident_chars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {} }
Expand All @@ -78,4 +79,6 @@ fn main() {
}

fn b() {}
fn owo() {}
fn wrong_pythagoras(a: f32, b: f32) -> f32 {
a * a + a * b
}
16 changes: 14 additions & 2 deletions tests/ui/min_ident_chars.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c98e566

Please sign in to comment.