@@ -6,6 +6,7 @@ use rustc::front::map::NodeItem;
6
6
use rustc:: lint:: * ;
7
7
use rustc:: middle:: ty;
8
8
use rustc_front:: hir:: * ;
9
+ use syntax:: ast:: NodeId ;
9
10
use utils:: { STRING_PATH , VEC_PATH } ;
10
11
use utils:: { span_lint, match_type} ;
11
12
@@ -35,7 +36,7 @@ impl LintPass for PtrArg {
35
36
impl LateLintPass for PtrArg {
36
37
fn check_item ( & mut self , cx : & LateContext , item : & Item ) {
37
38
if let ItemFn ( ref decl, _, _, _, _, _) = item. node {
38
- check_fn ( cx, decl) ;
39
+ check_fn ( cx, decl, item . id ) ;
39
40
}
40
41
}
41
42
@@ -46,34 +47,34 @@ impl LateLintPass for PtrArg {
46
47
return ; // ignore trait impls
47
48
}
48
49
}
49
- check_fn ( cx, & sig. decl ) ;
50
+ check_fn ( cx, & sig. decl , item . id ) ;
50
51
}
51
52
}
52
53
53
54
fn check_trait_item ( & mut self , cx : & LateContext , item : & TraitItem ) {
54
55
if let MethodTraitItem ( ref sig, _) = item. node {
55
- check_fn ( cx, & sig. decl ) ;
56
+ check_fn ( cx, & sig. decl , item . id ) ;
56
57
}
57
58
}
58
59
}
59
60
60
- fn check_fn ( cx : & LateContext , decl : & FnDecl ) {
61
- for arg in & decl . inputs {
62
- if let Some ( ty ) = cx . tcx . ast_ty_to_ty_cache . borrow ( ) . get ( & arg . ty . id ) {
63
- if let ty :: TyRef ( _ , ty:: TypeAndMut { ty , mutbl : MutImmutable } ) = ty . sty {
64
- if match_type ( cx , ty, & VEC_PATH ) {
65
- span_lint ( cx,
66
- PTR_ARG ,
67
- arg . ty . span ,
68
- "writing `&Vec<_>` instead of `&[_]` involves one more reference and cannot be used \
69
- with non-Vec-based slices. Consider changing the type to `&[...]`" ) ;
70
- } else if match_type ( cx , ty , & STRING_PATH ) {
71
- span_lint ( cx,
72
- PTR_ARG ,
73
- arg . ty . span ,
74
- "writing `&String` instead of `&str` involves a new object where a slice will do. \
75
- Consider changing the type to `&str`" ) ;
76
- }
61
+ fn check_fn ( cx : & LateContext , decl : & FnDecl , fn_id : NodeId ) {
62
+ let fn_ty = cx . tcx . node_id_to_type ( fn_id ) . fn_sig ( ) . skip_binder ( ) ;
63
+
64
+ for ( arg , ty) in decl . inputs . iter ( ) . zip ( & fn_ty . inputs ) {
65
+ if let ty :: TyRef ( _ , ty :: TypeAndMut { ty , mutbl : MutImmutable } ) = ty. sty {
66
+ if match_type ( cx, ty , & VEC_PATH ) {
67
+ span_lint ( cx ,
68
+ PTR_ARG ,
69
+ arg . ty . span ,
70
+ "writing `&Vec<_>` instead of `&[_]` involves one more reference and cannot be used \
71
+ with non-Vec-based slices. Consider changing the type to `&[...]`" ) ;
72
+ } else if match_type ( cx, ty , & STRING_PATH ) {
73
+ span_lint ( cx ,
74
+ PTR_ARG ,
75
+ arg . ty . span ,
76
+ "writing `&String` instead of `&str` involves a new object where a slice will do. \
77
+ Consider changing the type to `&str`" ) ;
77
78
}
78
79
}
79
80
}
0 commit comments