Remove BuiltinLintDiag::UnusedImports variant

This commit is contained in:
Guillaume Gomez
2026-04-06 17:32:39 +02:00
parent edad6298f6
commit e03e00f9e3
5 changed files with 71 additions and 86 deletions
@@ -36,31 +36,6 @@ pub struct DecorateBuiltinLint<'sess, 'tcx> {
impl<'a> Diagnostic<'a, ()> for DecorateBuiltinLint<'_, '_> {
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
match self.diagnostic {
BuiltinLintDiag::UnusedImports {
remove_whole_use,
num_to_remove,
remove_spans,
test_module_span,
span_snippets,
} => {
let sugg = if remove_whole_use {
lints::UnusedImportsSugg::RemoveWholeUse { span: remove_spans[0] }
} else {
lints::UnusedImportsSugg::RemoveImports { remove_spans, num_to_remove }
};
let test_module_span =
test_module_span.map(|span| self.sess.source_map().guess_head_span(span));
lints::UnusedImports {
sugg,
test_module_span,
num_snippets: span_snippets.len(),
span_snippets: DiagArgValue::StrListSepByAnd(
span_snippets.into_iter().map(Cow::Owned).collect(),
),
}
.into_diag(dcx, level)
}
BuiltinLintDiag::NamedArgumentUsedPositionally {
position_sp_to_replace,
position_sp_for_msg,
-44
View File
@@ -3039,50 +3039,6 @@ pub(crate) struct IllFormedAttributeInputHelp {
pub lint: String,
}
#[derive(Diagnostic)]
#[diag(
"{$num_snippets ->
[one] unused import: {$span_snippets}
*[other] unused imports: {$span_snippets}
}"
)]
pub(crate) struct UnusedImports {
#[subdiagnostic]
pub sugg: UnusedImportsSugg,
#[help("if this is a test module, consider adding a `#[cfg(test)]` to the containing module")]
pub test_module_span: Option<Span>,
pub span_snippets: DiagArgValue,
pub num_snippets: usize,
}
#[derive(Subdiagnostic)]
pub(crate) enum UnusedImportsSugg {
#[suggestion(
"remove the whole `use` item",
applicability = "machine-applicable",
code = "",
style = "tool-only"
)]
RemoveWholeUse {
#[primary_span]
span: Span,
},
#[multipart_suggestion(
"{$num_to_remove ->
[one] remove the unused import
*[other] remove the unused imports
}",
applicability = "machine-applicable",
style = "tool-only"
)]
RemoveImports {
#[suggestion_part(code = "")]
remove_spans: Vec<Span>,
num_to_remove: usize,
},
}
#[derive(Diagnostic)]
#[diag("named argument `{$named_arg_name}` is not used by name")]
pub(crate) struct NamedArgumentUsedPositionally {
-7
View File
@@ -656,13 +656,6 @@ pub enum DeprecatedSinceKind {
// becomes hacky (and it gets allocated).
#[derive(Debug)]
pub enum BuiltinLintDiag {
UnusedImports {
remove_whole_use: bool,
num_to_remove: usize,
remove_spans: Vec<Span>,
test_module_span: Option<Span>,
span_snippets: Vec<String>,
},
NamedArgumentUsedPositionally {
/// Span where the named argument is used by position and will be replaced with the named
/// argument name
+26 -9
View File
@@ -23,13 +23,14 @@
// - `check_unused` finally emits the diagnostics based on the data generated
// in the last step
use std::borrow::Cow;
use rustc_ast as ast;
use rustc_ast::visit::{self, Visitor};
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
use rustc_data_structures::unord::UnordSet;
use rustc_errors::MultiSpan;
use rustc_errors::{DiagArgValue, Diagnostic, MultiSpan};
use rustc_hir::def::{DefKind, Res};
use rustc_session::lint::BuiltinLintDiag;
use rustc_session::lint::builtin::{
MACRO_USE_EXTERN_CRATE, UNUSED_EXTERN_CRATES, UNUSED_IMPORTS, UNUSED_QUALIFICATIONS,
};
@@ -496,16 +497,32 @@ pub(crate) fn check_unused(&mut self, krate: &ast::Crate) {
}
};
visitor.r.lint_buffer.buffer_lint(
visitor.r.lint_buffer.dyn_buffer_lint_any(
UNUSED_IMPORTS,
unused.use_tree_id,
ms,
BuiltinLintDiag::UnusedImports {
remove_whole_use,
num_to_remove,
remove_spans,
test_module_span,
span_snippets,
move |dcx, level, sess| {
let sugg = if remove_whole_use {
errors::UnusedImportsSugg::RemoveWholeUse { span: remove_spans[0] }
} else {
errors::UnusedImportsSugg::RemoveImports { remove_spans, num_to_remove }
};
let test_module_span = test_module_span.map(|span| {
sess.downcast_ref::<rustc_session::Session>()
.expect("expected a `Session`")
.source_map()
.guess_head_span(span)
});
errors::UnusedImports {
sugg,
test_module_span,
num_snippets: span_snippets.len(),
span_snippets: DiagArgValue::StrListSepByAnd(
span_snippets.into_iter().map(Cow::Owned).collect(),
),
}
.into_diag(dcx, level)
},
);
}
+45 -1
View File
@@ -1,7 +1,7 @@
use rustc_errors::codes::*;
use rustc_errors::formatting::DiagMessageAddArg;
use rustc_errors::{
Applicability, Diag, DiagCtxtHandle, Diagnostic, ElidedLifetimeInPathSubdiag,
Applicability, Diag, DiagArgValue, DiagCtxtHandle, Diagnostic, ElidedLifetimeInPathSubdiag,
EmissionGuarantee, IntoDiagArg, Level, MultiSpan, Subdiagnostic, msg,
};
use rustc_macros::{Diagnostic, Subdiagnostic};
@@ -1747,3 +1747,47 @@ pub(crate) struct ElidedLifetimesInPaths {
#[subdiagnostic]
pub subdiag: rustc_errors::ElidedLifetimeInPathSubdiag,
}
#[derive(Diagnostic)]
#[diag(
"{$num_snippets ->
[one] unused import: {$span_snippets}
*[other] unused imports: {$span_snippets}
}"
)]
pub(crate) struct UnusedImports {
#[subdiagnostic]
pub sugg: UnusedImportsSugg,
#[help("if this is a test module, consider adding a `#[cfg(test)]` to the containing module")]
pub test_module_span: Option<Span>,
pub span_snippets: DiagArgValue,
pub num_snippets: usize,
}
#[derive(Subdiagnostic)]
pub(crate) enum UnusedImportsSugg {
#[suggestion(
"remove the whole `use` item",
applicability = "machine-applicable",
code = "",
style = "tool-only"
)]
RemoveWholeUse {
#[primary_span]
span: Span,
},
#[multipart_suggestion(
"{$num_to_remove ->
[one] remove the unused import
*[other] remove the unused imports
}",
applicability = "machine-applicable",
style = "tool-only"
)]
RemoveImports {
#[suggestion_part(code = "")]
remove_spans: Vec<Span>,
num_to_remove: usize,
},
}