Skip to content

Commit be0dc49

Browse files
committed
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
1 parent 0b664bb commit be0dc49

File tree

4 files changed

+103
-37
lines changed

4 files changed

+103
-37
lines changed

src/librustc/lint/builtin.rs

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -1269,27 +1269,71 @@ impl LintPass for UnusedUnsafe {
12691269
}
12701270

12711271
declare_lint! {
1272-
UNSAFE_BLOCKS,
1272+
UNSAFE_CODE,
12731273
Allow,
1274-
"usage of an `unsafe` block"
1274+
"usage of `unsafe` code"
12751275
}
12761276

12771277
#[derive(Copy)]
1278-
pub struct UnsafeBlocks;
1278+
pub struct UnsafeCode;
12791279

1280-
impl LintPass for UnsafeBlocks {
1280+
impl LintPass for UnsafeCode {
12811281
fn get_lints(&self) -> LintArray {
1282-
lint_array!(UNSAFE_BLOCKS)
1282+
lint_array!(UNSAFE_CODE)
12831283
}
12841284

12851285
fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
12861286
if let ast::ExprBlock(ref blk) = e.node {
12871287
// Don't warn about generated blocks, that'll just pollute the output.
12881288
if blk.rules == ast::UnsafeBlock(ast::UserProvided) {
1289-
cx.span_lint(UNSAFE_BLOCKS, blk.span, "usage of an `unsafe` block");
1289+
cx.span_lint(UNSAFE_CODE, blk.span, "usage of an `unsafe` block");
12901290
}
12911291
}
12921292
}
1293+
1294+
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
1295+
use syntax::ast::Unsafety::Unsafe;
1296+
1297+
fn check_method(cx: &Context, meth: &P<ast::Method>) {
1298+
if let ast::Method_::MethDecl(_, _, _, _, Unsafe, _, _, _) = meth.node {
1299+
cx.span_lint(UNSAFE_CODE, meth.span, "implementation of an `unsafe` method");
1300+
}
1301+
}
1302+
1303+
match it.node {
1304+
ast::ItemFn(_, Unsafe, _, _, _) =>
1305+
cx.span_lint(UNSAFE_CODE, it.span, "declaration of an `unsafe` function"),
1306+
1307+
ast::ItemTrait(trait_safety, _, _, ref items) => {
1308+
if trait_safety == Unsafe {
1309+
cx.span_lint(UNSAFE_CODE, it.span, "declaration of an `unsafe` trait");
1310+
}
1311+
1312+
for it in items {
1313+
match *it {
1314+
ast::RequiredMethod(ast::TypeMethod { unsafety: Unsafe, span, ..}) =>
1315+
cx.span_lint(UNSAFE_CODE, span, "declaration of an `unsafe` method"),
1316+
ast::ProvidedMethod(ref meth) => check_method(cx, meth),
1317+
_ => (),
1318+
}
1319+
}
1320+
},
1321+
1322+
ast::ItemImpl(impl_safety, _, _, _, _, ref impls) => {
1323+
if impl_safety == Unsafe {
1324+
cx.span_lint(UNSAFE_CODE, it.span, "implementation of an `unsafe` trait");
1325+
}
1326+
1327+
for item in impls {
1328+
if let ast::ImplItem::MethodImplItem(ref meth) = *item {
1329+
check_method(cx, meth);
1330+
}
1331+
}
1332+
},
1333+
1334+
_ => return,
1335+
}
1336+
}
12931337
}
12941338

12951339
declare_lint! {

src/librustc/lint/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -206,7 +206,7 @@ impl LintStore {
206206
UnusedImportBraces,
207207
NonShorthandFieldPatterns,
208208
UnusedUnsafe,
209-
UnsafeBlocks,
209+
UnsafeCode,
210210
UnusedMut,
211211
UnusedAllocation,
212212
MissingCopyImplementations,

src/test/compile-fail/lint-unsafe-block.rs

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(unused_unsafe)]
12+
#![allow(dead_code)]
13+
#![deny(unsafe_code)]
14+
15+
struct Bar;
16+
17+
#[allow(unsafe_code)]
18+
mod allowed_unsafe {
19+
fn allowed() { unsafe {} }
20+
unsafe fn also_allowed() {}
21+
unsafe trait AllowedUnsafe {}
22+
unsafe impl AllowedUnsafe for super::Bar {}
23+
}
24+
25+
macro_rules! unsafe_in_macro {
26+
() => {
27+
unsafe {} //~ ERROR: usage of an `unsafe` block
28+
}
29+
}
30+
31+
unsafe fn baz() {} //~ ERROR: declaration of an `unsafe` function
32+
unsafe trait Foo {} //~ ERROR: declaration of an `unsafe` trait
33+
unsafe impl Foo for Bar {} //~ ERROR: implementation of an `unsafe` trait
34+
35+
trait Baz {
36+
unsafe fn baz(&self); //~ ERROR: declaration of an `unsafe` method
37+
unsafe fn provided(&self) {} //~ ERROR: implementation of an `unsafe` method
38+
unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method
39+
}
40+
41+
impl Baz for Bar {
42+
unsafe fn baz(&self) {} //~ ERROR: implementation of an `unsafe` method
43+
unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method
44+
}
45+
46+
fn main() {
47+
unsafe {} //~ ERROR: usage of an `unsafe` block
48+
49+
unsafe_in_macro!()
50+
}

0 commit comments

Comments
 (0)