Skip to content

Commit 9263f80

Browse files
committed
Auto merge of rust-lang#11782 - Alexendoo:macro-use-imports-ordering, r=Centri3
Make `macro_use_imports` lint ordering more stable changelog: none Fixes [the `macro_use_imports` ordering dependence](rust-lang#117649 (comment)) on the hash of `Span`s
2 parents 6eb935a + d8c0e64 commit 9263f80

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

clippy_lints/src/macro_use.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use clippy_utils::source::snippet;
33
use hir::def::{DefKind, Res};
44
use rustc_ast::ast;
5-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
5+
use rustc_data_structures::fx::FxHashSet;
66
use rustc_errors::Applicability;
77
use rustc_hir as hir;
88
use rustc_lint::{LateContext, LateLintPass, LintContext};
99
use rustc_session::{declare_tool_lint, impl_lint_pass};
1010
use rustc_span::edition::Edition;
1111
use rustc_span::{sym, Span};
12+
use std::collections::BTreeMap;
1213

1314
declare_clippy_lint! {
1415
/// ### What it does
@@ -137,7 +138,7 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports {
137138
}
138139
}
139140
fn check_crate_post(&mut self, cx: &LateContext<'_>) {
140-
let mut used = FxHashMap::default();
141+
let mut used = BTreeMap::new();
141142
let mut check_dup = vec![];
142143
for (import, span, hir_id) in &self.imports {
143144
let found_idx = self.mac_refs.iter().position(|mac| import.ends_with(&mac.name));
@@ -186,20 +187,16 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports {
186187
}
187188
}
188189

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-
198190
// If mac_refs is not empty we have encountered an import we could not handle
199191
// such as `std::prelude::v1::foo` or some other macro that expands to an import.
200192
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+
203200
span_lint_hir_and_then(
204201
cx,
205202
MACRO_USE_IMPORTS,
@@ -210,7 +207,7 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports {
210207
diag.span_suggestion(
211208
*span,
212209
"remove the attribute and import the macro directly, try",
213-
help,
210+
format!("use {import};"),
214211
Applicability::MaybeIncorrect,
215212
);
216213
},

tests/ui/macro_use_imports.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
2-
--> $DIR/macro_use_imports.rs:25:5
2+
--> $DIR/macro_use_imports.rs:19:5
33
|
44
LL | #[macro_use]
5-
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::inner::nested::string_add;`
5+
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::{pub_macro, inner_mod_macro, function_macro, ty_macro, pub_in_private_macro};`
66
|
77
= note: `-D clippy::macro-use-imports` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::macro_use_imports)]`
@@ -14,16 +14,16 @@ LL | #[macro_use]
1414
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::{inner::mut_mut, inner::try_err};`
1515

1616
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
17-
--> $DIR/macro_use_imports.rs:21:5
17+
--> $DIR/macro_use_imports.rs:25:5
1818
|
1919
LL | #[macro_use]
20-
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mini_mac::ClippyMiniMacroTest;`
20+
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::inner::nested::string_add;`
2121

2222
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
23-
--> $DIR/macro_use_imports.rs:19:5
23+
--> $DIR/macro_use_imports.rs:21:5
2424
|
2525
LL | #[macro_use]
26-
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::{pub_macro, inner_mod_macro, function_macro, ty_macro, pub_in_private_macro};`
26+
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mini_mac::ClippyMiniMacroTest;`
2727

2828
error: aborting due to 4 previous errors
2929

0 commit comments

Comments
 (0)