Skip to content

Commit

Permalink
fix: add deref suggestion for type mismatch binary op
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanjoi committed Jun 23, 2023
1 parent fe37f37 commit 2343980
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
15 changes: 15 additions & 0 deletions compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

if let Some(hir::Node::Expr(hir::Expr {
kind: hir::ExprKind::Binary(_, left, right),
..
})) = self.tcx.hir().find_parent(expr.hir_id) && mutability.is_mut() {
let deref_expr = if expr.hir_id == left.hir_id { right } else { left };
let sugg = vec![(deref_expr.span.shrink_to_lo(), "*".to_string())];
return Some((
sugg,
format!("consider dereferencing here"),
Applicability::MachineApplicable,
true,
false
))
}

let sugg = mutability.ref_prefix_str();
let (sugg, verbose) = if needs_parens {
(
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/suggestions/issue-112958.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() {
let a = &mut 0;
let b = 1;
let _ = a < b;
//~^ ERROR mismatched types
//~| HELP consider dereferencing here
//~| SUGGESTION *
}
16 changes: 16 additions & 0 deletions tests/ui/suggestions/issue-112958.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0308]: mismatched types
--> $DIR/issue-112958.rs:4:17
|
LL | let _ = a < b;
| ^ expected `&mut _`, found integer
|
= note: expected mutable reference `&mut _`
found type `{integer}`
help: consider dereferencing here
|
LL | let _ = *a < b;
| +

error: aborting due to previous error

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

0 comments on commit 2343980

Please sign in to comment.