@@ -3539,15 +3539,25 @@ 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 find the user-written code span. See issue #139050
3544+ if let Some ( rhs_span) = rhs_expr. span . find_ancestor_not_from_extern_macro ( sm)
3545+ && let Some ( lhs_span) = lhs_expr. span . find_ancestor_not_from_extern_macro ( sm)
3546+ && let Ok ( rhs_snippet) = sm. span_to_snippet ( rhs_span)
3547+ && let Ok ( lhs_snippet) = sm. span_to_snippet ( lhs_span)
35443548 {
35453549 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- ) ;
3550+ if rhs_span != lhs_span {
3551+ err. multipart_suggestion (
3552+ "consider swapping the equality" ,
3553+ vec ! [ ( lhs_span, rhs_snippet) , ( rhs_span, lhs_snippet) ] ,
3554+ Applicability :: MaybeIncorrect ,
3555+ ) ;
3556+ } else {
3557+ // rhs_span and lhs_span are the same because it from extern macro.
3558+ // we should suggest to swap two arguments of the equality
3559+ err. span_help ( rhs_span, "consider swapping two arguments of the equality" ) ;
3560+ }
35513561 }
35523562 }
35533563 }
0 commit comments