Skip to content

Properly reimplement unsafe-code lint to honor changing lint attributes #22752

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

Merged
merged 1 commit into from
Feb 25, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
54 changes: 22 additions & 32 deletions src/librustc/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1292,46 +1292,36 @@ impl LintPass for UnsafeCode {
}

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(ast::Unsafety::Unsafe, _, _, _) =>
cx.span_lint(UNSAFE_CODE, it.span, "declaration of an `unsafe` trait"),

ast::ItemTrait(trait_safety, _, _, ref items) => {
if trait_safety == Unsafe {
cx.span_lint(UNSAFE_CODE, it.span, "declaration of an `unsafe` trait");
}
ast::ItemImpl(ast::Unsafety::Unsafe, _, _, _, _, _) =>
cx.span_lint(UNSAFE_CODE, it.span, "implementation 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),
_ => (),
}
}
},
_ => return,
}
}

ast::ItemImpl(impl_safety, _, _, _, _, ref impls) => {
if impl_safety == Unsafe {
cx.span_lint(UNSAFE_CODE, it.span, "implementation of an `unsafe` trait");
}
fn check_fn(&mut self, cx: &Context, fk: visit::FnKind, _: &ast::FnDecl,
_: &ast::Block, span: Span, _: ast::NodeId) {
match fk {
visit::FkItemFn(_, _, ast::Unsafety::Unsafe, _) =>
cx.span_lint(UNSAFE_CODE, span, "declaration of an `unsafe` function"),

for item in impls {
if let ast::ImplItem::MethodImplItem(ref meth) = *item {
check_method(cx, meth);
}
visit::FkMethod(_, _, m) => {
if let ast::Method_::MethDecl(_, _, _, _, ast::Unsafety::Unsafe, _, _, _) = m.node {
cx.span_lint(UNSAFE_CODE, m.span, "implementation of an `unsafe` method")
}
},

_ => return,
_ => (),
}
}

fn check_ty_method(&mut self, cx: &Context, ty_method: &ast::TypeMethod) {
if let ast::TypeMethod { unsafety: ast::Unsafety::Unsafe, span, ..} = *ty_method {
cx.span_lint(UNSAFE_CODE, span, "declaration of an `unsafe` method")
}
}
}
Expand Down
49 changes: 49 additions & 0 deletions src/test/compile-fail/lint-unsafe-code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use std::marker::PhantomFn;

struct Bar;
struct Bar2;
struct Bar3;

#[allow(unsafe_code)]
mod allowed_unsafe {
Expand Down Expand Up @@ -46,6 +48,53 @@ impl Baz for Bar {
unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method
}


#[allow(unsafe_code)]
trait A {
unsafe fn allowed_unsafe(&self);
unsafe fn allowed_unsafe_provided(&self) {}
}

#[allow(unsafe_code)]
impl Baz for Bar2 {
unsafe fn baz(&self) {}
unsafe fn provided_override(&self) {}
}

impl Baz for Bar3 {
#[allow(unsafe_code)]
unsafe fn baz(&self) {}
unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method
}

#[allow(unsafe_code)]
unsafe trait B {
fn dummy(&self) {}
}

trait C {
#[allow(unsafe_code)]
unsafe fn baz(&self);
unsafe fn provided(&self) {} //~ ERROR: implementation of an `unsafe` method
}

impl C for Bar {
#[allow(unsafe_code)]
unsafe fn baz(&self) {}
unsafe fn provided(&self) {} //~ ERROR: implementation of an `unsafe` method
}

impl C for Bar2 {
unsafe fn baz(&self) {} //~ ERROR: implementation of an `unsafe` method
}

trait D {
#[allow(unsafe_code)]
unsafe fn unsafe_provided(&self) {}
}

impl D for Bar {}

fn main() {
unsafe {} //~ ERROR: usage of an `unsafe` block

Expand Down