Skip to content

Commit

Permalink
Added lint for #[deriving] structs and enums with unsafe pointers. #1…
Browse files Browse the repository at this point in the history
  • Loading branch information
pongad committed Mar 27, 2014
1 parent c83994e commit 5744556
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/librustc/middle/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ pub enum Lint {
DeprecatedOwnedVector,

Warnings,

RawPointerDeriving,
}

pub fn level_to_str(lv: level) -> &'static str {
Expand Down Expand Up @@ -406,6 +408,13 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
desc: "use of a `~[T]` vector",
default: allow,
}),

("raw_pointer_deriving",
LintSpec {
lint: RawPointerDeriving,
desc: "uses of #[deriving] with raw pointers are rarely correct",
default: warn,
}),
];

/*
Expand Down Expand Up @@ -959,6 +968,37 @@ fn check_heap_item(cx: &Context, it: &ast::Item) {
}
}

struct RawPtrDerivingVisitor<'a> {
cx: &'a Context<'a>
}

impl<'a> Visitor<()> for RawPtrDerivingVisitor<'a> {
fn visit_ty(&mut self, ty: &ast::Ty, _: ()) {
static MSG: &'static str = "use of `#[deriving]` with a raw pointer";
match ty.node {
ast::TyPtr(..) => self.cx.span_lint(RawPointerDeriving, ty.span, MSG),
_ => {}
}
visit::walk_ty(self, ty, ());
}
// explicit override to a no-op to reduce code bloat
fn visit_expr(&mut self, _: &ast::Expr, _: ()) {}
fn visit_block(&mut self, _: &ast::Block, _: ()) {}
}

fn check_raw_ptr_deriving(cx: &Context, item: &ast::Item) {
if !attr::contains_name(item.attrs.as_slice(), "deriving") {
return
}
match item.node {
ast::ItemStruct(..) | ast::ItemEnum(..) => {
let mut visitor = RawPtrDerivingVisitor { cx: cx };
visit::walk_item(&mut visitor, item, ());
}
_ => {}
}
}

static crate_attrs: &'static [&'static str] = &[
"crate_type", "feature", "no_start", "no_main", "no_std", "crate_id",
"desc", "comment", "license", "copyright", // not used in rustc now
Expand Down Expand Up @@ -1585,6 +1625,7 @@ impl<'a> Visitor<()> for Context<'a> {
check_heap_item(cx, it);
check_missing_doc_item(cx, it);
check_attrs_usage(cx, it.attrs.as_slice());
check_raw_ptr_deriving(cx, it);

cx.visit_ids(|v| v.visit_item(it, ()));

Expand Down
1 change: 1 addition & 0 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ pub struct t_box_ {
// alive, and using ty::get is unsafe when the ctxt is no longer alive.
enum t_opaque {}

#[allow(raw_pointer_deriving)]
#[deriving(Clone, Eq, TotalEq, Hash)]
pub struct t { priv inner: *t_opaque }

Expand Down
35 changes: 35 additions & 0 deletions src/test/compile-fail/lint-raw-ptr-deriving.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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.

#[feature(struct_variant)];
#[allow(dead_code)];
#[deny(raw_pointer_deriving)];

#[deriving(Clone)]
struct Foo {
x: *int //~ ERROR use of `#[deriving]` with a raw pointer
}

#[deriving(Clone)]
struct Bar(*mut int); //~ ERROR use of `#[deriving]` with a raw pointer

#[deriving(Clone)]
enum Baz {
A(*int), //~ ERROR use of `#[deriving]` with a raw pointer
B { x: *mut int } //~ ERROR use of `#[deriving]` with a raw pointer
}

#[deriving(Clone)]
struct Buzz {
x: (*int, //~ ERROR use of `#[deriving]` with a raw pointer
*uint) //~ ERROR use of `#[deriving]` with a raw pointer
}

fn main() {}

5 comments on commit 5744556

@bors
Copy link
Contributor

@bors bors commented on 5744556 Mar 28, 2014

Choose a reason for hiding this comment

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

@bors
Copy link
Contributor

@bors bors commented on 5744556 Mar 28, 2014

Choose a reason for hiding this comment

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

merging pongad/rust/lintraw = 5744556 into auto

@bors
Copy link
Contributor

@bors bors commented on 5744556 Mar 28, 2014

Choose a reason for hiding this comment

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

pongad/rust/lintraw = 5744556 merged ok, testing candidate = 5a68892

@bors
Copy link
Contributor

@bors bors commented on 5744556 Mar 28, 2014

Choose a reason for hiding this comment

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

@bors
Copy link
Contributor

@bors bors commented on 5744556 Mar 28, 2014

Choose a reason for hiding this comment

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

fast-forwarding master to auto = 5a68892

Please sign in to comment.