13
13
//! This lint is **warn** by default
14
14
15
15
use rustc:: lint:: * ;
16
- use rustc:: hir:: * ;
17
16
use std:: borrow:: Cow ;
18
17
use syntax:: codemap:: Spanned ;
18
+ use syntax:: ast;
19
19
20
20
use utils:: { in_macro, snippet, snippet_block, span_lint_and_then} ;
21
21
@@ -45,23 +45,22 @@ impl LintPass for CollapsibleIf {
45
45
}
46
46
}
47
47
48
- impl LateLintPass for CollapsibleIf {
49
- fn check_expr ( & mut self , cx : & LateContext , expr : & Expr ) {
48
+ impl EarlyLintPass for CollapsibleIf {
49
+ fn check_expr ( & mut self , cx : & EarlyContext , expr : & ast :: Expr ) {
50
50
if !in_macro ( cx, expr. span ) {
51
51
check_if ( cx, expr)
52
52
}
53
53
}
54
54
}
55
55
56
- fn check_if ( cx : & LateContext , e : & Expr ) {
57
- if let ExprIf ( ref check, ref then, ref else_) = e. node {
56
+ fn check_if ( cx : & EarlyContext , e : & ast :: Expr ) {
57
+ if let ast :: ExprKind :: If ( ref check, ref then, ref else_) = e. node {
58
58
if let Some ( ref else_) = * else_ {
59
59
if_let_chain ! { [
60
- let ExprBlock ( ref block) = else_. node,
60
+ let ast :: ExprKind :: Block ( ref block) = else_. node,
61
61
block. stmts. is_empty( ) ,
62
- block. rules == BlockCheckMode :: DefaultBlock ,
63
62
let Some ( ref else_) = block. expr,
64
- let ExprIf ( _, _, _) = else_. node
63
+ let ast :: ExprKind :: If ( _, _, _) = else_. node
65
64
] , {
66
65
span_lint_and_then( cx,
67
66
COLLAPSIBLE_IF ,
@@ -70,7 +69,7 @@ fn check_if(cx: &LateContext, e: &Expr) {
70
69
db. span_suggestion( block. span, "try" , snippet_block( cx, else_. span, ".." ) . into_owned( ) ) ;
71
70
} ) ;
72
71
} }
73
- } else if let Some ( & Expr { node : ExprIf ( ref check_inner, ref content, None ) , span : sp, .. } ) =
72
+ } else if let Some ( & ast :: Expr { node : ast :: ExprKind :: If ( ref check_inner, ref content, None ) , span : sp, .. } ) =
74
73
single_stmt_of_block ( then) {
75
74
if e. span . expn_id != sp. expn_id {
76
75
return ;
@@ -87,24 +86,24 @@ fn check_if(cx: &LateContext, e: &Expr) {
87
86
}
88
87
}
89
88
90
- fn requires_brackets ( e : & Expr ) -> bool {
89
+ fn requires_brackets ( e : & ast :: Expr ) -> bool {
91
90
match e. node {
92
- ExprBinary ( Spanned { node : n, .. } , _, _) if n == BiEq => false ,
91
+ ast :: ExprKind :: Binary ( Spanned { node : n, .. } , _, _) if n == ast :: BinOpKind :: Eq => false ,
93
92
_ => true ,
94
93
}
95
94
}
96
95
97
- fn check_to_string ( cx : & LateContext , e : & Expr ) -> Cow < ' static , str > {
96
+ fn check_to_string ( cx : & EarlyContext , e : & ast :: Expr ) -> Cow < ' static , str > {
98
97
if requires_brackets ( e) {
99
98
format ! ( "({})" , snippet( cx, e. span, ".." ) ) . into ( )
100
99
} else {
101
100
snippet ( cx, e. span , ".." )
102
101
}
103
102
}
104
103
105
- fn single_stmt_of_block ( block : & Block ) -> Option < & Expr > {
104
+ fn single_stmt_of_block ( block : & ast :: Block ) -> Option < & ast :: Expr > {
106
105
if block. stmts . len ( ) == 1 && block. expr . is_none ( ) {
107
- if let StmtExpr ( ref expr, _) = block. stmts [ 0 ] . node {
106
+ if let ast :: StmtKind :: Expr ( ref expr, _) = block. stmts [ 0 ] . node {
108
107
single_stmt_of_expr ( expr)
109
108
} else {
110
109
None
@@ -120,8 +119,8 @@ fn single_stmt_of_block(block: &Block) -> Option<&Expr> {
120
119
}
121
120
}
122
121
123
- fn single_stmt_of_expr ( expr : & Expr ) -> Option < & Expr > {
124
- if let ExprBlock ( ref block) = expr. node {
122
+ fn single_stmt_of_expr ( expr : & ast :: Expr ) -> Option < & ast :: Expr > {
123
+ if let ast :: ExprKind :: Block ( ref block) = expr. node {
125
124
single_stmt_of_block ( block)
126
125
} else {
127
126
Some ( expr)
0 commit comments