Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest == to wrong assign expr #102708

Merged
merged 2 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions compiler/rustc_hir_analysis/src/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let rhs_ty = self.check_expr(&rhs);
let (applicability, eq) = if self.can_coerce(rhs_ty, lhs_ty) {
(Applicability::MachineApplicable, true)
} else if let ExprKind::Binary(
Spanned { node: hir::BinOpKind::And | hir::BinOpKind::Or, .. },
_,
rhs_expr,
) = lhs.kind
{
let actual_lhs_ty = self.check_expr(&rhs_expr);
(Applicability::MaybeIncorrect, self.can_coerce(rhs_ty, actual_lhs_ty))
} else {
(Applicability::MaybeIncorrect, false)
};
Expand All @@ -1066,9 +1074,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
if eq {
err.span_suggestion_verbose(
span,
span.shrink_to_hi(),
"you might have meant to compare for equality",
"==",
'=',
applicability,
);
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/expr/if/bad-if-let-suggestion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ error[E0308]: mismatched types
|
LL | if let x = 1 && i = 2 {}
| ^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
|
help: you might have meant to compare for equality
|
LL | if let x = 1 && i == 2 {}
| +

error: aborting due to 8 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1510,7 +1510,7 @@ LL | if x = let 0 = 0 {}
help: you might have meant to compare for equality
|
LL | if x == let 0 = 0 {}
| ~~
| +

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

error[E0308]: mismatched types
--> $DIR/disallowed-positions.rs:249:11
Expand Down
22 changes: 11 additions & 11 deletions src/test/ui/type/type-check/assignment-expected-bool.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | let _: bool = 0 = 0;
help: you might have meant to compare for equality
|
LL | let _: bool = 0 == 0;
| ~~
| +

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

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

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

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

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

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

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

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

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

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

error[E0070]: invalid left-hand side of assignment
--> $DIR/assignment-expected-bool.rs:31:22
Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/type/type-check/assignment-in-if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,17 @@ fn main() {
) {
println!("{}", x);
}

if x == x && x = x && x == x {
//~^ ERROR mismatched types
//~| ERROR mismatched types
//~| ERROR mismatched types
println!("{}", x);
}

if x == x && x == x && x = x {
//~^ ERROR mismatched types
//~| ERROR mismatched types
println!("{}", x);
}
}
54 changes: 47 additions & 7 deletions src/test/ui/type/type-check/assignment-in-if.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | if x = x {
help: you might have meant to compare for equality
|
LL | if x == x {
| ~~
| +

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

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

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

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

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

error: aborting due to 6 previous errors
error[E0308]: mismatched types
--> $DIR/assignment-in-if.rs:44:18
|
LL | if x == x && x = x && x == x {
| ^ expected `bool`, found `usize`

error[E0308]: mismatched types
--> $DIR/assignment-in-if.rs:44:22
|
LL | if x == x && x = x && x == x {
| ^ expected `bool`, found `usize`

error[E0308]: mismatched types
--> $DIR/assignment-in-if.rs:44:8
|
LL | if x == x && x = x && x == x {
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
|
help: you might have meant to compare for equality
|
LL | if x == x && x == x && x == x {
| +

error[E0308]: mismatched types
--> $DIR/assignment-in-if.rs:51:28
|
LL | if x == x && x == x && x = x {
| ^ expected `bool`, found `usize`

error[E0308]: mismatched types
--> $DIR/assignment-in-if.rs:51:8
|
LL | if x == x && x == x && x = x {
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
|
help: you might have meant to compare for equality
|
LL | if x == x && x == x && x == x {
| +

error: aborting due to 11 previous errors

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