add lint for unsafe blocks

This commit is contained in:
Daniel Micay 2013-11-21 17:36:54 -05:00
parent 61e07d6792
commit a1afe9cc0a
2 changed files with 40 additions and 2 deletions

View File

@ -75,6 +75,7 @@ pub enum lint {
type_limits,
type_overflow,
unused_unsafe,
unsafe_block,
managed_heap_memory,
owned_heap_memory,
@ -236,6 +237,13 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
default: warn
}),
("unsafe_block",
LintSpec {
lint: unsafe_block,
desc: "usage of an `unsafe` block",
default: allow
}),
("unused_variable",
LintSpec {
lint: unused_variable,
@ -870,8 +878,7 @@ fn check_pat_non_uppercase_statics(cx: &Context, p: &ast::Pat) {
fn check_unused_unsafe(cx: &Context, e: &ast::Expr) {
match e.node {
// Don't warn about generated blocks, that'll just pollute the
// output.
// Don't warn about generated blocks, that'll just pollute the output.
ast::ExprBlock(ref blk) => {
if blk.rules == ast::UnsafeBlock(ast::UserProvided) &&
!cx.tcx.used_unsafe.contains(&blk.id) {
@ -883,6 +890,16 @@ fn check_unused_unsafe(cx: &Context, e: &ast::Expr) {
}
}
fn check_unsafe_block(cx: &Context, e: &ast::Expr) {
match e.node {
// Don't warn about generated blocks, that'll just pollute the output.
ast::ExprBlock(ref blk) if blk.rules == ast::UnsafeBlock(ast::UserProvided) => {
cx.span_lint(unsafe_block, blk.span, "usage of an `unsafe` block");
}
_ => ()
}
}
fn check_unused_mut_pat(cx: &Context, p: @ast::Pat) {
match p.node {
ast::PatIdent(ast::BindByValue(ast::MutMutable),
@ -1126,6 +1143,7 @@ impl<'self> Visitor<()> for Context<'self> {
check_while_true_expr(self, e);
check_stability(self, e);
check_unused_unsafe(self, e);
check_unsafe_block(self, e);
check_unnecessary_allocation(self, e);
check_heap_expr(self, e);

View File

@ -0,0 +1,20 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[allow(unused_unsafe)];
#[deny(unsafe_block)];
unsafe fn allowed() {}
#[allow(unsafe_block)] fn also_allowed() { unsafe {} }
fn main() {
unsafe {} //~ ERROR: usage of an `unsafe` block
}