Auto merge of #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](https://github.com/rust-lang/rust/pull/117649#issuecomment-1797716088) on the hash of `Span`s
This commit is contained in:
commit
9263f806d8
@ -2,13 +2,14 @@ use clippy_utils::diagnostics::span_lint_hir_and_then;
|
|||||||
use clippy_utils::source::snippet;
|
use clippy_utils::source::snippet;
|
||||||
use hir::def::{DefKind, Res};
|
use hir::def::{DefKind, Res};
|
||||||
use rustc_ast::ast;
|
use rustc_ast::ast;
|
||||||
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
use rustc_data_structures::fx::FxHashSet;
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
||||||
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
||||||
use rustc_span::edition::Edition;
|
use rustc_span::edition::Edition;
|
||||||
use rustc_span::{sym, Span};
|
use rustc_span::{sym, Span};
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
declare_clippy_lint! {
|
declare_clippy_lint! {
|
||||||
/// ### What it does
|
/// ### What it does
|
||||||
@ -137,7 +138,7 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn check_crate_post(&mut self, cx: &LateContext<'_>) {
|
fn check_crate_post(&mut self, cx: &LateContext<'_>) {
|
||||||
let mut used = FxHashMap::default();
|
let mut used = BTreeMap::new();
|
||||||
let mut check_dup = vec![];
|
let mut check_dup = vec![];
|
||||||
for (import, span, hir_id) in &self.imports {
|
for (import, span, hir_id) in &self.imports {
|
||||||
let found_idx = self.mac_refs.iter().position(|mac| import.ends_with(&mac.name));
|
let found_idx = self.mac_refs.iter().position(|mac| import.ends_with(&mac.name));
|
||||||
@ -186,20 +187,16 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut suggestions = vec![];
|
|
||||||
for ((root, span, hir_id), path) in used {
|
|
||||||
if path.len() == 1 {
|
|
||||||
suggestions.push((span, format!("{root}::{}", path[0]), hir_id));
|
|
||||||
} else {
|
|
||||||
suggestions.push((span, format!("{root}::{{{}}}", path.join(", ")), hir_id));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If mac_refs is not empty we have encountered an import we could not handle
|
// If mac_refs is not empty we have encountered an import we could not handle
|
||||||
// such as `std::prelude::v1::foo` or some other macro that expands to an import.
|
// such as `std::prelude::v1::foo` or some other macro that expands to an import.
|
||||||
if self.mac_refs.is_empty() {
|
if self.mac_refs.is_empty() {
|
||||||
for (span, import, hir_id) in suggestions {
|
for ((root, span, hir_id), path) in used {
|
||||||
let help = format!("use {import};");
|
let import = if let [single] = &path[..] {
|
||||||
|
format!("{root}::{single}")
|
||||||
|
} else {
|
||||||
|
format!("{root}::{{{}}}", path.join(", "))
|
||||||
|
};
|
||||||
|
|
||||||
span_lint_hir_and_then(
|
span_lint_hir_and_then(
|
||||||
cx,
|
cx,
|
||||||
MACRO_USE_IMPORTS,
|
MACRO_USE_IMPORTS,
|
||||||
@ -210,7 +207,7 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports {
|
|||||||
diag.span_suggestion(
|
diag.span_suggestion(
|
||||||
*span,
|
*span,
|
||||||
"remove the attribute and import the macro directly, try",
|
"remove the attribute and import the macro directly, try",
|
||||||
help,
|
format!("use {import};"),
|
||||||
Applicability::MaybeIncorrect,
|
Applicability::MaybeIncorrect,
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
|
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
|
||||||
--> $DIR/macro_use_imports.rs:25:5
|
--> $DIR/macro_use_imports.rs:19:5
|
||||||
|
|
|
|
||||||
LL | #[macro_use]
|
LL | #[macro_use]
|
||||||
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::inner::nested::string_add;`
|
| ^^^^^^^^^^^^ 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};`
|
||||||
|
|
|
|
||||||
= note: `-D clippy::macro-use-imports` implied by `-D warnings`
|
= note: `-D clippy::macro-use-imports` implied by `-D warnings`
|
||||||
= help: to override `-D warnings` add `#[allow(clippy::macro_use_imports)]`
|
= help: to override `-D warnings` add `#[allow(clippy::macro_use_imports)]`
|
||||||
@ -13,17 +13,17 @@ error: `macro_use` attributes are no longer needed in the Rust 2018 edition
|
|||||||
LL | #[macro_use]
|
LL | #[macro_use]
|
||||||
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::{inner::mut_mut, inner::try_err};`
|
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::{inner::mut_mut, inner::try_err};`
|
||||||
|
|
||||||
|
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
|
||||||
|
--> $DIR/macro_use_imports.rs:25:5
|
||||||
|
|
|
||||||
|
LL | #[macro_use]
|
||||||
|
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::inner::nested::string_add;`
|
||||||
|
|
||||||
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
|
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
|
||||||
--> $DIR/macro_use_imports.rs:21:5
|
--> $DIR/macro_use_imports.rs:21:5
|
||||||
|
|
|
|
||||||
LL | #[macro_use]
|
LL | #[macro_use]
|
||||||
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mini_mac::ClippyMiniMacroTest;`
|
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mini_mac::ClippyMiniMacroTest;`
|
||||||
|
|
||||||
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
|
|
||||||
--> $DIR/macro_use_imports.rs:19:5
|
|
||||||
|
|
|
||||||
LL | #[macro_use]
|
|
||||||
| ^^^^^^^^^^^^ 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};`
|
|
||||||
|
|
||||||
error: aborting due to 4 previous errors
|
error: aborting due to 4 previous errors
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user