@@ -48,60 +48,53 @@ impl LateLintPass for EtaPass {
48
48
}
49
49
50
50
fn check_closure ( cx : & LateContext , expr : & Expr ) {
51
- if let ExprClosure ( _, ref decl, ref blk, _) = expr. node {
52
- if !blk. stmts . is_empty ( ) {
53
- // || {foo(); bar()}; can't be reduced here
54
- return ;
55
- }
56
-
57
- if let Some ( ref ex) = blk. expr {
58
- if let ExprCall ( ref caller, ref args) = ex. node {
59
- if args. len ( ) != decl. inputs . len ( ) {
60
- // Not the same number of arguments, there
61
- // is no way the closure is the same as the function
62
- return ;
63
- }
64
- if is_adjusted ( cx, ex) || args. iter ( ) . any ( |arg| is_adjusted ( cx, arg) ) {
65
- // Are the expression or the arguments type-adjusted? Then we need the closure
66
- return ;
51
+ if let ExprClosure ( _, ref decl, ref ex, _) = expr. node {
52
+ if let ExprCall ( ref caller, ref args) = ex. node {
53
+ if args. len ( ) != decl. inputs . len ( ) {
54
+ // Not the same number of arguments, there
55
+ // is no way the closure is the same as the function
56
+ return ;
57
+ }
58
+ if is_adjusted ( cx, ex) || args. iter ( ) . any ( |arg| is_adjusted ( cx, arg) ) {
59
+ // Are the expression or the arguments type-adjusted? Then we need the closure
60
+ return ;
61
+ }
62
+ let fn_ty = cx. tcx . tables ( ) . expr_ty ( caller) ;
63
+ match fn_ty. sty {
64
+ // Is it an unsafe function? They don't implement the closure traits
65
+ ty:: TyFnDef ( _, _, fn_ty) |
66
+ ty:: TyFnPtr ( fn_ty) => {
67
+ if fn_ty. unsafety == Unsafety :: Unsafe ||
68
+ fn_ty. sig . skip_binder ( ) . output . sty == ty:: TyNever {
69
+ return ;
70
+ }
67
71
}
68
- let fn_ty = cx. tcx . expr_ty ( caller) ;
69
- match fn_ty. sty {
70
- // Is it an unsafe function? They don't implement the closure traits
71
- ty:: TyFnDef ( _, _, fn_ty) |
72
- ty:: TyFnPtr ( fn_ty) => {
73
- if fn_ty. unsafety == Unsafety :: Unsafe ||
74
- fn_ty. sig . skip_binder ( ) . output . sty == ty:: TyNever {
72
+ _ => ( ) ,
73
+ }
74
+ for ( a1, a2) in decl. inputs . iter ( ) . zip ( args) {
75
+ if let PatKind :: Binding ( _, ident, _) = a1. pat . node {
76
+ // XXXManishearth Should I be checking the binding mode here?
77
+ if let ExprPath ( None , ref p) = a2. node {
78
+ if p. segments . len ( ) != 1 {
79
+ // If it's a proper path, it can't be a local variable
75
80
return ;
76
81
}
77
- }
78
- _ => ( ) ,
79
- }
80
- for ( a1, a2) in decl. inputs . iter ( ) . zip ( args) {
81
- if let PatKind :: Binding ( _, ident, _) = a1. pat . node {
82
- // XXXManishearth Should I be checking the binding mode here?
83
- if let ExprPath ( None , ref p) = a2. node {
84
- if p. segments . len ( ) != 1 {
85
- // If it's a proper path, it can't be a local variable
86
- return ;
87
- }
88
- if p. segments [ 0 ] . name != ident. node {
89
- // The two idents should be the same
90
- return ;
91
- }
92
- } else {
82
+ if p. segments [ 0 ] . name != ident. node {
83
+ // The two idents should be the same
93
84
return ;
94
85
}
95
86
} else {
96
87
return ;
97
88
}
89
+ } else {
90
+ return ;
98
91
}
99
- span_lint_and_then ( cx, REDUNDANT_CLOSURE , expr. span , "redundant closure found" , |db| {
100
- if let Some ( snippet) = snippet_opt ( cx, caller. span ) {
101
- db. span_suggestion ( expr. span , "remove closure as shown:" , snippet) ;
102
- }
103
- } ) ;
104
92
}
93
+ span_lint_and_then ( cx, REDUNDANT_CLOSURE , expr. span , "redundant closure found" , |db| {
94
+ if let Some ( snippet) = snippet_opt ( cx, caller. span ) {
95
+ db. span_suggestion ( expr. span , "remove closure as shown:" , snippet) ;
96
+ }
97
+ } ) ;
105
98
}
106
99
}
107
100
}
0 commit comments