Avoid source-map call in operator error

This commit is contained in:
Michael Goulet 2022-09-04 21:11:50 +00:00
parent c2804e6ec2
commit 00c9d3d2ee
4 changed files with 20 additions and 24 deletions

View File

@ -313,7 +313,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// error types are considered "builtin" // error types are considered "builtin"
Err(_) if lhs_ty.references_error() || rhs_ty.references_error() => self.tcx.ty_error(), Err(_) if lhs_ty.references_error() || rhs_ty.references_error() => self.tcx.ty_error(),
Err(errors) => { Err(errors) => {
let source_map = self.tcx.sess.source_map();
let (mut err, missing_trait, use_output) = match is_assign { let (mut err, missing_trait, use_output) = match is_assign {
IsAssign::Yes => { IsAssign::Yes => {
let mut err = struct_span_err!( let mut err = struct_span_err!(
@ -448,24 +447,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) )
.is_ok() .is_ok()
{ {
if let Ok(lstring) = source_map.span_to_snippet(lhs_expr.span) { let msg = &format!(
let msg = &format!( "`{}{}` can be used on `{}` if you dereference the left-hand side",
"`{}{}` can be used on `{}`, you can dereference `{}`", op.node.as_str(),
op.node.as_str(), match is_assign {
match is_assign { IsAssign::Yes => "=",
IsAssign::Yes => "=", IsAssign::No => "",
IsAssign::No => "", },
}, lhs_deref_ty,
lhs_deref_ty.peel_refs(), );
lstring, err.span_suggestion_verbose(
); lhs_expr.span.shrink_to_lo(),
err.span_suggestion_verbose( msg,
lhs_expr.span.shrink_to_lo(), "*",
msg, rustc_errors::Applicability::MachineApplicable,
"*", );
rustc_errors::Applicability::MachineApplicable,
);
}
} }
}; };

View File

@ -6,7 +6,7 @@ LL | x % 2 == 0
| | | |
| &&{integer} | &&{integer}
| |
help: `%` can be used on `{integer}`, you can dereference `x` help: `%` can be used on `&{integer}` if you dereference the left-hand side
| |
LL | *x % 2 == 0 LL | *x % 2 == 0
| + | +

View File

@ -19,7 +19,7 @@ LL | x.lock().unwrap() += 1;
| | | |
| cannot use `+=` on type `MutexGuard<'_, usize>` | cannot use `+=` on type `MutexGuard<'_, usize>`
| |
help: `+=` can be used on `usize`, you can dereference `x.lock().unwrap()` help: `+=` can be used on `usize` if you dereference the left-hand side
| |
LL | *x.lock().unwrap() += 1; LL | *x.lock().unwrap() += 1;
| + | +
@ -47,7 +47,7 @@ LL | y += 1;
| | | |
| cannot use `+=` on type `MutexGuard<'_, usize>` | cannot use `+=` on type `MutexGuard<'_, usize>`
| |
help: `+=` can be used on `usize`, you can dereference `y` help: `+=` can be used on `usize` if you dereference the left-hand side
| |
LL | *y += 1; LL | *y += 1;
| + | +

View File

@ -19,7 +19,7 @@ LL | x.last_mut().unwrap() += 1;
| | | |
| cannot use `+=` on type `&mut usize` | cannot use `+=` on type `&mut usize`
| |
help: `+=` can be used on `usize`, you can dereference `x.last_mut().unwrap()` help: `+=` can be used on `usize` if you dereference the left-hand side
| |
LL | *x.last_mut().unwrap() += 1; LL | *x.last_mut().unwrap() += 1;
| + | +
@ -45,7 +45,7 @@ LL | y += 1;
| | | |
| cannot use `+=` on type `&mut usize` | cannot use `+=` on type `&mut usize`
| |
help: `+=` can be used on `usize`, you can dereference `y` help: `+=` can be used on `usize` if you dereference the left-hand side
| |
LL | *y += 1; LL | *y += 1;
| + | +