mirror of https://github.com/rust-lang/rust.git
Suggest lhs deref for binops
This commit is contained in:
parent
ed086d86b8
commit
614ddc9695
|
@ -2326,14 +2326,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let needs_parens = match expr.kind {
|
|
||||||
// parenthesize if needed (Issue #46756)
|
|
||||||
hir::ExprKind::Cast(_, _) | hir::ExprKind::Binary(_, _, _) => true,
|
|
||||||
// parenthesize borrows of range literals (Issue #54505)
|
|
||||||
_ if is_range_literal(expr) => true,
|
|
||||||
_ => false,
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some((sugg, msg)) = self.can_use_as_ref(expr) {
|
if let Some((sugg, msg)) = self.can_use_as_ref(expr) {
|
||||||
return Some((
|
return Some((
|
||||||
sugg,
|
sugg,
|
||||||
|
@ -2361,18 +2353,48 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let sugg = mutability.ref_prefix_str();
|
let make_sugg = |expr: &Expr<'_>, span: Span, sugg: &str| {
|
||||||
let (sugg, verbose) = if needs_parens {
|
let needs_parens = match expr.kind {
|
||||||
(
|
// parenthesize if needed (Issue #46756)
|
||||||
vec![
|
hir::ExprKind::Cast(_, _) | hir::ExprKind::Binary(_, _, _) => true,
|
||||||
(sp.shrink_to_lo(), format!("{prefix}{sugg}(")),
|
// parenthesize borrows of range literals (Issue #54505)
|
||||||
(sp.shrink_to_hi(), ")".to_string()),
|
_ if is_range_literal(expr) => true,
|
||||||
],
|
_ => false,
|
||||||
false,
|
};
|
||||||
)
|
|
||||||
} else {
|
if needs_parens {
|
||||||
(vec![(sp.shrink_to_lo(), format!("{prefix}{sugg}"))], true)
|
(
|
||||||
|
vec![
|
||||||
|
(span.shrink_to_lo(), format!("{prefix}{sugg}(")),
|
||||||
|
(span.shrink_to_hi(), ")".to_string()),
|
||||||
|
],
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
(vec![(span.shrink_to_lo(), format!("{prefix}{sugg}"))], true)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Suggest dereferencing the lhs for expressions such as `&T == T`
|
||||||
|
if let Some(hir::Node::Expr(hir::Expr {
|
||||||
|
kind: hir::ExprKind::Binary(_, lhs, ..),
|
||||||
|
..
|
||||||
|
})) = self.tcx.hir().find_parent(expr.hir_id)
|
||||||
|
&& let &ty::Ref(..) = self.check_expr(lhs).kind()
|
||||||
|
{
|
||||||
|
let (sugg, verbose) = make_sugg(lhs, lhs.span, "*");
|
||||||
|
|
||||||
|
return Some((
|
||||||
|
sugg,
|
||||||
|
"consider dereferencing the borrow".to_string(),
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
verbose,
|
||||||
|
false,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let sugg = mutability.ref_prefix_str();
|
||||||
|
let (sugg, verbose) = make_sugg(expr, sp, sugg);
|
||||||
return Some((
|
return Some((
|
||||||
sugg,
|
sugg,
|
||||||
format!("consider {}borrowing here", mutability.mutably_str()),
|
format!("consider {}borrowing here", mutability.mutably_str()),
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
// Issue #52544
|
||||||
|
// run-rustfix
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let i: &i64 = &1;
|
||||||
|
if *i < 0 {}
|
||||||
|
//~^ ERROR mismatched types [E0308]
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
// Issue #52544
|
||||||
|
// run-rustfix
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let i: &i64 = &1;
|
||||||
|
if i < 0 {}
|
||||||
|
//~^ ERROR mismatched types [E0308]
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
error[E0308]: mismatched types
|
||||||
|
--> $DIR/binary-op-suggest-deref.rs:6:12
|
||||||
|
|
|
||||||
|
LL | if i < 0 {}
|
||||||
|
| ^ expected `&i64`, found integer
|
||||||
|
|
|
||||||
|
help: consider dereferencing the borrow
|
||||||
|
|
|
||||||
|
LL | if *i < 0 {}
|
||||||
|
| +
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0308`.
|
Loading…
Reference in New Issue