about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>2022-10-13 18:19:18 +0530
committerGitHub <noreply@github.com>2022-10-13 18:19:18 +0530
commitad45dd172213fff157cde566db9fd9279798824e (patch)
treeb507e1ca4cb3f03d8c751dc3eaaec5e705e18a1d
parentfa0ca783f89a83046e6ce0383385ba5b28296435 (diff)
parent57d0278d5cd397a89bb33f1857b3ed431c1f341c (diff)
downloadrust-ad45dd172213fff157cde566db9fd9279798824e.tar.gz
rust-ad45dd172213fff157cde566db9fd9279798824e.zip
Rollup merge of #102765 - TaKO8Ki:follow-up-to-102708, r=compiler-errors
Suggest `==` to the first expr which has `ExprKind::Assign` kind

follow-up to #102708

[playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=4241dc33ed8af02e1ef530d6b14903fd)
-rw-r--r--compiler/rustc_hir_analysis/src/check/expr.rs12
-rw-r--r--src/test/ui/type/type-check/assignment-in-if.rs6
-rw-r--r--src/test/ui/type/type-check/assignment-in-if.stderr19
3 files changed, 36 insertions, 1 deletions
diff --git a/compiler/rustc_hir_analysis/src/check/expr.rs b/compiler/rustc_hir_analysis/src/check/expr.rs
index 375c13d922b..34c25784597 100644
--- a/compiler/rustc_hir_analysis/src/check/expr.rs
+++ b/compiler/rustc_hir_analysis/src/check/expr.rs
@@ -1051,8 +1051,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                 rhs_expr,
             ) = lhs.kind
             {
+                // if x == 1 && y == 2 { .. }
+                //                 +
                 let actual_lhs_ty = self.check_expr(&rhs_expr);
                 (Applicability::MaybeIncorrect, self.can_coerce(rhs_ty, actual_lhs_ty))
+            } else if let ExprKind::Binary(
+                Spanned { node: hir::BinOpKind::And | hir::BinOpKind::Or, .. },
+                lhs_expr,
+                _,
+            ) = rhs.kind
+            {
+                // if x == 1 && y == 2 { .. }
+                //       +
+                let actual_rhs_ty = self.check_expr(&lhs_expr);
+                (Applicability::MaybeIncorrect, self.can_coerce(actual_rhs_ty, lhs_ty))
             } else {
                 (Applicability::MaybeIncorrect, false)
             };
diff --git a/src/test/ui/type/type-check/assignment-in-if.rs b/src/test/ui/type/type-check/assignment-in-if.rs
index 3a7845096fd..ada250df246 100644
--- a/src/test/ui/type/type-check/assignment-in-if.rs
+++ b/src/test/ui/type/type-check/assignment-in-if.rs
@@ -53,4 +53,10 @@ fn main() {
         //~| ERROR mismatched types
         println!("{}", x);
     }
+
+    if x = 1 && x == 1 {
+        //~^ ERROR mismatched types
+        //~| ERROR mismatched types
+        println!("{}", x);
+    }
 }
diff --git a/src/test/ui/type/type-check/assignment-in-if.stderr b/src/test/ui/type/type-check/assignment-in-if.stderr
index 166f2293777..8ab08e25e30 100644
--- a/src/test/ui/type/type-check/assignment-in-if.stderr
+++ b/src/test/ui/type/type-check/assignment-in-if.stderr
@@ -104,6 +104,23 @@ help: you might have meant to compare for equality
 LL |     if x == x && x == x && x == x {
    |                               +
 
-error: aborting due to 11 previous errors
+error[E0308]: mismatched types
+  --> $DIR/assignment-in-if.rs:57:12
+   |
+LL |     if x = 1 && x == 1 {
+   |            ^ expected `bool`, found integer
+
+error[E0308]: mismatched types
+  --> $DIR/assignment-in-if.rs:57:8
+   |
+LL |     if x = 1 && x == 1 {
+   |        ^^^^^^^^^^^^^^^ expected `bool`, found `()`
+   |
+help: you might have meant to compare for equality
+   |
+LL |     if x == 1 && x == 1 {
+   |           +
+
+error: aborting due to 13 previous errors
 
 For more information about this error, try `rustc --explain E0308`.