@@ -16,48 +16,32 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
16
16
used_trait_imports. extend ( imports. iter ( ) ) ;
17
17
}
18
18
19
- for id in tcx. hir ( ) . items ( ) {
20
- if matches ! ( tcx. def_kind( id. def_id) , DefKind :: Use ) {
21
- if tcx. visibility ( id. def_id ) . is_public ( ) {
22
- continue ;
23
- }
24
- let item = tcx. hir ( ) . item ( id) ;
25
- if item. span . is_dummy ( ) {
26
- continue ;
27
- }
28
- if let hir:: ItemKind :: Use ( path, _) = item. kind {
29
- check_import ( tcx, & mut used_trait_imports, item. item_id ( ) , path. span ) ;
30
- }
19
+ for & id in tcx. maybe_unused_trait_imports ( ( ) ) {
20
+ debug_assert_eq ! ( tcx. def_kind( id) , DefKind :: Use ) ;
21
+ if tcx. visibility ( id) . is_public ( ) {
22
+ continue ;
23
+ }
24
+ if used_trait_imports. contains ( & id) {
25
+ continue ;
31
26
}
27
+ let item = tcx. hir ( ) . expect_item ( id) ;
28
+ if item. span . is_dummy ( ) {
29
+ continue ;
30
+ }
31
+ let hir:: ItemKind :: Use ( path, _) = item. kind else { unreachable ! ( ) } ;
32
+ tcx. struct_span_lint_hir ( lint:: builtin:: UNUSED_IMPORTS , item. hir_id ( ) , path. span , |lint| {
33
+ let msg = if let Ok ( snippet) = tcx. sess . source_map ( ) . span_to_snippet ( path. span ) {
34
+ format ! ( "unused import: `{}`" , snippet)
35
+ } else {
36
+ "unused import" . to_owned ( )
37
+ } ;
38
+ lint. build ( & msg) . emit ( ) ;
39
+ } ) ;
32
40
}
33
41
34
42
unused_crates_lint ( tcx) ;
35
43
}
36
44
37
- fn check_import < ' tcx > (
38
- tcx : TyCtxt < ' tcx > ,
39
- used_trait_imports : & mut FxHashSet < LocalDefId > ,
40
- item_id : hir:: ItemId ,
41
- span : Span ,
42
- ) {
43
- if !tcx. maybe_unused_trait_import ( item_id. def_id ) {
44
- return ;
45
- }
46
-
47
- if used_trait_imports. contains ( & item_id. def_id ) {
48
- return ;
49
- }
50
-
51
- tcx. struct_span_lint_hir ( lint:: builtin:: UNUSED_IMPORTS , item_id. hir_id ( ) , span, |lint| {
52
- let msg = if let Ok ( snippet) = tcx. sess . source_map ( ) . span_to_snippet ( span) {
53
- format ! ( "unused import: `{}`" , snippet)
54
- } else {
55
- "unused import" . to_owned ( )
56
- } ;
57
- lint. build ( & msg) . emit ( ) ;
58
- } ) ;
59
- }
60
-
61
45
fn unused_crates_lint ( tcx : TyCtxt < ' _ > ) {
62
46
let lint = lint:: builtin:: UNUSED_EXTERN_CRATES ;
63
47
0 commit comments