Skip to content

Commit 614ddc9

Browse files
committed
Suggest lhs deref for binops
1 parent ed086d8 commit 614ddc9

File tree

4 files changed

+71
-19
lines changed

4 files changed

+71
-19
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+41-19
Original file line numberDiff line numberDiff line change
@@ -2326,14 +2326,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23262326
));
23272327
}
23282328

2329-
let needs_parens = match expr.kind {
2330-
// parenthesize if needed (Issue #46756)
2331-
hir::ExprKind::Cast(_, _) | hir::ExprKind::Binary(_, _, _) => true,
2332-
// parenthesize borrows of range literals (Issue #54505)
2333-
_ if is_range_literal(expr) => true,
2334-
_ => false,
2335-
};
2336-
23372329
if let Some((sugg, msg)) = self.can_use_as_ref(expr) {
23382330
return Some((
23392331
sugg,
@@ -2361,18 +2353,48 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23612353
}
23622354
}
23632355

2364-
let sugg = mutability.ref_prefix_str();
2365-
let (sugg, verbose) = if needs_parens {
2366-
(
2367-
vec![
2368-
(sp.shrink_to_lo(), format!("{prefix}{sugg}(")),
2369-
(sp.shrink_to_hi(), ")".to_string()),
2370-
],
2371-
false,
2372-
)
2373-
} else {
2374-
(vec![(sp.shrink_to_lo(), format!("{prefix}{sugg}"))], true)
2356+
let make_sugg = |expr: &Expr<'_>, span: Span, sugg: &str| {
2357+
let needs_parens = match expr.kind {
2358+
// parenthesize if needed (Issue #46756)
2359+
hir::ExprKind::Cast(_, _) | hir::ExprKind::Binary(_, _, _) => true,
2360+
// parenthesize borrows of range literals (Issue #54505)
2361+
_ if is_range_literal(expr) => true,
2362+
_ => false,
2363+
};
2364+
2365+
if needs_parens {
2366+
(
2367+
vec![
2368+
(span.shrink_to_lo(), format!("{prefix}{sugg}(")),
2369+
(span.shrink_to_hi(), ")".to_string()),
2370+
],
2371+
false,
2372+
)
2373+
} else {
2374+
(vec![(span.shrink_to_lo(), format!("{prefix}{sugg}"))], true)
2375+
}
23752376
};
2377+
2378+
// Suggest dereferencing the lhs for expressions such as `&T == T`
2379+
if let Some(hir::Node::Expr(hir::Expr {
2380+
kind: hir::ExprKind::Binary(_, lhs, ..),
2381+
..
2382+
})) = self.tcx.hir().find_parent(expr.hir_id)
2383+
&& let &ty::Ref(..) = self.check_expr(lhs).kind()
2384+
{
2385+
let (sugg, verbose) = make_sugg(lhs, lhs.span, "*");
2386+
2387+
return Some((
2388+
sugg,
2389+
"consider dereferencing the borrow".to_string(),
2390+
Applicability::MachineApplicable,
2391+
verbose,
2392+
false,
2393+
));
2394+
}
2395+
2396+
let sugg = mutability.ref_prefix_str();
2397+
let (sugg, verbose) = make_sugg(expr, sp, sugg);
23762398
return Some((
23772399
sugg,
23782400
format!("consider {}borrowing here", mutability.mutably_str()),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Issue #52544
2+
// run-rustfix
3+
4+
fn main() {
5+
let i: &i64 = &1;
6+
if *i < 0 {}
7+
//~^ ERROR mismatched types [E0308]
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Issue #52544
2+
// run-rustfix
3+
4+
fn main() {
5+
let i: &i64 = &1;
6+
if i < 0 {}
7+
//~^ ERROR mismatched types [E0308]
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/binary-op-suggest-deref.rs:6:12
3+
|
4+
LL | if i < 0 {}
5+
| ^ expected `&i64`, found integer
6+
|
7+
help: consider dereferencing the borrow
8+
|
9+
LL | if *i < 0 {}
10+
| +
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)