@@ -340,20 +340,36 @@ fn check_comparison<'a, 'tcx>(
340340 }
341341 if l_ty. is_bool ( ) && r_ty. is_bool ( ) {
342342 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 ( ) ) ;
343349
344350 if op. node == BinOpKind :: Eq {
345351 let expression_info = one_side_is_unary_not ( left_side, right_side) ;
346352 if expression_info. one_side_is_unary_not {
347353 span_lint_and_sugg (
348354 cx,
349355 BOOL_COMPARISON ,
350- e . span ,
356+ binop_span ,
351357 "this comparison might be written more concisely" ,
352358 "try simplifying it as shown" ,
353359 format ! (
354360 "{} != {}" ,
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+ )
357373 ) ,
358374 applicability,
359375 ) ;
@@ -362,24 +378,24 @@ fn check_comparison<'a, 'tcx>(
362378
363379 match ( fetch_bool_expr ( left_side) , fetch_bool_expr ( right_side) ) {
364380 ( 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) ;
366382 } ) ,
367383 ( 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) ;
369385 } ) ,
370386 ( 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) ;
372388 } ) ,
373389 ( 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) ;
375391 } ) ,
376392 ( None , None ) => no_literal. map_or ( ( ) , |( h, m) | {
377393 let left_side = Sugg :: hir_with_applicability ( cx, left_side, ".." , & mut applicability) ;
378394 let right_side = Sugg :: hir_with_applicability ( cx, right_side, ".." , & mut applicability) ;
379395 span_lint_and_sugg (
380396 cx,
381397 BOOL_COMPARISON ,
382- e . span ,
398+ binop_span ,
383399 m,
384400 "try simplifying it as shown" ,
385401 h ( left_side, right_side) . to_string ( ) ,
@@ -394,17 +410,17 @@ fn check_comparison<'a, 'tcx>(
394410
395411fn suggest_bool_comparison < ' a , ' tcx > (
396412 cx : & LateContext < ' tcx > ,
397- e : & ' tcx Expr < ' _ > ,
413+ span : Span ,
398414 expr : & Expr < ' _ > ,
399415 mut app : Applicability ,
400416 message : & str ,
401417 conv_hint : impl FnOnce ( Sugg < ' a > ) -> Sugg < ' a > ,
402418) {
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) ;
404420 span_lint_and_sugg (
405421 cx,
406422 BOOL_COMPARISON ,
407- e . span ,
423+ span,
408424 message,
409425 "try simplifying it as shown" ,
410426 conv_hint ( hint) . to_string ( ) ,
0 commit comments