Skip to content

Commit 1dea21f

Browse files
committed
auto merge of #10599 : thestinger/rust/unsafe, r=cmr
This is just meant to be for containing usage of `unsafe`, much like `heap_memory`.
2 parents 6143400 + a1afe9c commit 1dea21f

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/librustc/middle/lint.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pub enum lint {
7575
type_limits,
7676
type_overflow,
7777
unused_unsafe,
78+
unsafe_block,
7879

7980
managed_heap_memory,
8081
owned_heap_memory,
@@ -236,6 +237,13 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
236237
default: warn
237238
}),
238239

240+
("unsafe_block",
241+
LintSpec {
242+
lint: unsafe_block,
243+
desc: "usage of an `unsafe` block",
244+
default: allow
245+
}),
246+
239247
("unused_variable",
240248
LintSpec {
241249
lint: unused_variable,
@@ -870,8 +878,7 @@ fn check_pat_non_uppercase_statics(cx: &Context, p: &ast::Pat) {
870878

871879
fn check_unused_unsafe(cx: &Context, e: &ast::Expr) {
872880
match e.node {
873-
// Don't warn about generated blocks, that'll just pollute the
874-
// output.
881+
// Don't warn about generated blocks, that'll just pollute the output.
875882
ast::ExprBlock(ref blk) => {
876883
if blk.rules == ast::UnsafeBlock(ast::UserProvided) &&
877884
!cx.tcx.used_unsafe.contains(&blk.id) {
@@ -883,6 +890,16 @@ fn check_unused_unsafe(cx: &Context, e: &ast::Expr) {
883890
}
884891
}
885892

893+
fn check_unsafe_block(cx: &Context, e: &ast::Expr) {
894+
match e.node {
895+
// Don't warn about generated blocks, that'll just pollute the output.
896+
ast::ExprBlock(ref blk) if blk.rules == ast::UnsafeBlock(ast::UserProvided) => {
897+
cx.span_lint(unsafe_block, blk.span, "usage of an `unsafe` block");
898+
}
899+
_ => ()
900+
}
901+
}
902+
886903
fn check_unused_mut_pat(cx: &Context, p: @ast::Pat) {
887904
match p.node {
888905
ast::PatIdent(ast::BindByValue(ast::MutMutable),
@@ -1126,6 +1143,7 @@ impl<'self> Visitor<()> for Context<'self> {
11261143
check_while_true_expr(self, e);
11271144
check_stability(self, e);
11281145
check_unused_unsafe(self, e);
1146+
check_unsafe_block(self, e);
11291147
check_unnecessary_allocation(self, e);
11301148
check_heap_expr(self, e);
11311149

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2013 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+
#[deny(unsafe_block)];
13+
14+
unsafe fn allowed() {}
15+
16+
#[allow(unsafe_block)] fn also_allowed() { unsafe {} }
17+
18+
fn main() {
19+
unsafe {} //~ ERROR: usage of an `unsafe` block
20+
}

0 commit comments

Comments
 (0)