Skip to content

Commit bf7d74c

Browse files
author
Keegan McAllister
committed
---
yaml --- r: 130111 b: refs/heads/snap-stage3 c: f422de1 h: refs/heads/master i: 130109: abb0420 130107: c619408 130103: 2d8bc2f 130095: 84e7573 130079: 2db4b5c 130047: 7a2c647 v: v3
1 parent ba98d59 commit bf7d74c

File tree

3 files changed

+75
-41
lines changed

3 files changed

+75
-41
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 0bdac78da87605f6f7f6e7924872617226b19c85
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 7f676b86994edcd6adf27018a5d18e957c9390ab
4+
refs/heads/snap-stage3: f422de1e85e87db51bfb61655da3faa331fbd91a
55
refs/heads/try: 28d5878c1f0465c11c8e7a3085008b0c592d48d0
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/librustc/lint/builtin.rs

Lines changed: 56 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use syntax::attr;
4545
use syntax::codemap::Span;
4646
use syntax::parse::token;
4747
use syntax::{ast, ast_util, visit};
48+
use syntax::visit::Visitor;
4849

4950
declare_lint!(WHILE_TRUE, Warn,
5051
"suggest using `loop { }` instead of `while true { }`")
@@ -339,6 +340,51 @@ impl LintPass for TypeLimits {
339340
declare_lint!(CTYPES, Warn,
340341
"proper use of libc types in foreign modules")
341342

343+
struct CTypesVisitor<'a> {
344+
cx: &'a Context<'a>
345+
}
346+
347+
impl<'a> CTypesVisitor<'a> {
348+
fn check_def(&mut self, sp: Span, ty_id: ast::NodeId, path_id: ast::NodeId) {
349+
match self.cx.tcx.def_map.borrow().get_copy(&path_id) {
350+
def::DefPrimTy(ast::TyInt(ast::TyI)) => {
351+
self.cx.span_lint(CTYPES, sp,
352+
"found rust type `int` in foreign module, while \
353+
libc::c_int or libc::c_long should be used");
354+
}
355+
def::DefPrimTy(ast::TyUint(ast::TyU)) => {
356+
self.cx.span_lint(CTYPES, sp,
357+
"found rust type `uint` in foreign module, while \
358+
libc::c_uint or libc::c_ulong should be used");
359+
}
360+
def::DefTy(..) => {
361+
let tty = match self.cx.tcx.ast_ty_to_ty_cache.borrow().find(&ty_id) {
362+
Some(&ty::atttce_resolved(t)) => t,
363+
_ => fail!("ast_ty_to_ty_cache was incomplete after typeck!")
364+
};
365+
366+
if !ty::is_ffi_safe(self.cx.tcx, tty) {
367+
self.cx.span_lint(CTYPES, sp,
368+
"found type without foreign-function-safe
369+
representation annotation in foreign module, consider \
370+
adding a #[repr(...)] attribute to the type");
371+
}
372+
}
373+
_ => ()
374+
}
375+
}
376+
}
377+
378+
impl<'a> Visitor<()> for CTypesVisitor<'a> {
379+
fn visit_ty(&mut self, ty: &ast::Ty, _: ()) {
380+
match ty.node {
381+
ast::TyPath(_, _, id) => self.check_def(ty.span, ty.id, id),
382+
_ => (),
383+
}
384+
visit::walk_ty(self, ty, ());
385+
}
386+
}
387+
342388
pub struct CTypes;
343389

344390
impl LintPass for CTypes {
@@ -348,38 +394,8 @@ impl LintPass for CTypes {
348394

349395
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
350396
fn check_ty(cx: &Context, ty: &ast::Ty) {
351-
match ty.node {
352-
ast::TyPath(_, _, id) => {
353-
match cx.tcx.def_map.borrow().get_copy(&id) {
354-
def::DefPrimTy(ast::TyInt(ast::TyI)) => {
355-
cx.span_lint(CTYPES, ty.span,
356-
"found rust type `int` in foreign module, while \
357-
libc::c_int or libc::c_long should be used");
358-
}
359-
def::DefPrimTy(ast::TyUint(ast::TyU)) => {
360-
cx.span_lint(CTYPES, ty.span,
361-
"found rust type `uint` in foreign module, while \
362-
libc::c_uint or libc::c_ulong should be used");
363-
}
364-
def::DefTy(..) => {
365-
let tty = match cx.tcx.ast_ty_to_ty_cache.borrow().find(&ty.id) {
366-
Some(&ty::atttce_resolved(t)) => t,
367-
_ => fail!("ast_ty_to_ty_cache was incomplete after typeck!")
368-
};
369-
370-
if !ty::is_ffi_safe(cx.tcx, tty) {
371-
cx.span_lint(CTYPES, ty.span,
372-
"found type without foreign-function-safe
373-
representation annotation in foreign module, consider \
374-
adding a #[repr(...)] attribute to the type");
375-
}
376-
}
377-
_ => ()
378-
}
379-
}
380-
ast::TyPtr(ref mt) => { check_ty(cx, &*mt.ty) }
381-
_ => {}
382-
}
397+
let mut vis = CTypesVisitor { cx: cx };
398+
vis.visit_ty(ty, ());
383399
}
384400

385401
fn check_foreign_fn(cx: &Context, decl: &ast::FnDecl) {
@@ -390,15 +406,15 @@ impl LintPass for CTypes {
390406
}
391407

392408
match it.node {
393-
ast::ItemForeignMod(ref nmod) if nmod.abi != abi::RustIntrinsic => {
394-
for ni in nmod.items.iter() {
395-
match ni.node {
396-
ast::ForeignItemFn(decl, _) => check_foreign_fn(cx, &*decl),
397-
ast::ForeignItemStatic(t, _) => check_ty(cx, &*t)
409+
ast::ItemForeignMod(ref nmod) if nmod.abi != abi::RustIntrinsic => {
410+
for ni in nmod.items.iter() {
411+
match ni.node {
412+
ast::ForeignItemFn(decl, _) => check_foreign_fn(cx, &*decl),
413+
ast::ForeignItemStatic(t, _) => check_ty(cx, &*t)
414+
}
398415
}
399416
}
400-
}
401-
_ => {/* nothing to do */ }
417+
_ => (),
402418
}
403419
}
404420
}
@@ -493,7 +509,7 @@ struct RawPtrDerivingVisitor<'a> {
493509
cx: &'a Context<'a>
494510
}
495511

496-
impl<'a> visit::Visitor<()> for RawPtrDerivingVisitor<'a> {
512+
impl<'a> Visitor<()> for RawPtrDerivingVisitor<'a> {
497513
fn visit_ty(&mut self, ty: &ast::Ty, _: ()) {
498514
static MSG: &'static str = "use of `#[deriving]` with a raw pointer";
499515
match ty.node {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2014 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+
#![deny(warnings)]
12+
13+
extern {
14+
pub fn foo(x: (int)); //~ ERROR found rust type `int` in foreign module
15+
}
16+
17+
fn main() {
18+
}

0 commit comments

Comments
 (0)