@@ -45,6 +45,7 @@ use syntax::attr;
45
45
use syntax:: codemap:: Span ;
46
46
use syntax:: parse:: token;
47
47
use syntax:: { ast, ast_util, visit} ;
48
+ use syntax:: visit:: Visitor ;
48
49
49
50
declare_lint ! ( WHILE_TRUE , Warn ,
50
51
"suggest using `loop { }` instead of `while true { }`" )
@@ -339,6 +340,51 @@ impl LintPass for TypeLimits {
339
340
declare_lint ! ( CTYPES , Warn ,
340
341
"proper use of libc types in foreign modules" )
341
342
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
+
342
388
pub struct CTypes ;
343
389
344
390
impl LintPass for CTypes {
@@ -348,38 +394,8 @@ impl LintPass for CTypes {
348
394
349
395
fn check_item ( & mut self , cx : & Context , it : & ast:: Item ) {
350
396
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, ( ) ) ;
383
399
}
384
400
385
401
fn check_foreign_fn ( cx : & Context , decl : & ast:: FnDecl ) {
@@ -390,15 +406,15 @@ impl LintPass for CTypes {
390
406
}
391
407
392
408
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
+ }
398
415
}
399
416
}
400
- }
401
- _ => { /* nothing to do */ }
417
+ _ => ( ) ,
402
418
}
403
419
}
404
420
}
@@ -493,7 +509,7 @@ struct RawPtrDerivingVisitor<'a> {
493
509
cx : & ' a Context < ' a >
494
510
}
495
511
496
- impl < ' a > visit :: Visitor < ( ) > for RawPtrDerivingVisitor < ' a > {
512
+ impl < ' a > Visitor < ( ) > for RawPtrDerivingVisitor < ' a > {
497
513
fn visit_ty ( & mut self , ty : & ast:: Ty , _: ( ) ) {
498
514
static MSG : & ' static str = "use of `#[deriving]` with a raw pointer" ;
499
515
match ty. node {
0 commit comments