about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-11-21 17:26:31 -0800
committerbors <bors@rust-lang.org>2013-11-21 17:26:31 -0800
commit1dea21f41d28640d8fa6e9d43a55b9a034383f34 (patch)
treeacd9539c676397ed0d124a1912fe07b2d4af3985
parent6143400aaac2239feb979deebe9777f6edccce1a (diff)
parenta1afe9cc0ad410af599ff580d41ce9c9e158136f (diff)
downloadrust-1dea21f41d28640d8fa6e9d43a55b9a034383f34.tar.gz
rust-1dea21f41d28640d8fa6e9d43a55b9a034383f34.zip
auto merge of #10599 : thestinger/rust/unsafe, r=cmr
This is just meant to be for containing usage of `unsafe`, much like `heap_memory`.
-rw-r--r--src/librustc/middle/lint.rs22
-rw-r--r--src/test/compile-fail/lint-unsafe-block.rs20
2 files changed, 40 insertions, 2 deletions
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 <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
+}