Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate lint reports in typeck::check_unused to LintDiagnostic #100674

Merged
merged 4 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/typeck.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,11 @@ typeck_manual_implementation =
.help = add `#![feature(unboxed_closures)]` to the crate attributes to enable
typeck_substs_on_overridden_impl = could not resolve substs on overridden impl
typeck_unused_extern_crate =
unused extern crate
.suggestion = remove it
typeck_extern_crate_not_idiomatic =
`extern crate` is not idiomatic in the new edition
.suggestion = convert it to a `{$msg_code}`
63 changes: 27 additions & 36 deletions compiler/rustc_typeck/src/check_unused.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::errors::{ExternCrateNotIdiomatic, UnusedExternCrate};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId};
Expand Down Expand Up @@ -108,25 +108,16 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) {
// We do this in any edition.
if extern_crate.warn_if_unused {
if let Some(&span) = unused_extern_crates.get(&def_id) {
// Removal suggestion span needs to include attributes (Issue #54400)
let id = tcx.hir().local_def_id_to_hir_id(def_id);
tcx.struct_span_lint_hir(lint, id, span, |lint| {
// Removal suggestion span needs to include attributes (Issue #54400)
let span_with_attrs = tcx
.hir()
.attrs(id)
.iter()
.map(|attr| attr.span)
.fold(span, |acc, attr_span| acc.to(attr_span));

lint.build("unused extern crate")
.span_suggestion_short(
span_with_attrs,
"remove it",
"",
Applicability::MachineApplicable,
)
.emit();
});
let span_with_attrs = tcx
.hir()
.attrs(id)
.iter()
.map(|attr| attr.span)
.fold(span, |acc, attr_span| acc.to(attr_span));

tcx.emit_spanned_lint(lint, id, span, UnusedExternCrate { span: span_with_attrs });
continue;
}
}
Expand Down Expand Up @@ -158,23 +149,23 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) {
if !tcx.hir().attrs(id).is_empty() {
continue;
}
tcx.struct_span_lint_hir(lint, id, extern_crate.span, |lint| {
// Otherwise, we can convert it into a `use` of some kind.
let base_replacement = match extern_crate.orig_name {
Some(orig_name) => format!("use {} as {};", orig_name, item.ident.name),
None => format!("use {};", item.ident.name),
};
let vis = tcx.sess.source_map().span_to_snippet(item.vis_span).unwrap_or_default();
let add_vis = |to| if vis.is_empty() { to } else { format!("{} {}", vis, to) };
lint.build("`extern crate` is not idiomatic in the new edition")
.span_suggestion_short(
extern_crate.span,
&format!("convert it to a `{}`", add_vis("use".to_string())),
add_vis(base_replacement),
Applicability::MachineApplicable,
)
.emit();
})

let base_replacement = match extern_crate.orig_name {
Some(orig_name) => format!("use {} as {};", orig_name, item.ident.name),
None => format!("use {};", item.ident.name),
};
let vis = tcx.sess.source_map().span_to_snippet(item.vis_span).unwrap_or_default();
let add_vis = |to| if vis.is_empty() { to } else { format!("{} {}", vis, to) };
tcx.emit_spanned_lint(
lint,
id,
extern_crate.span,
ExternCrateNotIdiomatic {
span: extern_crate.span,
msg_code: add_vis("use".to_string()),
suggestion_code: add_vis(base_replacement),
},
);
}
}

Expand Down
20 changes: 19 additions & 1 deletion compiler/rustc_typeck/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Errors emitted by typeck.
use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed};
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic};
use rustc_middle::ty::Ty;
use rustc_session::{parse::ParseSess, SessionDiagnostic};
use rustc_span::{symbol::Ident, Span, Symbol};
Expand Down Expand Up @@ -324,3 +324,21 @@ pub struct SubstsOnOverriddenImpl {
#[primary_span]
pub span: Span,
}

#[derive(LintDiagnostic)]
#[lint(typeck::unused_extern_crate)]
pub struct UnusedExternCrate {
#[primary_span]
davidtwco marked this conversation as resolved.
Show resolved Hide resolved
#[suggestion(applicability = "machine-applicable", code = "")]
pub span: Span,
}

#[derive(LintDiagnostic)]
#[lint(typeck::extern_crate_not_idiomatic)]
pub struct ExternCrateNotIdiomatic {
#[primary_span]
#[suggestion_short(applicability = "machine-applicable", code = "{suggestion_code}")]
pub span: Span,
pub msg_code: String,
pub suggestion_code: String,
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
error: unused extern crate
--> $DIR/issue-54400-unused-extern-crate-attr-span.rs:12:1
--> $DIR/issue-54400-unused-extern-crate-attr-span.rs:11:1
|
LL | / #[cfg(blandiloquence)]
LL | | extern crate edition_lint_paths;
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |________________________________|
| help: remove it
| |________________________________^ help: remove it
|
note: the lint level is defined here
--> $DIR/issue-54400-unused-extern-crate-attr-span.rs:6:9
Expand Down