Skip to content

Commit 3de034d

Browse files
authored
Rollup merge of #100674 - PragmaTwice:mig-typeck-unused-crate-diag, r=davidtwco
Migrate lint reports in typeck::check_unused to LintDiagnostic In this PR, I migrate two lint reports in `typeck::check_unused` by `LintDiagnostic`, all of which is about extern crates. ```@rustbot``` label +A-translation r? rust-lang/diagnostics
2 parents 5548e58 + 9efe979 commit 3de034d

File tree

3 files changed

+52
-37
lines changed

3 files changed

+52
-37
lines changed

compiler/rustc_error_messages/locales/en-US/typeck.ftl

+8
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,11 @@ typeck_manual_implementation =
123123
.help = add `#![feature(unboxed_closures)]` to the crate attributes to enable
124124
125125
typeck_substs_on_overridden_impl = could not resolve substs on overridden impl
126+
127+
typeck_unused_extern_crate =
128+
unused extern crate
129+
.suggestion = remove it
130+
131+
typeck_extern_crate_not_idiomatic =
132+
`extern crate` is not idiomatic in the new edition
133+
.suggestion = convert it to a `{$msg_code}`

compiler/rustc_typeck/src/check_unused.rs

+27-36
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
use crate::errors::{ExternCrateNotIdiomatic, UnusedExternCrate};
12
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
2-
use rustc_errors::Applicability;
33
use rustc_hir as hir;
44
use rustc_hir::def::DefKind;
55
use rustc_hir::def_id::{DefId, LocalDefId};
@@ -108,25 +108,16 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) {
108108
// We do this in any edition.
109109
if extern_crate.warn_if_unused {
110110
if let Some(&span) = unused_extern_crates.get(&def_id) {
111+
// Removal suggestion span needs to include attributes (Issue #54400)
111112
let id = tcx.hir().local_def_id_to_hir_id(def_id);
112-
tcx.struct_span_lint_hir(lint, id, span, |lint| {
113-
// Removal suggestion span needs to include attributes (Issue #54400)
114-
let span_with_attrs = tcx
115-
.hir()
116-
.attrs(id)
117-
.iter()
118-
.map(|attr| attr.span)
119-
.fold(span, |acc, attr_span| acc.to(attr_span));
120-
121-
lint.build("unused extern crate")
122-
.span_suggestion_short(
123-
span_with_attrs,
124-
"remove it",
125-
"",
126-
Applicability::MachineApplicable,
127-
)
128-
.emit();
129-
});
113+
let span_with_attrs = tcx
114+
.hir()
115+
.attrs(id)
116+
.iter()
117+
.map(|attr| attr.span)
118+
.fold(span, |acc, attr_span| acc.to(attr_span));
119+
120+
tcx.emit_spanned_lint(lint, id, span, UnusedExternCrate { span: span_with_attrs });
130121
continue;
131122
}
132123
}
@@ -158,23 +149,23 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) {
158149
if !tcx.hir().attrs(id).is_empty() {
159150
continue;
160151
}
161-
tcx.struct_span_lint_hir(lint, id, extern_crate.span, |lint| {
162-
// Otherwise, we can convert it into a `use` of some kind.
163-
let base_replacement = match extern_crate.orig_name {
164-
Some(orig_name) => format!("use {} as {};", orig_name, item.ident.name),
165-
None => format!("use {};", item.ident.name),
166-
};
167-
let vis = tcx.sess.source_map().span_to_snippet(item.vis_span).unwrap_or_default();
168-
let add_vis = |to| if vis.is_empty() { to } else { format!("{} {}", vis, to) };
169-
lint.build("`extern crate` is not idiomatic in the new edition")
170-
.span_suggestion_short(
171-
extern_crate.span,
172-
&format!("convert it to a `{}`", add_vis("use".to_string())),
173-
add_vis(base_replacement),
174-
Applicability::MachineApplicable,
175-
)
176-
.emit();
177-
})
152+
153+
let base_replacement = match extern_crate.orig_name {
154+
Some(orig_name) => format!("use {} as {};", orig_name, item.ident.name),
155+
None => format!("use {};", item.ident.name),
156+
};
157+
let vis = tcx.sess.source_map().span_to_snippet(item.vis_span).unwrap_or_default();
158+
let add_vis = |to| if vis.is_empty() { to } else { format!("{} {}", vis, to) };
159+
tcx.emit_spanned_lint(
160+
lint,
161+
id,
162+
extern_crate.span,
163+
ExternCrateNotIdiomatic {
164+
span: extern_crate.span,
165+
msg_code: add_vis("use".to_string()),
166+
suggestion_code: add_vis(base_replacement),
167+
},
168+
);
178169
}
179170
}
180171

compiler/rustc_typeck/src/errors.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Errors emitted by typeck.
22
use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed};
3-
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
3+
use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic};
44
use rustc_middle::ty::Ty;
55
use rustc_session::{parse::ParseSess, SessionDiagnostic};
66
use rustc_span::{symbol::Ident, Span, Symbol};
@@ -324,3 +324,19 @@ pub struct SubstsOnOverriddenImpl {
324324
#[primary_span]
325325
pub span: Span,
326326
}
327+
328+
#[derive(LintDiagnostic)]
329+
#[lint(typeck::unused_extern_crate)]
330+
pub struct UnusedExternCrate {
331+
#[suggestion(applicability = "machine-applicable", code = "")]
332+
pub span: Span,
333+
}
334+
335+
#[derive(LintDiagnostic)]
336+
#[lint(typeck::extern_crate_not_idiomatic)]
337+
pub struct ExternCrateNotIdiomatic {
338+
#[suggestion_short(applicability = "machine-applicable", code = "{suggestion_code}")]
339+
pub span: Span,
340+
pub msg_code: String,
341+
pub suggestion_code: String,
342+
}

0 commit comments

Comments
 (0)