@@ -769,34 +769,40 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
769769 let mut err = self . demand_suptype_diag ( expr. span , expected_ty, actual_ty) . unwrap ( ) ;
770770 let lhs_ty = self . check_expr ( & lhs) ;
771771 let rhs_ty = self . check_expr ( & rhs) ;
772- if self . can_coerce ( lhs_ty, rhs_ty) {
773- if !lhs. is_syntactic_place_expr ( ) {
774- // Do not suggest `if let x = y` as `==` is way more likely to be the intention.
775- if let hir:: Node :: Expr ( hir:: Expr {
776- kind : ExprKind :: Match ( _, _, hir:: MatchSource :: IfDesugar { .. } ) ,
777- ..
778- } ) = self . tcx . hir ( ) . get (
779- self . tcx . hir ( ) . get_parent_node ( self . tcx . hir ( ) . get_parent_node ( expr. hir_id ) ) ,
780- ) {
781- // Likely `if let` intended.
782- err. span_suggestion_verbose (
783- expr. span . shrink_to_lo ( ) ,
784- "you might have meant to use pattern matching" ,
785- "let " . to_string ( ) ,
786- Applicability :: MaybeIncorrect ,
787- ) ;
788- }
772+ let ( applicability, eq) = if self . can_coerce ( rhs_ty, lhs_ty) {
773+ ( Applicability :: MachineApplicable , true )
774+ } else {
775+ ( Applicability :: MaybeIncorrect , false )
776+ } ;
777+ if !lhs. is_syntactic_place_expr ( ) {
778+ // Do not suggest `if let x = y` as `==` is way more likely to be the intention.
779+ if let hir:: Node :: Expr ( hir:: Expr {
780+ kind :
781+ ExprKind :: Match (
782+ _,
783+ _,
784+ hir:: MatchSource :: IfDesugar { .. } | hir:: MatchSource :: WhileDesugar ,
785+ ) ,
786+ ..
787+ } ) = self . tcx . hir ( ) . get (
788+ self . tcx . hir ( ) . get_parent_node ( self . tcx . hir ( ) . get_parent_node ( expr. hir_id ) ) ,
789+ ) {
790+ // Likely `if let` intended.
791+ err. span_suggestion_verbose (
792+ expr. span . shrink_to_lo ( ) ,
793+ "you might have meant to use pattern matching" ,
794+ "let " . to_string ( ) ,
795+ applicability,
796+ ) ;
789797 }
798+ }
799+ if eq {
790800 err. span_suggestion_verbose (
791801 * span,
792802 "you might have meant to compare for equality" ,
793803 "==" . to_string ( ) ,
794- Applicability :: MaybeIncorrect ,
804+ applicability ,
795805 ) ;
796- } else {
797- // Do this to cause extra errors about the assignment.
798- let lhs_ty = self . check_expr_with_needs ( & lhs, Needs :: MutPlace ) ;
799- let _ = self . check_expr_coercable_to_type ( & rhs, lhs_ty, Some ( lhs) ) ;
800806 }
801807
802808 if self . sess ( ) . if_let_suggestions . borrow ( ) . get ( & expr. span ) . is_some ( ) {
0 commit comments