@@ -4843,6 +4843,53 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
48434843 ) ;
48444844 true
48454845 }
4846+ pub ( crate ) fn suggest_swapping_lhs_and_rhs < T > (
4847+ & self ,
4848+ err : & mut Diag < ' _ > ,
4849+ predicate : T ,
4850+ param_env : ty:: ParamEnv < ' tcx > ,
4851+ cause_code : & ObligationCauseCode < ' tcx > ,
4852+ ) where
4853+ T : Upcast < TyCtxt < ' tcx > , ty:: Predicate < ' tcx > > ,
4854+ {
4855+ let tcx = self . tcx ;
4856+ let predicate = predicate. upcast ( tcx) ;
4857+ match * cause_code {
4858+ ObligationCauseCode :: BinOp {
4859+ lhs_hir_id,
4860+ rhs_hir_id : Some ( rhs_hir_id) ,
4861+ rhs_span : Some ( rhs_span) ,
4862+ ..
4863+ } if let Some ( typeck_results) = & self . typeck_results
4864+ && let hir:: Node :: Expr ( lhs) = tcx. hir_node ( lhs_hir_id)
4865+ && let hir:: Node :: Expr ( rhs) = tcx. hir_node ( rhs_hir_id)
4866+ && let Some ( lhs_ty) = typeck_results. expr_ty_opt ( lhs)
4867+ && let Some ( rhs_ty) = typeck_results. expr_ty_opt ( rhs) =>
4868+ {
4869+ if let Some ( pred) = predicate. as_trait_clause ( )
4870+ && tcx. is_lang_item ( pred. def_id ( ) , LangItem :: PartialEq )
4871+ && self
4872+ . infcx
4873+ . type_implements_trait ( pred. def_id ( ) , [ rhs_ty, lhs_ty] , param_env)
4874+ . must_apply_modulo_regions ( )
4875+ {
4876+ let lhs_span = tcx. hir ( ) . span ( lhs_hir_id) ;
4877+ let sm = tcx. sess . source_map ( ) ;
4878+ if let Ok ( rhs_snippet) = sm. span_to_snippet ( rhs_span)
4879+ && let Ok ( lhs_snippet) = sm. span_to_snippet ( lhs_span)
4880+ {
4881+ err. note ( format ! ( "`{rhs_ty}` implements `PartialEq<{lhs_ty}>`" ) ) ;
4882+ err. multipart_suggestion (
4883+ "consider swapping the equality" ,
4884+ vec ! [ ( lhs_span, rhs_snippet) , ( rhs_span, lhs_snippet) ] ,
4885+ Applicability :: MaybeIncorrect ,
4886+ ) ;
4887+ }
4888+ }
4889+ }
4890+ _ => { }
4891+ }
4892+ }
48464893}
48474894
48484895/// Add a hint to add a missing borrow or remove an unnecessary one.
0 commit comments