Skip to content

Commit ad45dd1

Browse files
authored
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)
2 parents fa0ca78 + 57d0278 commit ad45dd1

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

compiler/rustc_hir_analysis/src/check/expr.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1051,8 +1051,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10511051
rhs_expr,
10521052
) = lhs.kind
10531053
{
1054+
// if x == 1 && y == 2 { .. }
1055+
// +
10541056
let actual_lhs_ty = self.check_expr(&rhs_expr);
10551057
(Applicability::MaybeIncorrect, self.can_coerce(rhs_ty, actual_lhs_ty))
1058+
} else if let ExprKind::Binary(
1059+
Spanned { node: hir::BinOpKind::And | hir::BinOpKind::Or, .. },
1060+
lhs_expr,
1061+
_,
1062+
) = rhs.kind
1063+
{
1064+
// if x == 1 && y == 2 { .. }
1065+
// +
1066+
let actual_rhs_ty = self.check_expr(&lhs_expr);
1067+
(Applicability::MaybeIncorrect, self.can_coerce(actual_rhs_ty, lhs_ty))
10561068
} else {
10571069
(Applicability::MaybeIncorrect, false)
10581070
};

src/test/ui/type/type-check/assignment-in-if.rs

+6
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,10 @@ fn main() {
5353
//~| ERROR mismatched types
5454
println!("{}", x);
5555
}
56+
57+
if x = 1 && x == 1 {
58+
//~^ ERROR mismatched types
59+
//~| ERROR mismatched types
60+
println!("{}", x);
61+
}
5662
}

src/test/ui/type/type-check/assignment-in-if.stderr

+18-1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,23 @@ help: you might have meant to compare for equality
104104
LL | if x == x && x == x && x == x {
105105
| +
106106

107-
error: aborting due to 11 previous errors
107+
error[E0308]: mismatched types
108+
--> $DIR/assignment-in-if.rs:57:12
109+
|
110+
LL | if x = 1 && x == 1 {
111+
| ^ expected `bool`, found integer
112+
113+
error[E0308]: mismatched types
114+
--> $DIR/assignment-in-if.rs:57:8
115+
|
116+
LL | if x = 1 && x == 1 {
117+
| ^^^^^^^^^^^^^^^ expected `bool`, found `()`
118+
|
119+
help: you might have meant to compare for equality
120+
|
121+
LL | if x == 1 && x == 1 {
122+
| +
123+
124+
error: aborting due to 13 previous errors
108125

109126
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)