@@ -4,7 +4,9 @@ use clippy_utils::is_from_proc_macro;
4
4
use rustc_data_structures:: fx:: FxHashSet ;
5
5
use rustc_hir:: def:: { DefKind , Res } ;
6
6
use rustc_hir:: intravisit:: { Visitor , walk_item, walk_trait_item} ;
7
- use rustc_hir:: { GenericParamKind , HirId , Item , ItemKind , ItemLocalId , Node , Pat , PatKind , TraitItem , UsePath } ;
7
+ use rustc_hir:: {
8
+ GenericParamKind , HirId , Impl , ImplItemKind , Item , ItemKind , ItemLocalId , Node , Pat , PatKind , TraitItem , UsePath ,
9
+ } ;
8
10
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
9
11
use rustc_session:: impl_lint_pass;
10
12
use rustc_span:: Span ;
@@ -84,6 +86,7 @@ impl LateLintPass<'_> for MinIdentChars {
84
86
if let PatKind :: Binding ( _, _, ident, ..) = pat. kind
85
87
&& let str = ident. as_str ( )
86
88
&& self . is_ident_too_short ( cx, str, ident. span )
89
+ && is_not_in_trait_impl ( cx, pat)
87
90
{
88
91
emit_min_ident_chars ( self , cx, str, ident. span ) ;
89
92
}
@@ -201,3 +204,27 @@ fn opt_as_use_node(node: Node<'_>) -> Option<&'_ UsePath<'_>> {
201
204
None
202
205
}
203
206
}
207
+
208
+ /// Check if a pattern is a function param in an impl block for a trait.
209
+ fn is_not_in_trait_impl ( cx : & LateContext < ' _ > , pat : & Pat < ' _ > ) -> bool {
210
+ let parent_node = cx. tcx . parent_hir_node ( pat. hir_id ) ;
211
+ if !matches ! ( parent_node, Node :: Param ( _) ) {
212
+ return true ;
213
+ }
214
+
215
+ for ( _, parent_node) in cx. tcx . hir_parent_iter ( pat. hir_id ) {
216
+ if let Node :: ImplItem ( impl_item) = parent_node
217
+ && matches ! ( impl_item. kind, ImplItemKind :: Fn ( _, _) )
218
+ {
219
+ let impl_parent_node = cx. tcx . parent_hir_node ( impl_item. hir_id ( ) ) ;
220
+ if let Node :: Item ( parent_item) = impl_parent_node
221
+ && let ItemKind :: Impl ( Impl { of_trait : Some ( _) , .. } ) = & parent_item. kind
222
+ {
223
+ return false ;
224
+ }
225
+ return true ;
226
+ }
227
+ }
228
+
229
+ true
230
+ }
0 commit comments