Skip to content

Commit 2343980

Browse files
committed
fix: add deref suggestion for type mismatch binary op
1 parent fe37f37 commit 2343980

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14171417
}
14181418
}
14191419

1420+
if let Some(hir::Node::Expr(hir::Expr {
1421+
kind: hir::ExprKind::Binary(_, left, right),
1422+
..
1423+
})) = self.tcx.hir().find_parent(expr.hir_id) && mutability.is_mut() {
1424+
let deref_expr = if expr.hir_id == left.hir_id { right } else { left };
1425+
let sugg = vec![(deref_expr.span.shrink_to_lo(), "*".to_string())];
1426+
return Some((
1427+
sugg,
1428+
format!("consider dereferencing here"),
1429+
Applicability::MachineApplicable,
1430+
true,
1431+
false
1432+
))
1433+
}
1434+
14201435
let sugg = mutability.ref_prefix_str();
14211436
let (sugg, verbose) = if needs_parens {
14221437
(

tests/ui/suggestions/issue-112958.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
let a = &mut 0;
3+
let b = 1;
4+
let _ = a < b;
5+
//~^ ERROR mismatched types
6+
//~| HELP consider dereferencing here
7+
//~| SUGGESTION *
8+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-112958.rs:4:17
3+
|
4+
LL | let _ = a < b;
5+
| ^ expected `&mut _`, found integer
6+
|
7+
= note: expected mutable reference `&mut _`
8+
found type `{integer}`
9+
help: consider dereferencing here
10+
|
11+
LL | let _ = *a < b;
12+
| +
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)