提交 be0dc496 编写于 作者: I Ivan Petkov

Unsafe lint will also check for other unsafe code

Checks include declaration/implementation of unsafe functions, traits,
and methods.

This allows warning or forbidding all uses of unsafe code, whereas
previously only unsafe blocks were caught by the lint.

The lint has been renamed from `unsafe-blocks` to `unsafe-code` to
reflect its new purpose.

This is a minor [breaking-change]

Closes #22430
上级 0b664bb8
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
......@@ -1269,27 +1269,71 @@ fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
}
declare_lint! {
UNSAFE_BLOCKS,
UNSAFE_CODE,
Allow,
"usage of an `unsafe` block"
"usage of `unsafe` code"
}
#[derive(Copy)]
pub struct UnsafeBlocks;
pub struct UnsafeCode;
impl LintPass for UnsafeBlocks {
impl LintPass for UnsafeCode {
fn get_lints(&self) -> LintArray {
lint_array!(UNSAFE_BLOCKS)
lint_array!(UNSAFE_CODE)
}
fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
if let ast::ExprBlock(ref blk) = e.node {
// Don't warn about generated blocks, that'll just pollute the output.
if blk.rules == ast::UnsafeBlock(ast::UserProvided) {
cx.span_lint(UNSAFE_BLOCKS, blk.span, "usage of an `unsafe` block");
cx.span_lint(UNSAFE_CODE, blk.span, "usage of an `unsafe` block");
}
}
}
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
use syntax::ast::Unsafety::Unsafe;
fn check_method(cx: &Context, meth: &P<ast::Method>) {
if let ast::Method_::MethDecl(_, _, _, _, Unsafe, _, _, _) = meth.node {
cx.span_lint(UNSAFE_CODE, meth.span, "implementation of an `unsafe` method");
}
}
match it.node {
ast::ItemFn(_, Unsafe, _, _, _) =>
cx.span_lint(UNSAFE_CODE, it.span, "declaration of an `unsafe` function"),
ast::ItemTrait(trait_safety, _, _, ref items) => {
if trait_safety == Unsafe {
cx.span_lint(UNSAFE_CODE, it.span, "declaration of an `unsafe` trait");
}
for it in items {
match *it {
ast::RequiredMethod(ast::TypeMethod { unsafety: Unsafe, span, ..}) =>
cx.span_lint(UNSAFE_CODE, span, "declaration of an `unsafe` method"),
ast::ProvidedMethod(ref meth) => check_method(cx, meth),
_ => (),
}
}
},
ast::ItemImpl(impl_safety, _, _, _, _, ref impls) => {
if impl_safety == Unsafe {
cx.span_lint(UNSAFE_CODE, it.span, "implementation of an `unsafe` trait");
}
for item in impls {
if let ast::ImplItem::MethodImplItem(ref meth) = *item {
check_method(cx, meth);
}
}
},
_ => return,
}
}
}
declare_lint! {
......
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
......@@ -206,7 +206,7 @@ pub fn register_builtin(&mut self, sess: Option<&Session>) {
UnusedImportBraces,
NonShorthandFieldPatterns,
UnusedUnsafe,
UnsafeBlocks,
UnsafeCode,
UnusedMut,
UnusedAllocation,
MissingCopyImplementations,
......
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
......@@ -10,10 +10,17 @@
#![allow(unused_unsafe)]
#![allow(dead_code)]
#![deny(unsafe_blocks)]
unsafe fn allowed() {}
#![deny(unsafe_code)]
#[allow(unsafe_blocks)] fn also_allowed() { unsafe {} }
struct Bar;
#[allow(unsafe_code)]
mod allowed_unsafe {
fn allowed() { unsafe {} }
unsafe fn also_allowed() {}
unsafe trait AllowedUnsafe {}
unsafe impl AllowedUnsafe for super::Bar {}
}
macro_rules! unsafe_in_macro {
() => {
......@@ -21,6 +28,21 @@ unsafe fn allowed() {}
}
}
unsafe fn baz() {} //~ ERROR: declaration of an `unsafe` function
unsafe trait Foo {} //~ ERROR: declaration of an `unsafe` trait
unsafe impl Foo for Bar {} //~ ERROR: implementation of an `unsafe` trait
trait Baz {
unsafe fn baz(&self); //~ ERROR: declaration of an `unsafe` method
unsafe fn provided(&self) {} //~ ERROR: implementation of an `unsafe` method
unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method
}
impl Baz for Bar {
unsafe fn baz(&self) {} //~ ERROR: implementation of an `unsafe` method
unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method
}
fn main() {
unsafe {} //~ ERROR: usage of an `unsafe` block
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册