@@ -9,7 +9,7 @@ use syntax::ast::Lit_::LitBool;
9
9
use syntax:: codemap:: Span ;
10
10
11
11
use utils:: { COW_PATH , OPTION_PATH , RESULT_PATH } ;
12
- use utils:: { match_type, snippet, span_lint, span_note_and_lint, span_help_and_lint , in_external_macro, expr_block} ;
12
+ use utils:: { match_type, snippet, span_lint, span_note_and_lint, span_lint_and_then , in_external_macro, expr_block} ;
13
13
14
14
/// **What it does:** This lint checks for matches with a single arm where an `if let` will usually suffice. It is `Warn` by default.
15
15
///
@@ -124,15 +124,17 @@ fn check_single_match(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
124
124
125
125
fn check_single_match_single_pattern ( cx : & LateContext , ex : & Expr , arms : & [ Arm ] , expr : & Expr ) {
126
126
if arms[ 1 ] . pats [ 0 ] . node == PatWild {
127
- span_help_and_lint ( cx,
127
+ span_lint_and_then ( cx,
128
128
SINGLE_MATCH ,
129
129
expr. span ,
130
- "you seem to be trying to use match for destructuring a single pattern. Consider using \
131
- `if let`",
132
- & format ! ( "try\n if let {} = {} {}" ,
133
- snippet( cx, arms[ 0 ] . pats[ 0 ] . span, ".." ) ,
134
- snippet( cx, ex. span, ".." ) ,
135
- expr_block( cx, & arms[ 0 ] . body, None , ".." ) ) ) ;
130
+ "you seem to be trying to use match for destructuring a single pattern. \
131
+ Consider using `if let`", |db| {
132
+ db. span_suggestion ( expr. span , "try this" ,
133
+ format ! ( "if let {} = {} {}" ,
134
+ snippet( cx, arms[ 0 ] . pats[ 0 ] . span, ".." ) ,
135
+ snippet( cx, ex. span, ".." ) ,
136
+ expr_block( cx, & arms[ 0 ] . body, None , ".." ) ) ) ;
137
+ } ) ;
136
138
}
137
139
}
138
140
@@ -156,23 +158,25 @@ fn check_single_match_opt_like(cx: &LateContext, ex: &Expr, arms: &[Arm], expr:
156
158
157
159
for & ( ty_path, pat_path) in candidates {
158
160
if & path == pat_path && match_type ( cx, ty, ty_path) {
159
- span_help_and_lint ( cx,
161
+ span_lint_and_then ( cx,
160
162
SINGLE_MATCH ,
161
163
expr. span ,
162
- "you seem to be trying to use match for destructuring a single pattern. Consider using \
163
- `if let`",
164
- & format ! ( "try\n if let {} = {} {}" ,
165
- snippet( cx, arms[ 0 ] . pats[ 0 ] . span, ".." ) ,
166
- snippet( cx, ex. span, ".." ) ,
167
- expr_block( cx, & arms[ 0 ] . body, None , ".." ) ) ) ;
164
+ "you seem to be trying to use match for destructuring a single pattern. \
165
+ Consider using `if let`", |db| {
166
+ db. span_suggestion ( expr. span , "try this" ,
167
+ format ! ( "if let {} = {} {}" ,
168
+ snippet( cx, arms[ 0 ] . pats[ 0 ] . span, ".." ) ,
169
+ snippet( cx, ex. span, ".." ) ,
170
+ expr_block( cx, & arms[ 0 ] . body, None , ".." ) ) ) ;
171
+ } ) ;
168
172
}
169
173
}
170
174
}
171
175
172
176
fn check_match_bool ( cx : & LateContext , ex : & Expr , arms : & [ Arm ] , expr : & Expr ) {
173
177
// type of expression == bool
174
178
if cx. tcx . expr_ty ( ex) . sty == ty:: TyBool {
175
- if arms. len ( ) == 2 && arms[ 0 ] . pats . len ( ) == 1 {
179
+ let sugg = if arms. len ( ) == 2 && arms[ 0 ] . pats . len ( ) == 1 {
176
180
// no guards
177
181
let exprs = if let PatLit ( ref arm_bool) = arms[ 0 ] . pats [ 0 ] . node {
178
182
if let ExprLit ( ref lit) = arm_bool. node {
@@ -187,56 +191,42 @@ fn check_match_bool(cx: &LateContext, ex: &Expr, arms: &[Arm], expr: &Expr) {
187
191
} else {
188
192
None
189
193
} ;
194
+
190
195
if let Some ( ( ref true_expr, ref false_expr) ) = exprs {
191
196
if !is_unit_expr ( true_expr) {
192
197
if !is_unit_expr ( false_expr) {
193
- span_help_and_lint ( cx,
194
- MATCH_BOOL ,
195
- expr. span ,
196
- "you seem to be trying to match on a boolean expression. Consider using \
197
- an if..else block:",
198
- & format ! ( "try\n if {} {} else {}" ,
199
- snippet( cx, ex. span, "b" ) ,
200
- expr_block( cx, true_expr, None , ".." ) ,
201
- expr_block( cx, false_expr, None , ".." ) ) ) ;
198
+ Some ( format ! ( "if {} {} else {}" ,
199
+ snippet( cx, ex. span, "b" ) ,
200
+ expr_block( cx, true_expr, None , ".." ) ,
201
+ expr_block( cx, false_expr, None , ".." ) ) )
202
202
} else {
203
- span_help_and_lint ( cx,
204
- MATCH_BOOL ,
205
- expr. span ,
206
- "you seem to be trying to match on a boolean expression. Consider using \
207
- an if..else block:",
208
- & format ! ( "try\n if {} {}" ,
209
- snippet( cx, ex. span, "b" ) ,
210
- expr_block( cx, true_expr, None , ".." ) ) ) ;
203
+ Some ( format ! ( "if {} {}" ,
204
+ snippet( cx, ex. span, "b" ) ,
205
+ expr_block( cx, true_expr, None , ".." ) ) )
211
206
}
212
207
} else if !is_unit_expr ( false_expr) {
213
- span_help_and_lint ( cx,
214
- MATCH_BOOL ,
215
- expr. span ,
216
- "you seem to be trying to match on a boolean expression. Consider using an \
217
- if..else block:",
218
- & format ! ( "try\n if !{} {}" ,
219
- snippet( cx, ex. span, "b" ) ,
220
- expr_block( cx, false_expr, None , ".." ) ) ) ;
208
+ Some ( format ! ( "try\n if !{} {}" ,
209
+ snippet( cx, ex. span, "b" ) ,
210
+ expr_block( cx, false_expr, None , ".." ) ) )
221
211
} else {
222
- span_lint ( cx,
223
- MATCH_BOOL ,
224
- expr. span ,
225
- "you seem to be trying to match on a boolean expression. Consider using an if..else \
226
- block") ;
212
+ None
227
213
}
228
214
} else {
229
- span_lint ( cx,
230
- MATCH_BOOL ,
231
- expr. span ,
232
- "you seem to be trying to match on a boolean expression. Consider using an if..else block" ) ;
215
+ None
233
216
}
234
217
} else {
235
- span_lint ( cx,
236
- MATCH_BOOL ,
237
- expr. span ,
238
- "you seem to be trying to match on a boolean expression. Consider using an if..else block" ) ;
239
- }
218
+ None
219
+ } ;
220
+
221
+ span_lint_and_then ( cx,
222
+ MATCH_BOOL ,
223
+ expr. span ,
224
+ "you seem to be trying to match on a boolean expression. Consider using \
225
+ an if..else block:", move |db| {
226
+ if let Some ( ref sugg) = sugg {
227
+ db. span_suggestion ( expr. span , "try this" , sugg. clone ( ) ) ;
228
+ }
229
+ } ) ;
240
230
}
241
231
}
242
232
0 commit comments