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