Skip to content

Commit b0a7d6e

Browse files
Suggest deref on comparison binop RHS even if type is not Copy
1 parent de96f3d commit b0a7d6e

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15081508
// FIXME(compiler-errors): We can actually do this if the checked_ty is
15091509
// `steps` layers of boxes, not just one, but this is easier and most likely.
15101510
|| (checked_ty.is_box() && steps == 1)
1511+
// We can always deref a binop that takes its arguments by ref.
1512+
|| matches!(
1513+
self.tcx.hir().get_parent(expr.hir_id),
1514+
hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Binary(op, ..), .. })
1515+
if !op.node.is_by_value()
1516+
)
15111517
{
15121518
let deref_kind = if checked_ty.is_box() {
15131519
"unboxing the value"

tests/ui/inference/deref-suggestion.rs

+9
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,13 @@ fn main() {
7272
} else {
7373
&0
7474
};
75+
76+
#[derive(PartialEq, Eq)]
77+
struct Foo;
78+
let foo = Foo;
79+
let bar = &Foo;
80+
81+
if foo == bar {
82+
//~^ ERROR mismatched types
83+
}
7584
}

tests/ui/inference/deref-suggestion.stderr

+14-1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,19 @@ LL | || };
175175
| |_____`if` and `else` have incompatible types
176176
| expected `i32`, found `&{integer}`
177177

178-
error: aborting due to 13 previous errors
178+
error[E0308]: mismatched types
179+
--> $DIR/deref-suggestion.rs:81:15
180+
|
181+
LL | if foo == bar {
182+
| --- ^^^ expected `Foo`, found `&Foo`
183+
| |
184+
| expected because this is `Foo`
185+
|
186+
help: consider dereferencing the borrow
187+
|
188+
LL | if foo == *bar {
189+
| +
190+
191+
error: aborting due to 14 previous errors
179192

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

0 commit comments

Comments
 (0)