@@ -3,7 +3,7 @@ use clippy_utils::is_from_proc_macro;
3
3
use rustc_data_structures:: fx:: FxHashSet ;
4
4
use rustc_hir:: def:: { DefKind , Res } ;
5
5
use rustc_hir:: intravisit:: { walk_item, Visitor } ;
6
- use rustc_hir:: { GenericParamKind , HirId , Item , ItemKind , ItemLocalId , Node , Pat , PatKind } ;
6
+ use rustc_hir:: { GenericParamKind , HirId , Item , ItemKind , ItemLocalId , Node , Pat , PatKind , UsePath } ;
7
7
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
8
8
use rustc_middle:: lint:: in_external_macro;
9
9
use rustc_session:: impl_lint_pass;
@@ -105,11 +105,26 @@ impl Visitor<'_> for IdentVisitor<'_, '_> {
105
105
106
106
let str = ident. as_str ( ) ;
107
107
if conf. is_ident_too_short ( cx, str, ident. span ) {
108
- if let Node :: Item ( item) = node
109
- && let ItemKind :: Use ( ..) = item. kind
108
+ // Check whether the node is part of a `use statement. We don't want to emit a warning if the user
109
+ // has no control over the type.
110
+ let usenode = opt_as_use_node ( node) . or_else ( || {
111
+ cx. tcx
112
+ . hir ( )
113
+ . parent_iter ( hir_id)
114
+ . find_map ( |( _, node) | opt_as_use_node ( node) )
115
+ } ) ;
116
+
117
+ // If the name of the identifier is the same from the one of the imported item, this means that we
118
+ // found a `use foo::bar`. We can early-return to not emit the warning.
119
+ // If however the identifier is different, this means it is an alias (`use foo::bar as baz`). In
120
+ // this case, we need to emit the warning for `baz`.
121
+ if let Some ( imported_item_path) = usenode
122
+ && let Some ( Res :: Def ( _, imported_item_defid) ) = imported_item_path. res . first ( )
123
+ && cx. tcx . item_name ( * imported_item_defid) . as_str ( ) == str
110
124
{
111
125
return ;
112
126
}
127
+
113
128
// `struct Awa<T>(T)`
114
129
// ^
115
130
if let Node :: PathSegment ( path) = node {
@@ -160,3 +175,16 @@ fn emit_min_ident_chars(conf: &MinIdentChars, cx: &impl LintContext, ident: &str
160
175
} ;
161
176
span_lint ( cx, MIN_IDENT_CHARS , span, & help) ;
162
177
}
178
+
179
+ /// Attempt to convert the node to an [`ItemKind::Use`] node.
180
+ ///
181
+ /// If it is, return the [`UsePath`] contained within.
182
+ fn opt_as_use_node ( node : Node < ' _ > ) -> Option < & ' _ UsePath < ' _ > > {
183
+ if let Node :: Item ( item) = node
184
+ && let ItemKind :: Use ( path, _) = item. kind
185
+ {
186
+ Some ( path)
187
+ } else {
188
+ None
189
+ }
190
+ }
0 commit comments