Skip to content

Commit

Permalink
auto merge of #10599 : thestinger/rust/unsafe, r=cmr
Browse files Browse the repository at this point in the history
This is just meant to be for containing usage of `unsafe`, much like `heap_memory`.
  • Loading branch information
bors committed Nov 22, 2013
2 parents 6143400 + a1afe9c commit 1dea21f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/librustc/middle/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub enum lint {
type_limits,
type_overflow,
unused_unsafe,
unsafe_block,

managed_heap_memory,
owned_heap_memory,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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),
Expand Down Expand Up @@ -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);

Expand Down
20 changes: 20 additions & 0 deletions src/test/compile-fail/lint-unsafe-block.rs
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 1dea21f

Please sign in to comment.