Skip to content

Commit 00c9d3d

Browse files
Avoid source-map call in operator error
1 parent c2804e6 commit 00c9d3d

File tree

4 files changed

+20
-24
lines changed

4 files changed

+20
-24
lines changed

compiler/rustc_typeck/src/check/op.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
313313
// error types are considered "builtin"
314314
Err(_) if lhs_ty.references_error() || rhs_ty.references_error() => self.tcx.ty_error(),
315315
Err(errors) => {
316-
let source_map = self.tcx.sess.source_map();
317316
let (mut err, missing_trait, use_output) = match is_assign {
318317
IsAssign::Yes => {
319318
let mut err = struct_span_err!(
@@ -448,24 +447,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
448447
)
449448
.is_ok()
450449
{
451-
if let Ok(lstring) = source_map.span_to_snippet(lhs_expr.span) {
452-
let msg = &format!(
453-
"`{}{}` can be used on `{}`, you can dereference `{}`",
454-
op.node.as_str(),
455-
match is_assign {
456-
IsAssign::Yes => "=",
457-
IsAssign::No => "",
458-
},
459-
lhs_deref_ty.peel_refs(),
460-
lstring,
461-
);
462-
err.span_suggestion_verbose(
463-
lhs_expr.span.shrink_to_lo(),
464-
msg,
465-
"*",
466-
rustc_errors::Applicability::MachineApplicable,
467-
);
468-
}
450+
let msg = &format!(
451+
"`{}{}` can be used on `{}` if you dereference the left-hand side",
452+
op.node.as_str(),
453+
match is_assign {
454+
IsAssign::Yes => "=",
455+
IsAssign::No => "",
456+
},
457+
lhs_deref_ty,
458+
);
459+
err.span_suggestion_verbose(
460+
lhs_expr.span.shrink_to_lo(),
461+
msg,
462+
"*",
463+
rustc_errors::Applicability::MachineApplicable,
464+
);
469465
}
470466
};
471467

src/test/ui/binop/binary-op-on-double-ref.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | x % 2 == 0
66
| |
77
| &&{integer}
88
|
9-
help: `%` can be used on `{integer}`, you can dereference `x`
9+
help: `%` can be used on `&{integer}` if you dereference the left-hand side
1010
|
1111
LL | *x % 2 == 0
1212
| +

src/test/ui/typeck/assign-non-lval-derefmut.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ LL | x.lock().unwrap() += 1;
1919
| |
2020
| cannot use `+=` on type `MutexGuard<'_, usize>`
2121
|
22-
help: `+=` can be used on `usize`, you can dereference `x.lock().unwrap()`
22+
help: `+=` can be used on `usize` if you dereference the left-hand side
2323
|
2424
LL | *x.lock().unwrap() += 1;
2525
| +
@@ -47,7 +47,7 @@ LL | y += 1;
4747
| |
4848
| cannot use `+=` on type `MutexGuard<'_, usize>`
4949
|
50-
help: `+=` can be used on `usize`, you can dereference `y`
50+
help: `+=` can be used on `usize` if you dereference the left-hand side
5151
|
5252
LL | *y += 1;
5353
| +

src/test/ui/typeck/assign-non-lval-mut-ref.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ LL | x.last_mut().unwrap() += 1;
1919
| |
2020
| cannot use `+=` on type `&mut usize`
2121
|
22-
help: `+=` can be used on `usize`, you can dereference `x.last_mut().unwrap()`
22+
help: `+=` can be used on `usize` if you dereference the left-hand side
2323
|
2424
LL | *x.last_mut().unwrap() += 1;
2525
| +
@@ -45,7 +45,7 @@ LL | y += 1;
4545
| |
4646
| cannot use `+=` on type `&mut usize`
4747
|
48-
help: `+=` can be used on `usize`, you can dereference `y`
48+
help: `+=` can be used on `usize` if you dereference the left-hand side
4949
|
5050
LL | *y += 1;
5151
| +

0 commit comments

Comments
 (0)