Skip to content

Commit 29c1c6e

Browse files
committed
refactor and add link to issue
1 parent 203e875 commit 29c1c6e

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

clippy_lints/src/min_ident_chars.rs

+16-17
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ declare_clippy_lint! {
3434
/// let title = movie.title;
3535
/// }
3636
/// ```
37-
/// ```
3837
#[clippy::version = "1.72.0"]
3938
pub MIN_IDENT_CHARS,
4039
restriction,
@@ -48,6 +47,17 @@ pub struct MinIdentChars {
4847
pub min_ident_chars_threshold: u64,
4948
}
5049

50+
impl MinIdentChars {
51+
#[expect(clippy::cast_possible_truncation)]
52+
fn is_ident_too_short(&self, cx: &LateContext<'_>, str: &str, span: Span) -> bool {
53+
!in_external_macro(cx.sess(), span)
54+
&& str.len() <= self.min_ident_chars_threshold as usize
55+
&& !str.starts_with('_')
56+
&& !str.is_empty()
57+
&& self.allowed_idents_below_min_chars.get(&str.to_owned()).is_none()
58+
}
59+
}
60+
5161
impl LateLintPass<'_> for MinIdentChars {
5262
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
5363
if self.min_ident_chars_threshold == 0 {
@@ -58,15 +68,10 @@ impl LateLintPass<'_> for MinIdentChars {
5868
}
5969

6070
// This is necessary as `Node::Pat`s are not visited in `visit_id`. :/
61-
#[expect(clippy::cast_possible_truncation)]
6271
fn check_pat(&mut self, cx: &LateContext<'_>, pat: &Pat<'_>) {
6372
if let PatKind::Binding(_, _, ident, ..) = pat.kind
6473
&& let str = ident.as_str()
65-
&& !in_external_macro(cx.sess(), ident.span)
66-
&& str.len() <= self.min_ident_chars_threshold as usize
67-
&& !str.starts_with('_')
68-
&& !str.is_empty()
69-
&& self.allowed_idents_below_min_chars.get(&str.to_owned()).is_none()
74+
&& self.is_ident_too_short(cx, str, ident.span)
7075
{
7176
emit_min_ident_chars(self, cx, str, ident.span);
7277
}
@@ -79,12 +84,11 @@ struct IdentVisitor<'cx, 'tcx> {
7984
}
8085

8186
impl Visitor<'_> for IdentVisitor<'_, '_> {
82-
#[expect(clippy::cast_possible_truncation)]
8387
fn visit_id(&mut self, hir_id: HirId) {
8488
let Self { conf, cx } = *self;
85-
// Reimplementation of `find`, as it uses indexing, which can (and will in async functions) panic.
86-
// This should probably be fixed on the rustc side, this is just a temporary workaround.
87-
// FIXME: Remove me if/when this is fixed in rustc
89+
// FIXME(#112534) Reimplementation of `find`, as it uses indexing, which can (and will in
90+
// async functions, or really anything async) panic. This should probably be fixed on the
91+
// rustc side, this is just a temporary workaround.
8892
let node = if hir_id.local_id == ItemLocalId::from_u32(0) {
8993
// In this case, we can just use `find`, `Owner`'s `node` field is private anyway so we can't
9094
// reimplement it even if we wanted to
@@ -103,12 +107,7 @@ impl Visitor<'_> for IdentVisitor<'_, '_> {
103107
};
104108

105109
let str = ident.as_str();
106-
if !in_external_macro(cx.sess(), ident.span)
107-
&& str.len() <= conf.min_ident_chars_threshold as usize
108-
&& !str.starts_with('_')
109-
&& !str.is_empty()
110-
&& conf.allowed_idents_below_min_chars.get(&str.to_owned()).is_none()
111-
{
110+
if conf.is_ident_too_short(cx, str, ident.span) {
112111
if let Node::Item(item) = node && let ItemKind::Use(..) = item.kind {
113112
return;
114113
}

0 commit comments

Comments
 (0)