@@ -3,13 +3,14 @@ use clippy_utils::source::snippet;
3
3
use hir:: def:: { DefKind , Res } ;
4
4
use if_chain:: if_chain;
5
5
use rustc_ast:: ast;
6
- use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
6
+ use rustc_data_structures:: fx:: FxHashSet ;
7
7
use rustc_errors:: Applicability ;
8
8
use rustc_hir as hir;
9
9
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
10
10
use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
11
11
use rustc_span:: edition:: Edition ;
12
12
use rustc_span:: { sym, Span } ;
13
+ use std:: collections:: BTreeMap ;
13
14
14
15
declare_clippy_lint ! {
15
16
/// ### What it does
@@ -142,7 +143,7 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports {
142
143
}
143
144
}
144
145
fn check_crate_post ( & mut self , cx : & LateContext < ' _ > ) {
145
- let mut used = FxHashMap :: default ( ) ;
146
+ let mut used = BTreeMap :: new ( ) ;
146
147
let mut check_dup = vec ! [ ] ;
147
148
for ( import, span, hir_id) in & self . imports {
148
149
let found_idx = self . mac_refs . iter ( ) . position ( |mac| import. ends_with ( & mac. name ) ) ;
@@ -191,20 +192,16 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports {
191
192
}
192
193
}
193
194
194
- let mut suggestions = vec ! [ ] ;
195
- for ( ( root, span, hir_id) , path) in used {
196
- if path. len ( ) == 1 {
197
- suggestions. push ( ( span, format ! ( "{root}::{}" , path[ 0 ] ) , hir_id) ) ;
198
- } else {
199
- suggestions. push ( ( span, format ! ( "{root}::{{{}}}" , path. join( ", " ) ) , hir_id) ) ;
200
- }
201
- }
202
-
203
195
// If mac_refs is not empty we have encountered an import we could not handle
204
196
// such as `std::prelude::v1::foo` or some other macro that expands to an import.
205
197
if self . mac_refs . is_empty ( ) {
206
- for ( span, import, hir_id) in suggestions {
207
- let help = format ! ( "use {import};" ) ;
198
+ for ( ( root, span, hir_id) , path) in used {
199
+ let import = if let [ single] = & path[ ..] {
200
+ format ! ( "{root}::{single}" )
201
+ } else {
202
+ format ! ( "{root}::{{{}}}" , path. join( ", " ) )
203
+ } ;
204
+
208
205
span_lint_hir_and_then (
209
206
cx,
210
207
MACRO_USE_IMPORTS ,
@@ -215,7 +212,7 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports {
215
212
diag. span_suggestion (
216
213
* span,
217
214
"remove the attribute and import the macro directly, try" ,
218
- help ,
215
+ format ! ( "use {import};" ) ,
219
216
Applicability :: MaybeIncorrect ,
220
217
) ;
221
218
} ,
0 commit comments