@@ -3539,15 +3539,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
35393539 . must_apply_modulo_regions ( )
35403540 {
35413541 let sm = self . tcx . sess . source_map ( ) ;
3542- if let Ok ( rhs_snippet) = sm. span_to_snippet ( rhs_expr. span )
3543- && let Ok ( lhs_snippet) = sm. span_to_snippet ( lhs_expr. span )
3542+ // // If the span of rhs_expr or lhs_expr is in an external macro,
3543+ // // we should not suggest to swap the equality. See issue #139050
3544+ // if rhs_expr.span.in_external_macro(sm) || lhs_expr.span.in_external_macro(sm) {
3545+ // return;
3546+ // }
3547+ if let Some ( rhs_span) = rhs_expr. span . find_ancestor_not_from_extern_macro ( sm)
3548+ && let Some ( lhs_span) = lhs_expr. span . find_ancestor_not_from_extern_macro ( sm)
3549+ && let Ok ( rhs_snippet) = sm. span_to_snippet ( rhs_span)
3550+ && let Ok ( lhs_snippet) = sm. span_to_snippet ( lhs_span)
35443551 {
35453552 err. note ( format ! ( "`{rhs_ty}` implements `PartialEq<{lhs_ty}>`" ) ) ;
3546- err. multipart_suggestion (
3547- "consider swapping the equality" ,
3548- vec ! [ ( lhs_expr. span, rhs_snippet) , ( rhs_expr. span, lhs_snippet) ] ,
3549- Applicability :: MaybeIncorrect ,
3550- ) ;
3553+ if rhs_span != lhs_span {
3554+ err. multipart_suggestion (
3555+ "consider swapping the equality" ,
3556+ vec ! [ ( lhs_span, rhs_snippet) , ( rhs_span, lhs_snippet) ] ,
3557+ Applicability :: MaybeIncorrect ,
3558+ ) ;
3559+ } else {
3560+ // rhs_span and lhs_span are the same because it from extern macro.
3561+ // we should suggest to swap two arguments of the equality
3562+ err. span_help ( rhs_span, "consider swapping two arguments of the equality" ) ;
3563+ }
35513564 }
35523565 }
35533566 }
0 commit comments