1
- use crate :: utils:: { snippet_with_applicability, span_lint_and_sugg} ;
2
1
use crate :: utils:: ast_utils:: { eq_id, IdentIter } ;
2
+ use crate :: utils:: { snippet_with_applicability, span_lint_and_sugg} ;
3
3
use core:: ops:: { Add , AddAssign } ;
4
4
use if_chain:: if_chain;
5
5
use rustc_ast:: ast:: * ;
@@ -8,6 +8,7 @@ use rustc_data_structures::fx::FxHashSet;
8
8
use rustc_errors:: Applicability ;
9
9
use rustc_lint:: { EarlyContext , EarlyLintPass } ;
10
10
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
11
+ use rustc_span:: source_map:: Spanned ;
11
12
use rustc_span:: symbol:: Ident ;
12
13
use rustc_span:: Span ;
13
14
@@ -196,17 +197,39 @@ fn ident_swap_sugg(
196
197
Some ( sugg)
197
198
}
198
199
199
- struct BinaryOp {
200
+ struct BinaryOp < ' exprs > {
200
201
op : BinOpKind ,
201
202
span : Span ,
202
- left : P < Expr > ,
203
- right : P < Expr > ,
203
+ left : & ' exprs Expr ,
204
+ right : & ' exprs Expr ,
204
205
}
205
206
206
207
// TODO make this collect expr pairs in (for example) if expressions, and rename it.
207
208
// Also, include enough info to make a coherent suggestion in those cases.
208
- fn chained_binops ( kind : & ExprKind ) -> Option < Vec < BinaryOp > > {
209
- todo ! ( )
209
+ fn chained_binops ( kind : & ' expr ExprKind ) -> Option < Vec < BinaryOp < ' expr > > > {
210
+ match kind {
211
+ ExprKind :: Binary ( _, left_outer, right_outer) => match ( & left_outer. kind , & right_outer. kind ) {
212
+ (
213
+ ExprKind :: Binary ( Spanned { node : left_op, .. } , left_left, left_right) ,
214
+ ExprKind :: Binary ( Spanned { node : right_op, .. } , right_left, right_right) ,
215
+ ) if left_op == right_op => Some ( vec ! [
216
+ BinaryOp {
217
+ op: * left_op,
218
+ left: left_left,
219
+ right: left_right,
220
+ span: left_outer. span,
221
+ } ,
222
+ BinaryOp {
223
+ op: * right_op,
224
+ left: right_left,
225
+ right: right_right,
226
+ span: right_outer. span,
227
+ } ,
228
+ ] ) ,
229
+ _ => None ,
230
+ } ,
231
+ _ => None ,
232
+ }
210
233
}
211
234
212
235
#[ derive( Clone , Copy , PartialEq , Eq , Default , Debug ) ]
@@ -219,7 +242,7 @@ impl Add for IdentLocation {
219
242
220
243
fn add ( self , other : Self ) -> Self :: Output {
221
244
Self {
222
- index : self . index + other. index
245
+ index : self . index + other. index ,
223
246
}
224
247
}
225
248
}
@@ -244,15 +267,13 @@ impl Add for IdentDifference {
244
267
245
268
fn add ( self , other : Self ) -> Self :: Output {
246
269
match ( self , other) {
247
- ( Self :: NoDifference , output) | ( output, Self :: NoDifference ) => output,
270
+ ( Self :: NoDifference , output) | ( output, Self :: NoDifference ) => output,
248
271
( Self :: Multiple , _)
249
272
| ( _, Self :: Multiple )
250
273
| ( Self :: Single ( _, _) , Self :: Double ( _, _) )
251
274
| ( Self :: Double ( _, _) , Self :: Single ( _, _) )
252
- | ( Self :: Double ( _, _) , Self :: Double ( _, _) ) =>
253
- Self :: Multiple ,
254
- ( Self :: NonIdentDifference , _) |( _, Self :: NonIdentDifference ) =>
255
- Self :: NonIdentDifference ,
275
+ | ( Self :: Double ( _, _) , Self :: Double ( _, _) ) => Self :: Multiple ,
276
+ ( Self :: NonIdentDifference , _) | ( _, Self :: NonIdentDifference ) => Self :: NonIdentDifference ,
256
277
( Self :: Single ( il1, _) , Self :: Single ( il2, _) ) => Self :: Double ( il1, il2) ,
257
278
}
258
279
}
@@ -305,19 +326,29 @@ fn ident_difference_expr_with_base_location(
305
326
// without needing to change the rest of the code.
306
327
let mut difference = IdentDifference :: NoDifference ;
307
328
308
- for ( left_attr, right_attr) in left. attrs . iter ( ) . zip ( right. attrs . iter ( ) ) {
309
- let ( new_difference, new_base) =
310
- ident_difference_via_ident_iter_with_base_location ( left_attr, right_attr, base) ;
329
+ if false
330
+ /* fill out and use this branch later */
331
+ {
332
+ for ( left_attr, right_attr) in left. attrs . iter ( ) . zip ( right. attrs . iter ( ) ) {
333
+ let ( new_difference, new_base) =
334
+ ident_difference_via_ident_iter_with_base_location ( left_attr, right_attr, base) ;
335
+ base = new_base;
336
+ difference += new_difference;
337
+ if difference. is_complete ( ) {
338
+ return ( difference, base) ;
339
+ }
340
+ }
341
+
342
+ match ( & left. kind , & right. kind ) {
343
+ _ => todo ! ( ) ,
344
+ }
345
+ } else {
346
+ let ( new_difference, new_base) = ident_difference_via_ident_iter_with_base_location ( left, right, base) ;
311
347
base = new_base;
312
348
difference += new_difference;
313
- if difference. is_complete ( ) {
314
- return ( difference, base) ;
315
- }
316
349
}
317
350
318
- match ( & left. kind , & right. kind ) {
319
- _ => todo ! ( ) ,
320
- }
351
+ ( difference, base)
321
352
}
322
353
323
354
fn ident_difference_via_ident_iter_with_base_location < Iterable : Into < IdentIter > > (
@@ -365,7 +396,7 @@ fn suggestion_with_swapped_ident(
365
396
new_ident : Ident ,
366
397
applicability : & mut Applicability ,
367
398
) -> Option < String > {
368
- get_ident ( expr, location) . and_then ( |current_ident| {
399
+ get_ident ( expr, location) . map ( |current_ident| {
369
400
format ! (
370
401
"{}{}{}" ,
371
402
snippet_with_applicability( cx, expr. span. with_hi( current_ident. span. lo( ) ) , ".." , applicability) ,
@@ -374,4 +405,3 @@ fn suggestion_with_swapped_ident(
374
405
)
375
406
} )
376
407
}
377
-
0 commit comments