@@ -340,20 +340,36 @@ fn check_comparison<'a, 'tcx>(
340
340
}
341
341
if l_ty. is_bool ( ) && r_ty. is_bool ( ) {
342
342
let mut applicability = Applicability :: MachineApplicable ;
343
+ // Eliminate parentheses in `e` by using the lo pos of lhs and hi pos of rhs,
344
+ // calling `source_callsite` make sure macros are handled correctly, see issue #9907
345
+ let binop_span = left_side
346
+ . span
347
+ . source_callsite ( )
348
+ . with_hi ( right_side. span . source_callsite ( ) . hi ( ) ) ;
343
349
344
350
if op. node == BinOpKind :: Eq {
345
351
let expression_info = one_side_is_unary_not ( left_side, right_side) ;
346
352
if expression_info. one_side_is_unary_not {
347
353
span_lint_and_sugg (
348
354
cx,
349
355
BOOL_COMPARISON ,
350
- e . span ,
356
+ binop_span ,
351
357
"this comparison might be written more concisely" ,
352
358
"try simplifying it as shown" ,
353
359
format ! (
354
360
"{} != {}" ,
355
- snippet_with_applicability( cx, expression_info. left_span, ".." , & mut applicability) ,
356
- snippet_with_applicability( cx, expression_info. right_span, ".." , & mut applicability)
361
+ snippet_with_applicability(
362
+ cx,
363
+ expression_info. left_span. source_callsite( ) ,
364
+ ".." ,
365
+ & mut applicability
366
+ ) ,
367
+ snippet_with_applicability(
368
+ cx,
369
+ expression_info. right_span. source_callsite( ) ,
370
+ ".." ,
371
+ & mut applicability
372
+ )
357
373
) ,
358
374
applicability,
359
375
) ;
@@ -362,24 +378,24 @@ fn check_comparison<'a, 'tcx>(
362
378
363
379
match ( fetch_bool_expr ( left_side) , fetch_bool_expr ( right_side) ) {
364
380
( Some ( true ) , None ) => left_true. map_or ( ( ) , |( h, m) | {
365
- suggest_bool_comparison ( cx, e , right_side, applicability, m, h) ;
381
+ suggest_bool_comparison ( cx, binop_span , right_side, applicability, m, h) ;
366
382
} ) ,
367
383
( None , Some ( true ) ) => right_true. map_or ( ( ) , |( h, m) | {
368
- suggest_bool_comparison ( cx, e , left_side, applicability, m, h) ;
384
+ suggest_bool_comparison ( cx, binop_span , left_side, applicability, m, h) ;
369
385
} ) ,
370
386
( Some ( false ) , None ) => left_false. map_or ( ( ) , |( h, m) | {
371
- suggest_bool_comparison ( cx, e , right_side, applicability, m, h) ;
387
+ suggest_bool_comparison ( cx, binop_span , right_side, applicability, m, h) ;
372
388
} ) ,
373
389
( None , Some ( false ) ) => right_false. map_or ( ( ) , |( h, m) | {
374
- suggest_bool_comparison ( cx, e , left_side, applicability, m, h) ;
390
+ suggest_bool_comparison ( cx, binop_span , left_side, applicability, m, h) ;
375
391
} ) ,
376
392
( None , None ) => no_literal. map_or ( ( ) , |( h, m) | {
377
393
let left_side = Sugg :: hir_with_applicability ( cx, left_side, ".." , & mut applicability) ;
378
394
let right_side = Sugg :: hir_with_applicability ( cx, right_side, ".." , & mut applicability) ;
379
395
span_lint_and_sugg (
380
396
cx,
381
397
BOOL_COMPARISON ,
382
- e . span ,
398
+ binop_span ,
383
399
m,
384
400
"try simplifying it as shown" ,
385
401
h ( left_side, right_side) . to_string ( ) ,
@@ -394,17 +410,17 @@ fn check_comparison<'a, 'tcx>(
394
410
395
411
fn suggest_bool_comparison < ' a , ' tcx > (
396
412
cx : & LateContext < ' tcx > ,
397
- e : & ' tcx Expr < ' _ > ,
413
+ span : Span ,
398
414
expr : & Expr < ' _ > ,
399
415
mut app : Applicability ,
400
416
message : & str ,
401
417
conv_hint : impl FnOnce ( Sugg < ' a > ) -> Sugg < ' a > ,
402
418
) {
403
- let hint = Sugg :: hir_with_context ( cx, expr, e . span . ctxt ( ) , ".." , & mut app) ;
419
+ let hint = Sugg :: hir_with_context ( cx, expr, span. ctxt ( ) , ".." , & mut app) ;
404
420
span_lint_and_sugg (
405
421
cx,
406
422
BOOL_COMPARISON ,
407
- e . span ,
423
+ span,
408
424
message,
409
425
"try simplifying it as shown" ,
410
426
conv_hint ( hint) . to_string ( ) ,
0 commit comments