migrate: UnsafeCode in builtin.rs

This commit is contained in:
Rejyr 2022-11-12 20:18:55 -05:00
parent c06a2426b2
commit 587d49766b
2 changed files with 63 additions and 83 deletions

View File

@ -31,8 +31,8 @@ use crate::{
BuiltinMutablesTransmutes, BuiltinNoMangleGeneric, BuiltinNonShorthandFieldPatterns, BuiltinMutablesTransmutes, BuiltinNoMangleGeneric, BuiltinNonShorthandFieldPatterns,
BuiltinSpecialModuleNameUsed, BuiltinTrivialBounds, BuiltinUnexpectedCliConfigName, BuiltinSpecialModuleNameUsed, BuiltinTrivialBounds, BuiltinUnexpectedCliConfigName,
BuiltinUnexpectedCliConfigValue, BuiltinUnnameableTestItems, BuiltinUnreachablePub, BuiltinUnexpectedCliConfigValue, BuiltinUnnameableTestItems, BuiltinUnreachablePub,
BuiltinUnstableFeatures, BuiltinUnusedDocComment, BuiltinUnusedDocCommentSub, BuiltinUnsafe, BuiltinUnstableFeatures, BuiltinUnusedDocComment,
BuiltinWhileTrue, BuiltinUnusedDocCommentSub, BuiltinWhileTrue,
}, },
types::{transparent_newtype_field, CItemKind}, types::{transparent_newtype_field, CItemKind},
EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext, EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext,
@ -46,8 +46,7 @@ use rustc_ast_pretty::pprust::{self, expr_to_string};
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_errors::{ use rustc_errors::{
fluent, Applicability, DelayDm, Diagnostic, DiagnosticBuilder, DiagnosticMessage, fluent, Applicability, DecorateLint, DelayDm, Diagnostic, DiagnosticStyledString, MultiSpan,
DiagnosticStyledString, MultiSpan,
}; };
use rustc_feature::{deprecated_attributes, AttributeGate, BuiltinAttribute, GateIssue, Stability}; use rustc_feature::{deprecated_attributes, AttributeGate, BuiltinAttribute, GateIssue, Stability};
use rustc_hir as hir; use rustc_hir as hir;
@ -314,48 +313,21 @@ impl UnsafeCode {
&self, &self,
cx: &EarlyContext<'_>, cx: &EarlyContext<'_>,
span: Span, span: Span,
msg: impl Into<DiagnosticMessage>, decorate: impl for<'a> DecorateLint<'a, ()>,
decorate: impl for<'a, 'b> FnOnce(
&'b mut DiagnosticBuilder<'a, ()>,
) -> &'b mut DiagnosticBuilder<'a, ()>,
) { ) {
// This comes from a macro that has `#[allow_internal_unsafe]`. // This comes from a macro that has `#[allow_internal_unsafe]`.
if span.allows_unsafe() { if span.allows_unsafe() {
return; return;
} }
cx.struct_span_lint(UNSAFE_CODE, span, msg, decorate); cx.emit_spanned_lint(UNSAFE_CODE, span, decorate);
}
fn report_overridden_symbol_name(
&self,
cx: &EarlyContext<'_>,
span: Span,
msg: DiagnosticMessage,
) {
self.report_unsafe(cx, span, msg, |lint| {
lint.note(fluent::lint_builtin_overridden_symbol_name)
})
}
fn report_overridden_symbol_section(
&self,
cx: &EarlyContext<'_>,
span: Span,
msg: DiagnosticMessage,
) {
self.report_unsafe(cx, span, msg, |lint| {
lint.note(fluent::lint_builtin_overridden_symbol_section)
})
} }
} }
impl EarlyLintPass for UnsafeCode { impl EarlyLintPass for UnsafeCode {
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) { fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) {
if attr.has_name(sym::allow_internal_unsafe) { if attr.has_name(sym::allow_internal_unsafe) {
self.report_unsafe(cx, attr.span, fluent::lint_builtin_allow_internal_unsafe, |lint| { self.report_unsafe(cx, attr.span, BuiltinUnsafe::AllowInternalUnsafe);
lint
});
} }
} }
@ -364,7 +336,7 @@ impl EarlyLintPass for UnsafeCode {
if let ast::ExprKind::Block(ref blk, _) = e.kind { if let ast::ExprKind::Block(ref blk, _) = e.kind {
// Don't warn about generated blocks; that'll just pollute the output. // Don't warn about generated blocks; that'll just pollute the output.
if blk.rules == ast::BlockCheckMode::Unsafe(ast::UserProvided) { if blk.rules == ast::BlockCheckMode::Unsafe(ast::UserProvided) {
self.report_unsafe(cx, blk.span, fluent::lint_builtin_unsafe_block, |lint| lint); self.report_unsafe(cx, blk.span, BuiltinUnsafe::UnsafeBlock);
} }
} }
} }
@ -372,62 +344,38 @@ impl EarlyLintPass for UnsafeCode {
fn check_item(&mut self, cx: &EarlyContext<'_>, it: &ast::Item) { fn check_item(&mut self, cx: &EarlyContext<'_>, it: &ast::Item) {
match it.kind { match it.kind {
ast::ItemKind::Trait(box ast::Trait { unsafety: ast::Unsafe::Yes(_), .. }) => { ast::ItemKind::Trait(box ast::Trait { unsafety: ast::Unsafe::Yes(_), .. }) => {
self.report_unsafe(cx, it.span, fluent::lint_builtin_unsafe_trait, |lint| lint) self.report_unsafe(cx, it.span, BuiltinUnsafe::UnsafeTrait);
} }
ast::ItemKind::Impl(box ast::Impl { unsafety: ast::Unsafe::Yes(_), .. }) => { ast::ItemKind::Impl(box ast::Impl { unsafety: ast::Unsafe::Yes(_), .. }) => {
self.report_unsafe(cx, it.span, fluent::lint_builtin_unsafe_impl, |lint| lint) self.report_unsafe(cx, it.span, BuiltinUnsafe::UnsafeImpl);
} }
ast::ItemKind::Fn(..) => { ast::ItemKind::Fn(..) => {
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::no_mangle) { if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::no_mangle) {
self.report_overridden_symbol_name( self.report_unsafe(cx, attr.span, BuiltinUnsafe::NoMangleFn);
cx,
attr.span,
fluent::lint_builtin_no_mangle_fn,
);
} }
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) { if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) {
self.report_overridden_symbol_name( self.report_unsafe(cx, attr.span, BuiltinUnsafe::ExportNameFn);
cx,
attr.span,
fluent::lint_builtin_export_name_fn,
);
} }
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::link_section) { if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::link_section) {
self.report_overridden_symbol_section( self.report_unsafe(cx, attr.span, BuiltinUnsafe::LinkSectionFn);
cx,
attr.span,
fluent::lint_builtin_link_section_fn,
);
} }
} }
ast::ItemKind::Static(..) => { ast::ItemKind::Static(..) => {
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::no_mangle) { if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::no_mangle) {
self.report_overridden_symbol_name( self.report_unsafe(cx, attr.span, BuiltinUnsafe::NoMangleStatic);
cx,
attr.span,
fluent::lint_builtin_no_mangle_static,
);
} }
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) { if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) {
self.report_overridden_symbol_name( self.report_unsafe(cx, attr.span, BuiltinUnsafe::ExportNameStatic);
cx,
attr.span,
fluent::lint_builtin_export_name_static,
);
} }
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::link_section) { if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::link_section) {
self.report_overridden_symbol_section( self.report_unsafe(cx, attr.span, BuiltinUnsafe::LinkSectionStatic);
cx,
attr.span,
fluent::lint_builtin_link_section_static,
);
} }
} }
@ -438,18 +386,10 @@ impl EarlyLintPass for UnsafeCode {
fn check_impl_item(&mut self, cx: &EarlyContext<'_>, it: &ast::AssocItem) { fn check_impl_item(&mut self, cx: &EarlyContext<'_>, it: &ast::AssocItem) {
if let ast::AssocItemKind::Fn(..) = it.kind { if let ast::AssocItemKind::Fn(..) = it.kind {
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::no_mangle) { if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::no_mangle) {
self.report_overridden_symbol_name( self.report_unsafe(cx, attr.span, BuiltinUnsafe::NoMangleMethod);
cx,
attr.span,
fluent::lint_builtin_no_mangle_method,
);
} }
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) { if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) {
self.report_overridden_symbol_name( self.report_unsafe(cx, attr.span, BuiltinUnsafe::ExportNameMethod);
cx,
attr.span,
fluent::lint_builtin_export_name_method,
);
} }
} }
} }
@ -464,13 +404,13 @@ impl EarlyLintPass for UnsafeCode {
body, body,
) = fk ) = fk
{ {
let msg = match ctxt { let decorator = match ctxt {
FnCtxt::Foreign => return, FnCtxt::Foreign => return,
FnCtxt::Free => fluent::lint_builtin_decl_unsafe_fn, FnCtxt::Free => BuiltinUnsafe::DeclUnsafeFn,
FnCtxt::Assoc(_) if body.is_none() => fluent::lint_builtin_decl_unsafe_method, FnCtxt::Assoc(_) if body.is_none() => BuiltinUnsafe::DeclUnsafeMethod,
FnCtxt::Assoc(_) => fluent::lint_builtin_impl_unsafe_method, FnCtxt::Assoc(_) => BuiltinUnsafe::ImplUnsafeMethod,
}; };
self.report_unsafe(cx, span, msg, |lint| lint); self.report_unsafe(cx, span, decorator);
} }
} }
} }

View File

@ -61,7 +61,47 @@ pub struct BuiltinNonShorthandFieldPatterns {
pub prefix: &'static str, pub prefix: &'static str,
} }
// FIXME: add lint::unsafe_code #[derive(LintDiagnostic)]
pub enum BuiltinUnsafe {
#[diag(lint_builtin_allow_internal_unsafe)]
AllowInternalUnsafe,
#[diag(lint_builtin_unsafe_block)]
UnsafeBlock,
#[diag(lint_builtin_unsafe_trait)]
UnsafeTrait,
#[diag(lint_builtin_unsafe_impl)]
UnsafeImpl,
#[diag(lint_builtin_no_mangle_fn)]
#[note(lint_builtin_overridden_symbol_name)]
NoMangleFn,
#[diag(lint_builtin_export_name_fn)]
#[note(lint_builtin_overridden_symbol_name)]
ExportNameFn,
#[diag(lint_builtin_link_section_fn)]
#[note(lint_builtin_overridden_symbol_section)]
LinkSectionFn,
#[diag(lint_builtin_no_mangle_static)]
#[note(lint_builtin_overridden_symbol_name)]
NoMangleStatic,
#[diag(lint_builtin_export_name_static)]
#[note(lint_builtin_overridden_symbol_name)]
ExportNameStatic,
#[diag(lint_builtin_link_section_static)]
#[note(lint_builtin_overridden_symbol_section)]
LinkSectionStatic,
#[diag(lint_builtin_no_mangle_method)]
#[note(lint_builtin_overridden_symbol_name)]
NoMangleMethod,
#[diag(lint_builtin_export_name_method)]
#[note(lint_builtin_overridden_symbol_name)]
ExportNameMethod,
#[diag(lint_builtin_decl_unsafe_fn)]
DeclUnsafeFn,
#[diag(lint_builtin_decl_unsafe_method)]
DeclUnsafeMethod,
#[diag(lint_builtin_impl_unsafe_method)]
ImplUnsafeMethod,
}
#[derive(LintDiagnostic)] #[derive(LintDiagnostic)]
#[diag(lint_builtin_missing_doc)] #[diag(lint_builtin_missing_doc)]