@@ -8,6 +8,7 @@ use rustc_hir::{
8
8
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
9
9
use rustc_middle:: lint:: in_external_macro;
10
10
use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
11
+ use rustc_span:: Span ;
11
12
use std:: borrow:: Cow ;
12
13
13
14
declare_clippy_lint ! {
@@ -56,26 +57,18 @@ impl LateLintPass<'_> for MinIdentChars {
56
57
walk_item ( & mut IdentVisitor { conf : self , cx } , item) ;
57
58
}
58
59
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`. :/
60
61
#[ expect( clippy:: cast_possible_truncation) ]
61
62
fn check_pat ( & mut self , cx : & LateContext < ' _ > , pat : & Pat < ' _ > ) {
62
63
if let PatKind :: Binding ( _, _, ident, ..) = pat. kind
63
64
&& let str = ident. as_str ( )
64
65
&& !in_external_macro ( cx. sess ( ) , ident. span )
65
66
&& str. len ( ) <= self . min_ident_chars_threshold as usize
67
+ && !str. starts_with ( '_' )
66
68
&& !str. is_empty ( )
67
69
&& self . allowed_idents_below_min_chars . get ( & str. to_owned ( ) ) . is_none ( )
68
70
{
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 ) ;
79
72
}
80
73
}
81
74
}
@@ -112,6 +105,7 @@ impl Visitor<'_> for IdentVisitor<'_, '_> {
112
105
let str = ident. as_str ( ) ;
113
106
if !in_external_macro ( cx. sess ( ) , ident. span )
114
107
&& str. len ( ) <= conf. min_ident_chars_threshold as usize
108
+ && !str. starts_with ( '_' )
115
109
&& !str. is_empty ( )
116
110
&& conf. allowed_idents_below_min_chars . get ( & str. to_owned ( ) ) . is_none ( )
117
111
{
@@ -141,16 +135,20 @@ impl Visitor<'_> for IdentVisitor<'_, '_> {
141
135
return ;
142
136
}
143
137
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 ) ;
154
139
}
155
140
}
156
141
}
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
+ }
0 commit comments