diff --git a/src/librustc/middle/lint.rs b/src/librustc/middle/lint.rs index 24e76709ff5..48b9a11ef8d 100644 --- a/src/librustc/middle/lint.rs +++ b/src/librustc/middle/lint.rs @@ -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); diff --git a/src/test/compile-fail/lint-unsafe-block.rs b/src/test/compile-fail/lint-unsafe-block.rs new file mode 100644 index 00000000000..a43bdd99c01 --- /dev/null +++ b/src/test/compile-fail/lint-unsafe-block.rs @@ -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 or the MIT license +// , 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 +}