Skip to content

Commit dad074f

Browse files
committed
feat: make unsafe_code lint into a lint group
1 parent bd43458 commit dad074f

11 files changed

+331
-205
lines changed

compiler/rustc_lint/src/builtin.rs

+7-140
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
//! `late_lint_methods!` invocation in `lib.rs`.
2222
2323
use crate::fluent_generated as fluent;
24+
use crate::r#unsafe::{UNSAFE_OBLIGATION_DEFINE, UNSAFE_OBLIGATION_DISCHARGE};
2425
use crate::{
2526
errors::BuiltinEllpisisInclusiveRangePatterns,
2627
lints::{
@@ -36,22 +37,21 @@ use crate::{
3637
BuiltinTypeAliasGenericBoundsSuggestion, BuiltinTypeAliasWhereClause,
3738
BuiltinUnexpectedCliConfigName, BuiltinUnexpectedCliConfigValue,
3839
BuiltinUngatedAsyncFnTrackCaller, BuiltinUnnameableTestItems, BuiltinUnpermittedTypeInit,
39-
BuiltinUnpermittedTypeInitSub, BuiltinUnreachablePub, BuiltinUnsafe,
40-
BuiltinUnstableFeatures, BuiltinUnusedDocComment, BuiltinUnusedDocCommentSub,
41-
BuiltinWhileTrue, SuggestChangingAssocTypes,
40+
BuiltinUnpermittedTypeInitSub, BuiltinUnreachablePub, BuiltinUnstableFeatures,
41+
BuiltinUnusedDocComment, BuiltinUnusedDocCommentSub, BuiltinWhileTrue,
42+
SuggestChangingAssocTypes,
4243
},
4344
types::{transparent_newtype_field, CItemKind},
4445
EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext,
4546
};
4647
use hir::IsAsync;
4748
use rustc_ast::attr;
4849
use rustc_ast::tokenstream::{TokenStream, TokenTree};
49-
use rustc_ast::visit::{FnCtxt, FnKind};
5050
use rustc_ast::{self as ast, *};
5151
use rustc_ast_pretty::pprust::{self, expr_to_string};
5252
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
5353
use rustc_data_structures::stack::ensure_sufficient_stack;
54-
use rustc_errors::{Applicability, DecorateLint, MultiSpan};
54+
use rustc_errors::{Applicability, MultiSpan};
5555
use rustc_feature::{deprecated_attributes, AttributeGate, BuiltinAttribute, GateIssue, Stability};
5656
use rustc_hir as hir;
5757
use rustc_hir::def::{DefKind, Res};
@@ -287,140 +287,6 @@ impl<'tcx> LateLintPass<'tcx> for NonShorthandFieldPatterns {
287287
}
288288
}
289289

290-
declare_lint! {
291-
/// The `unsafe_code` lint catches usage of `unsafe` code.
292-
///
293-
/// ### Example
294-
///
295-
/// ```rust,compile_fail
296-
/// #![deny(unsafe_code)]
297-
/// fn main() {
298-
/// unsafe {
299-
///
300-
/// }
301-
/// }
302-
/// ```
303-
///
304-
/// {{produces}}
305-
///
306-
/// ### Explanation
307-
///
308-
/// This lint is intended to restrict the usage of `unsafe`, which can be
309-
/// difficult to use correctly.
310-
UNSAFE_CODE,
311-
Allow,
312-
"usage of `unsafe` code"
313-
}
314-
315-
declare_lint_pass!(UnsafeCode => [UNSAFE_CODE]);
316-
317-
impl UnsafeCode {
318-
fn report_unsafe(
319-
&self,
320-
cx: &EarlyContext<'_>,
321-
span: Span,
322-
decorate: impl for<'a> DecorateLint<'a, ()>,
323-
) {
324-
// This comes from a macro that has `#[allow_internal_unsafe]`.
325-
if span.allows_unsafe() {
326-
return;
327-
}
328-
329-
cx.emit_spanned_lint(UNSAFE_CODE, span, decorate);
330-
}
331-
}
332-
333-
impl EarlyLintPass for UnsafeCode {
334-
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) {
335-
if attr.has_name(sym::allow_internal_unsafe) {
336-
self.report_unsafe(cx, attr.span, BuiltinUnsafe::AllowInternalUnsafe);
337-
}
338-
}
339-
340-
#[inline]
341-
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
342-
if let ast::ExprKind::Block(ref blk, _) = e.kind {
343-
// Don't warn about generated blocks; that'll just pollute the output.
344-
if blk.rules == ast::BlockCheckMode::Unsafe(ast::UserProvided) {
345-
self.report_unsafe(cx, blk.span, BuiltinUnsafe::UnsafeBlock);
346-
}
347-
}
348-
}
349-
350-
fn check_item(&mut self, cx: &EarlyContext<'_>, it: &ast::Item) {
351-
match it.kind {
352-
ast::ItemKind::Trait(box ast::Trait { unsafety: ast::Unsafe::Yes(_), .. }) => {
353-
self.report_unsafe(cx, it.span, BuiltinUnsafe::UnsafeTrait);
354-
}
355-
356-
ast::ItemKind::Impl(box ast::Impl { unsafety: ast::Unsafe::Yes(_), .. }) => {
357-
self.report_unsafe(cx, it.span, BuiltinUnsafe::UnsafeImpl);
358-
}
359-
360-
ast::ItemKind::Fn(..) => {
361-
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::no_mangle) {
362-
self.report_unsafe(cx, attr.span, BuiltinUnsafe::NoMangleFn);
363-
}
364-
365-
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) {
366-
self.report_unsafe(cx, attr.span, BuiltinUnsafe::ExportNameFn);
367-
}
368-
369-
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::link_section) {
370-
self.report_unsafe(cx, attr.span, BuiltinUnsafe::LinkSectionFn);
371-
}
372-
}
373-
374-
ast::ItemKind::Static(..) => {
375-
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::no_mangle) {
376-
self.report_unsafe(cx, attr.span, BuiltinUnsafe::NoMangleStatic);
377-
}
378-
379-
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) {
380-
self.report_unsafe(cx, attr.span, BuiltinUnsafe::ExportNameStatic);
381-
}
382-
383-
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::link_section) {
384-
self.report_unsafe(cx, attr.span, BuiltinUnsafe::LinkSectionStatic);
385-
}
386-
}
387-
388-
_ => {}
389-
}
390-
}
391-
392-
fn check_impl_item(&mut self, cx: &EarlyContext<'_>, it: &ast::AssocItem) {
393-
if let ast::AssocItemKind::Fn(..) = it.kind {
394-
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::no_mangle) {
395-
self.report_unsafe(cx, attr.span, BuiltinUnsafe::NoMangleMethod);
396-
}
397-
if let Some(attr) = cx.sess().find_by_name(&it.attrs, sym::export_name) {
398-
self.report_unsafe(cx, attr.span, BuiltinUnsafe::ExportNameMethod);
399-
}
400-
}
401-
}
402-
403-
fn check_fn(&mut self, cx: &EarlyContext<'_>, fk: FnKind<'_>, span: Span, _: ast::NodeId) {
404-
if let FnKind::Fn(
405-
ctxt,
406-
_,
407-
ast::FnSig { header: ast::FnHeader { unsafety: ast::Unsafe::Yes(_), .. }, .. },
408-
_,
409-
_,
410-
body,
411-
) = fk
412-
{
413-
let decorator = match ctxt {
414-
FnCtxt::Foreign => return,
415-
FnCtxt::Free => BuiltinUnsafe::DeclUnsafeFn,
416-
FnCtxt::Assoc(_) if body.is_none() => BuiltinUnsafe::DeclUnsafeMethod,
417-
FnCtxt::Assoc(_) => BuiltinUnsafe::ImplUnsafeMethod,
418-
};
419-
self.report_unsafe(cx, span, decorator);
420-
}
421-
}
422-
}
423-
424290
declare_lint! {
425291
/// The `missing_docs` lint detects missing documentation for public items.
426292
///
@@ -1633,7 +1499,8 @@ declare_lint_pass!(
16331499
WHILE_TRUE,
16341500
BOX_POINTERS,
16351501
NON_SHORTHAND_FIELD_PATTERNS,
1636-
UNSAFE_CODE,
1502+
UNSAFE_OBLIGATION_DEFINE,
1503+
UNSAFE_OBLIGATION_DISCHARGE,
16371504
MISSING_DOCS,
16381505
MISSING_COPY_IMPLEMENTATIONS,
16391506
MISSING_DEBUG_IMPLEMENTATIONS,

compiler/rustc_lint/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ mod passes;
7676
mod redundant_semicolon;
7777
mod traits;
7878
mod types;
79+
mod r#unsafe;
7980
mod unused;
8081

8182
pub use array_into_iter::ARRAY_INTO_ITER;
@@ -92,6 +93,7 @@ use rustc_session::lint::builtin::{
9293
};
9394
use rustc_span::symbol::Ident;
9495
use rustc_span::Span;
96+
use r#unsafe::UnsafeCode;
9597

9698
use array_into_iter::ArrayIntoIter;
9799
use builtin::*;
@@ -126,6 +128,8 @@ pub use rustc_session::lint::Level::{self, *};
126128
pub use rustc_session::lint::{BufferedEarlyLint, FutureIncompatibleInfo, Lint, LintId};
127129
pub use rustc_session::lint::{LintArray, LintPass};
128130

131+
use crate::r#unsafe::{UNSAFE_OBLIGATION_DEFINE, UNSAFE_OBLIGATION_DISCHARGE};
132+
129133
fluent_messages! { "../messages.ftl" }
130134

131135
pub fn provide(providers: &mut Providers) {
@@ -272,6 +276,8 @@ fn register_builtins(store: &mut LintStore) {
272276
store.register_lints(&BuiltinCombinedModuleLateLintPass::get_lints());
273277
store.register_lints(&BuiltinCombinedLateLintPass::get_lints());
274278

279+
add_lint_group!("unsafe_code", UNSAFE_OBLIGATION_DEFINE, UNSAFE_OBLIGATION_DISCHARGE);
280+
275281
add_lint_group!(
276282
"nonstandard_style",
277283
NON_CAMEL_CASE_TYPES,

0 commit comments

Comments
 (0)