@@ -2,8 +2,7 @@ use crate::utils::{differing_macro_contexts, in_macro_or_desugar, snippet_opt, s
2
2
use if_chain:: if_chain;
3
3
use rustc:: lint:: { in_external_macro, EarlyContext , EarlyLintPass , LintArray , LintPass } ;
4
4
use rustc:: { declare_lint_pass, declare_tool_lint} ;
5
- use syntax:: ast;
6
- use syntax:: ptr:: P ;
5
+ use syntax:: ast:: * ;
7
6
8
7
declare_clippy_lint ! {
9
8
/// **What it does:** Checks for use of the non-existent `=*`, `=!` and `=-`
@@ -86,33 +85,33 @@ declare_lint_pass!(Formatting => [
86
85
] ) ;
87
86
88
87
impl EarlyLintPass for Formatting {
89
- fn check_block ( & mut self , cx : & EarlyContext < ' _ > , block : & ast :: Block ) {
88
+ fn check_block ( & mut self , cx : & EarlyContext < ' _ > , block : & Block ) {
90
89
for w in block. stmts . windows ( 2 ) {
91
90
match ( & w[ 0 ] . node , & w[ 1 ] . node ) {
92
- ( & ast :: StmtKind :: Expr ( ref first) , & ast :: StmtKind :: Expr ( ref second) )
93
- | ( & ast :: StmtKind :: Expr ( ref first) , & ast :: StmtKind :: Semi ( ref second) ) => {
91
+ ( & StmtKind :: Expr ( ref first) , & StmtKind :: Expr ( ref second) )
92
+ | ( & StmtKind :: Expr ( ref first) , & StmtKind :: Semi ( ref second) ) => {
94
93
check_missing_else ( cx, first, second) ;
95
94
} ,
96
95
_ => ( ) ,
97
96
}
98
97
}
99
98
}
100
99
101
- fn check_expr ( & mut self , cx : & EarlyContext < ' _ > , expr : & ast :: Expr ) {
100
+ fn check_expr ( & mut self , cx : & EarlyContext < ' _ > , expr : & Expr ) {
102
101
check_assign ( cx, expr) ;
103
102
check_else ( cx, expr) ;
104
103
check_array ( cx, expr) ;
105
104
}
106
105
}
107
106
108
107
/// Implementation of the `SUSPICIOUS_ASSIGNMENT_FORMATTING` lint.
109
- fn check_assign ( cx : & EarlyContext < ' _ > , expr : & ast :: Expr ) {
110
- if let ast :: ExprKind :: Assign ( ref lhs, ref rhs) = expr. node {
108
+ fn check_assign ( cx : & EarlyContext < ' _ > , expr : & Expr ) {
109
+ if let ExprKind :: Assign ( ref lhs, ref rhs) = expr. node {
111
110
if !differing_macro_contexts ( lhs. span , rhs. span ) && !in_macro_or_desugar ( lhs. span ) {
112
111
let eq_span = lhs. span . between ( rhs. span ) ;
113
- if let ast :: ExprKind :: Unary ( op, ref sub_rhs) = rhs. node {
112
+ if let ExprKind :: Unary ( op, ref sub_rhs) = rhs. node {
114
113
if let Some ( eq_snippet) = snippet_opt ( cx, eq_span) {
115
- let op = ast :: UnOp :: to_string ( op) ;
114
+ let op = UnOp :: to_string ( op) ;
116
115
let eqop_span = lhs. span . between ( sub_rhs. span ) ;
117
116
if eq_snippet. ends_with ( '=' ) {
118
117
span_note_and_lint (
@@ -135,10 +134,10 @@ fn check_assign(cx: &EarlyContext<'_>, expr: &ast::Expr) {
135
134
}
136
135
137
136
/// Implementation of the `SUSPICIOUS_ELSE_FORMATTING` lint for weird `else`.
138
- fn check_else ( cx : & EarlyContext < ' _ > , expr : & ast :: Expr ) {
137
+ fn check_else ( cx : & EarlyContext < ' _ > , expr : & Expr ) {
139
138
if_chain ! {
140
- if let Some ( ( then, & Some ( ref else_) ) ) = unsugar_if ( expr) ;
141
- if is_block( else_) || unsugar_if ( else_) . is_some ( ) ;
139
+ if let ExprKind :: If ( _ , then, Some ( else_) ) = & expr. node ;
140
+ if is_block( else_) || is_if ( else_) ;
142
141
if !differing_macro_contexts( then. span, else_. span) ;
143
142
if !in_macro_or_desugar( then. span) && !in_external_macro( cx. sess, expr. span) ;
144
143
@@ -154,7 +153,7 @@ fn check_else(cx: &EarlyContext<'_>, expr: &ast::Expr) {
154
153
if let Some ( else_snippet) = snippet_opt( cx, else_span) ;
155
154
if let Some ( else_pos) = else_snippet. find( "else" ) ;
156
155
if else_snippet[ else_pos..] . contains( '\n' ) ;
157
- let else_desc = if unsugar_if ( else_) . is_some ( ) { "if" } else { "{..}" } ;
156
+ let else_desc = if is_if ( else_) { "if" } else { "{..}" } ;
158
157
159
158
then {
160
159
span_note_and_lint(
@@ -173,16 +172,16 @@ fn check_else(cx: &EarlyContext<'_>, expr: &ast::Expr) {
173
172
}
174
173
}
175
174
176
- fn has_unary_equivalent ( bin_op : ast :: BinOpKind ) -> bool {
175
+ fn has_unary_equivalent ( bin_op : BinOpKind ) -> bool {
177
176
// &, *, -
178
- bin_op == ast :: BinOpKind :: And || bin_op == ast :: BinOpKind :: Mul || bin_op == ast :: BinOpKind :: Sub
177
+ bin_op == BinOpKind :: And || bin_op == BinOpKind :: Mul || bin_op == BinOpKind :: Sub
179
178
}
180
179
181
180
/// Implementation of the `POSSIBLE_MISSING_COMMA` lint for array
182
- fn check_array ( cx : & EarlyContext < ' _ > , expr : & ast :: Expr ) {
183
- if let ast :: ExprKind :: Array ( ref array) = expr. node {
181
+ fn check_array ( cx : & EarlyContext < ' _ > , expr : & Expr ) {
182
+ if let ExprKind :: Array ( ref array) = expr. node {
184
183
for element in array {
185
- if let ast :: ExprKind :: Binary ( ref op, ref lhs, _) = element. node {
184
+ if let ExprKind :: Binary ( ref op, ref lhs, _) = element. node {
186
185
if has_unary_equivalent ( op. node ) && !differing_macro_contexts ( lhs. span , op. span ) {
187
186
let space_span = lhs. span . between ( op. span ) ;
188
187
if let Some ( space_snippet) = snippet_opt ( cx, space_span) {
@@ -204,18 +203,18 @@ fn check_array(cx: &EarlyContext<'_>, expr: &ast::Expr) {
204
203
}
205
204
}
206
205
207
- fn check_missing_else ( cx : & EarlyContext < ' _ > , first : & ast :: Expr , second : & ast :: Expr ) {
206
+ fn check_missing_else ( cx : & EarlyContext < ' _ > , first : & Expr , second : & Expr ) {
208
207
if !differing_macro_contexts ( first. span , second. span )
209
208
&& !in_macro_or_desugar ( first. span )
210
- && unsugar_if ( first) . is_some ( )
211
- && ( is_block ( second) || unsugar_if ( second) . is_some ( ) )
209
+ && is_if ( first)
210
+ && ( is_block ( second) || is_if ( second) )
212
211
{
213
212
// where the else would be
214
213
let else_span = first. span . between ( second. span ) ;
215
214
216
215
if let Some ( else_snippet) = snippet_opt ( cx, else_span) {
217
216
if !else_snippet. contains ( '\n' ) {
218
- let ( looks_like, next_thing) = if unsugar_if ( second) . is_some ( ) {
217
+ let ( looks_like, next_thing) = if is_if ( second) {
219
218
( "an `else if`" , "the second `if`" )
220
219
} else {
221
220
( "an `else {..}`" , "the next block" )
@@ -237,18 +236,19 @@ fn check_missing_else(cx: &EarlyContext<'_>, first: &ast::Expr, second: &ast::Ex
237
236
}
238
237
}
239
238
240
- fn is_block ( expr : & ast :: Expr ) -> bool {
241
- if let ast :: ExprKind :: Block ( ..) = expr. node {
239
+ fn is_block ( expr : & Expr ) -> bool {
240
+ if let ExprKind :: Block ( ..) = expr. node {
242
241
true
243
242
} else {
244
243
false
245
244
}
246
245
}
247
246
248
- /// Match `if` or `if let` expressions and return the `then` and `else` block.
249
- fn unsugar_if ( expr : & ast:: Expr ) -> Option < ( & P < ast:: Block > , & Option < P < ast:: Expr > > ) > {
250
- match expr. node {
251
- ast:: ExprKind :: If ( _, ref then, ref else_) => Some ( ( then, else_) ) ,
252
- _ => None ,
247
+ /// Check if the expression is an `if` or `if let`
248
+ fn is_if ( expr : & Expr ) -> bool {
249
+ if let ExprKind :: If ( ..) = expr. node {
250
+ true
251
+ } else {
252
+ false
253
253
}
254
254
}
0 commit comments