@@ -8,6 +8,7 @@ use rustc_hir::{
88use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
99use rustc_middle:: lint:: in_external_macro;
1010use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
11+ use rustc_span:: Span ;
1112use std:: borrow:: Cow ;
1213
1314declare_clippy_lint ! {
@@ -56,26 +57,18 @@ impl LateLintPass<'_> for MinIdentChars {
5657 walk_item ( & mut IdentVisitor { conf : self , cx } , item) ;
5758 }
5859
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`. :/
6061 #[ expect( clippy:: cast_possible_truncation) ]
6162 fn check_pat ( & mut self , cx : & LateContext < ' _ > , pat : & Pat < ' _ > ) {
6263 if let PatKind :: Binding ( _, _, ident, ..) = pat. kind
6364 && let str = ident. as_str ( )
6465 && !in_external_macro ( cx. sess ( ) , ident. span )
6566 && str. len ( ) <= self . min_ident_chars_threshold as usize
67+ && !str. starts_with ( '_' )
6668 && !str. is_empty ( )
6769 && self . allowed_idents_below_min_chars . get ( & str. to_owned ( ) ) . is_none ( )
6870 {
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 ) ;
7972 }
8073 }
8174}
@@ -112,6 +105,7 @@ impl Visitor<'_> for IdentVisitor<'_, '_> {
112105 let str = ident. as_str ( ) ;
113106 if !in_external_macro ( cx. sess ( ) , ident. span )
114107 && str. len ( ) <= conf. min_ident_chars_threshold as usize
108+ && !str. starts_with ( '_' )
115109 && !str. is_empty ( )
116110 && conf. allowed_idents_below_min_chars . get ( & str. to_owned ( ) ) . is_none ( )
117111 {
@@ -141,16 +135,20 @@ impl Visitor<'_> for IdentVisitor<'_, '_> {
141135 return ;
142136 }
143137
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 ) ;
154139 }
155140 }
156141}
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