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