migrate: OverruledAttribute

This commit is contained in:
Rejyr 2022-08-19 20:47:05 -04:00
parent 32e445af74
commit 7a6ae2367d
3 changed files with 75 additions and 12 deletions

View File

@ -394,6 +394,16 @@ lint_builtin_deref_nullptr = dereferencing a null pointer
lint_builtin_asm_labels = avoid using named labels in inline assembly
lint_overruled_attribute = {$lint_level}({$lint_source}) incompatible with previous forbid
.label = overruled by previous forbid
lint_default_source = `forbid` lint level is the default for {$id}
lint_node_source = `forbid` level set here
.note = {$reason}
lint_command_line_source = `forbid` lint level was set on command line
lint_malformed_attribute = malformed lint attribute input
lint_bad_attribute_argument = bad attribute argument

View File

@ -1,6 +1,46 @@
use rustc_errors::{fluent, AddSubdiagnostic};
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
use rustc_span::{Span, Symbol};
#[derive(SessionDiagnostic)]
#[error(lint::overruled_attribute, code = "E0453")]
pub struct OverruledAttribute {
#[primary_span]
pub span: Span,
#[label]
pub overruled: Span,
pub lint_level: String,
pub lint_source: Symbol,
#[subdiagnostic]
pub sub: OverruledAttributeSub,
}
//
pub enum OverruledAttributeSub {
DefaultSource { id: String },
NodeSource { span: Span, reason: Option<Symbol> },
CommandLineSource,
}
impl AddSubdiagnostic for OverruledAttributeSub {
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
match self {
OverruledAttributeSub::DefaultSource { id } => {
diag.note(fluent::lint::default_source);
diag.set_arg("id", id);
}
OverruledAttributeSub::NodeSource { span, reason } => {
diag.span_label(span, fluent::lint::node_source);
if let Some(rationale) = reason {
diag.note(rationale.as_str());
}
}
OverruledAttributeSub::CommandLineSource => {
diag.note(fluent::lint::command_line_source);
}
}
}
}
#[derive(SessionDiagnostic)]
#[error(lint::malformed_attribute, code = "E0452")]
pub struct MalformedAttribute {

View File

@ -6,7 +6,7 @@ use crate::late::unerased_lint_store;
use rustc_ast as ast;
use rustc_ast_pretty::pprust;
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{struct_span_err, Applicability, Diagnostic, LintDiagnosticBuilder, MultiSpan};
use rustc_errors::{Applicability, Diagnostic, LintDiagnosticBuilder, MultiSpan};
use rustc_hir as hir;
use rustc_hir::{intravisit, HirId};
use rustc_middle::hir::nested_filter;
@ -26,7 +26,10 @@ use rustc_span::symbol::{sym, Symbol};
use rustc_span::{Span, DUMMY_SP};
use tracing::debug;
use crate::errors::{MalformedAttribute, MalformedAttributeSub, UnknownTool};
use crate::errors::{
MalformedAttribute, MalformedAttributeSub, OverruledAttribute, OverruledAttributeSub,
UnknownTool,
};
fn lint_levels(tcx: TyCtxt<'_>, (): ()) -> LintLevelMap {
let store = unerased_lint_store(tcx);
@ -191,16 +194,26 @@ impl<'s> LintLevelsBuilder<'s> {
}
};
if !fcw_warning {
let mut diag_builder = struct_span_err!(
self.sess,
src.span(),
E0453,
"{}({}) incompatible with previous forbid",
level.as_str(),
src.name(),
);
decorate_diag(&mut diag_builder);
diag_builder.emit();
self.sess.emit_err(OverruledAttribute {
span: src.span(),
overruled: src.span(),
lint_level: level.as_str().to_string(),
lint_source: src.name(),
sub: match old_src {
LintLevelSource::Default => {
OverruledAttributeSub::DefaultSource { id: id.to_string() }
}
LintLevelSource::Node(_, forbid_source_span, reason) => {
OverruledAttributeSub::NodeSource {
span: forbid_source_span,
reason,
}
}
LintLevelSource::CommandLine(_, _) => {
OverruledAttributeSub::CommandLineSource
}
},
});
} else {
self.struct_lint(
FORBIDDEN_LINT_GROUPS,