Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add lint for unsafe blocks #10599

Merged
merged 1 commit into from
Nov 22, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that

macro_rules! foo ( () => { unsafe { *(0 as *int) } } )

fn main() { foo!() }

doesn't get picked up? Maybe we need UserProvided, UserMacroGenerated, CompileMacroGenerated, or something.

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
}