Skip to content

Commit b7c42c5

Browse files
committed
suggest == to the rest of assign expr
1 parent 760279f commit b7c42c5

File tree

6 files changed

+86
-20
lines changed

6 files changed

+86
-20
lines changed

compiler/rustc_hir_analysis/src/check/expr.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10441044
let rhs_ty = self.check_expr(&rhs);
10451045
let (applicability, eq) = if self.can_coerce(rhs_ty, lhs_ty) {
10461046
(Applicability::MachineApplicable, true)
1047+
} else if let ExprKind::Binary(
1048+
Spanned { node: hir::BinOpKind::And | hir::BinOpKind::Or, .. },
1049+
_,
1050+
rhs_expr,
1051+
) = lhs.kind
1052+
{
1053+
let actual_lhs_ty = self.check_expr(&rhs_expr);
1054+
(Applicability::MaybeIncorrect, self.can_coerce(rhs_ty, actual_lhs_ty))
10471055
} else {
10481056
(Applicability::MaybeIncorrect, false)
10491057
};

src/test/ui/expr/if/bad-if-let-suggestion.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ error[E0308]: mismatched types
6262
|
6363
LL | if let x = 1 && i = 2 {}
6464
| ^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
65+
|
66+
help: you might have meant to compare for equality
67+
|
68+
LL | if let x = 1 && i == 2 {}
69+
| +
6570

6671
error: aborting due to 8 previous errors
6772

src/test/ui/rfc-2497-if-let-chains/disallowed-positions.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1510,7 +1510,7 @@ LL | if x = let 0 = 0 {}
15101510
help: you might have meant to compare for equality
15111511
|
15121512
LL | if x == let 0 = 0 {}
1513-
| ~~
1513+
| +
15141514

15151515
error[E0308]: mismatched types
15161516
--> $DIR/disallowed-positions.rs:157:8
@@ -1704,7 +1704,7 @@ LL | while x = let 0 = 0 {}
17041704
help: you might have meant to compare for equality
17051705
|
17061706
LL | while x == let 0 = 0 {}
1707-
| ~~
1707+
| +
17081708

17091709
error[E0308]: mismatched types
17101710
--> $DIR/disallowed-positions.rs:249:11

src/test/ui/type/type-check/assignment-expected-bool.stderr

+11-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | let _: bool = 0 = 0;
77
help: you might have meant to compare for equality
88
|
99
LL | let _: bool = 0 == 0;
10-
| ~~
10+
| +
1111

1212
error[E0308]: mismatched types
1313
--> $DIR/assignment-expected-bool.rs:9:14
@@ -18,7 +18,7 @@ LL | 0 => 0 = 0,
1818
help: you might have meant to compare for equality
1919
|
2020
LL | 0 => 0 == 0,
21-
| ~~
21+
| +
2222

2323
error[E0308]: mismatched types
2424
--> $DIR/assignment-expected-bool.rs:10:14
@@ -29,7 +29,7 @@ LL | _ => 0 = 0,
2929
help: you might have meant to compare for equality
3030
|
3131
LL | _ => 0 == 0,
32-
| ~~
32+
| +
3333

3434
error[E0308]: mismatched types
3535
--> $DIR/assignment-expected-bool.rs:14:17
@@ -40,7 +40,7 @@ LL | true => 0 = 0,
4040
help: you might have meant to compare for equality
4141
|
4242
LL | true => 0 == 0,
43-
| ~~
43+
| +
4444

4545
error[E0308]: mismatched types
4646
--> $DIR/assignment-expected-bool.rs:18:8
@@ -51,7 +51,7 @@ LL | if 0 = 0 {}
5151
help: you might have meant to compare for equality
5252
|
5353
LL | if 0 == 0 {}
54-
| ~~
54+
| +
5555

5656
error[E0308]: mismatched types
5757
--> $DIR/assignment-expected-bool.rs:20:24
@@ -62,7 +62,7 @@ LL | let _: bool = if { 0 = 0 } {
6262
help: you might have meant to compare for equality
6363
|
6464
LL | let _: bool = if { 0 == 0 } {
65-
| ~~
65+
| +
6666

6767
error[E0308]: mismatched types
6868
--> $DIR/assignment-expected-bool.rs:21:9
@@ -73,7 +73,7 @@ LL | 0 = 0
7373
help: you might have meant to compare for equality
7474
|
7575
LL | 0 == 0
76-
| ~~
76+
| +
7777

7878
error[E0308]: mismatched types
7979
--> $DIR/assignment-expected-bool.rs:23:9
@@ -84,7 +84,7 @@ LL | 0 = 0
8484
help: you might have meant to compare for equality
8585
|
8686
LL | 0 == 0
87-
| ~~
87+
| +
8888

8989
error[E0308]: mismatched types
9090
--> $DIR/assignment-expected-bool.rs:26:13
@@ -95,7 +95,7 @@ LL | let _ = (0 = 0)
9595
help: you might have meant to compare for equality
9696
|
9797
LL | let _ = (0 == 0)
98-
| ~~
98+
| +
9999

100100
error[E0308]: mismatched types
101101
--> $DIR/assignment-expected-bool.rs:27:14
@@ -106,7 +106,7 @@ LL | && { 0 = 0 }
106106
help: you might have meant to compare for equality
107107
|
108108
LL | && { 0 == 0 }
109-
| ~~
109+
| +
110110

111111
error[E0308]: mismatched types
112112
--> $DIR/assignment-expected-bool.rs:28:12
@@ -117,7 +117,7 @@ LL | || (0 = 0);
117117
help: you might have meant to compare for equality
118118
|
119119
LL | || (0 == 0);
120-
| ~~
120+
| +
121121

122122
error[E0070]: invalid left-hand side of assignment
123123
--> $DIR/assignment-expected-bool.rs:31:22

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

+13
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,17 @@ fn main() {
4040
) {
4141
println!("{}", x);
4242
}
43+
44+
if x == x && x = x && x == x {
45+
//~^ ERROR mismatched types
46+
//~| ERROR mismatched types
47+
//~| ERROR mismatched types
48+
println!("{}", x);
49+
}
50+
51+
if x == x && x == x && x = x {
52+
//~^ ERROR mismatched types
53+
//~| ERROR mismatched types
54+
println!("{}", x);
55+
}
4356
}

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

+47-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | if x = x {
77
help: you might have meant to compare for equality
88
|
99
LL | if x == x {
10-
| ~~
10+
| +
1111

1212
error[E0308]: mismatched types
1313
--> $DIR/assignment-in-if.rs:20:8
@@ -18,7 +18,7 @@ LL | if (x = x) {
1818
help: you might have meant to compare for equality
1919
|
2020
LL | if (x == x) {
21-
| ~~
21+
| +
2222

2323
error[E0308]: mismatched types
2424
--> $DIR/assignment-in-if.rs:25:8
@@ -29,7 +29,7 @@ LL | if y = (Foo { foo: x }) {
2929
help: you might have meant to compare for equality
3030
|
3131
LL | if y == (Foo { foo: x }) {
32-
| ~~
32+
| +
3333

3434
error[E0308]: mismatched types
3535
--> $DIR/assignment-in-if.rs:30:8
@@ -40,7 +40,7 @@ LL | if 3 = x {
4040
help: you might have meant to compare for equality
4141
|
4242
LL | if 3 == x {
43-
| ~~
43+
| +
4444

4545
error[E0308]: mismatched types
4646
--> $DIR/assignment-in-if.rs:36:13
@@ -51,7 +51,7 @@ LL | x = 4
5151
help: you might have meant to compare for equality
5252
|
5353
LL | x == 4
54-
| ~~
54+
| +
5555

5656
error[E0308]: mismatched types
5757
--> $DIR/assignment-in-if.rs:38:13
@@ -62,8 +62,48 @@ LL | x = 5
6262
help: you might have meant to compare for equality
6363
|
6464
LL | x == 5
65-
| ~~
65+
| +
6666

67-
error: aborting due to 6 previous errors
67+
error[E0308]: mismatched types
68+
--> $DIR/assignment-in-if.rs:44:18
69+
|
70+
LL | if x == x && x = x && x == x {
71+
| ^ expected `bool`, found `usize`
72+
73+
error[E0308]: mismatched types
74+
--> $DIR/assignment-in-if.rs:44:22
75+
|
76+
LL | if x == x && x = x && x == x {
77+
| ^ expected `bool`, found `usize`
78+
79+
error[E0308]: mismatched types
80+
--> $DIR/assignment-in-if.rs:44:8
81+
|
82+
LL | if x == x && x = x && x == x {
83+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
84+
|
85+
help: you might have meant to compare for equality
86+
|
87+
LL | if x == x && x == x && x == x {
88+
| +
89+
90+
error[E0308]: mismatched types
91+
--> $DIR/assignment-in-if.rs:51:28
92+
|
93+
LL | if x == x && x == x && x = x {
94+
| ^ expected `bool`, found `usize`
95+
96+
error[E0308]: mismatched types
97+
--> $DIR/assignment-in-if.rs:51:8
98+
|
99+
LL | if x == x && x == x && x = x {
100+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
101+
|
102+
help: you might have meant to compare for equality
103+
|
104+
LL | if x == x && x == x && x == x {
105+
| +
106+
107+
error: aborting due to 11 previous errors
68108

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

0 commit comments

Comments
 (0)