Skip to content

Commit 013710e

Browse files
committed
Use suggestion for dereference help
1 parent da9ba98 commit 013710e

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

src/librustc_typeck/check/op.rs

+23-14
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,19 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
256256
let source_map = self.tcx.sess.source_map();
257257
match is_assign {
258258
IsAssign::Yes => {
259-
let mut err = struct_span_err!(self.tcx.sess, expr.span, E0368,
260-
"binary assignment operation `{}=` \
261-
cannot be applied to type `{}`",
262-
op.node.as_str(),
263-
lhs_ty);
264-
err.span_label(lhs_expr.span,
265-
format!("cannot use `{}=` on type `{}`",
266-
op.node.as_str(), lhs_ty));
259+
let mut err = struct_span_err!(
260+
self.tcx.sess,
261+
expr.span,
262+
E0368,
263+
"binary assignment operation `{}=` cannot be applied to type `{}`",
264+
op.node.as_str(),
265+
lhs_ty,
266+
);
267+
err.span_label(
268+
lhs_expr.span,
269+
format!("cannot use `{}=` on type `{}`",
270+
op.node.as_str(), lhs_ty),
271+
);
267272
let mut suggested_deref = false;
268273
if let Ref(_, mut rty, _) = lhs_ty.sty {
269274
if {
@@ -280,13 +285,17 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
280285
rty = rty_inner;
281286
}
282287
let msg = &format!(
283-
"`{}=` can be used on '{}', you can \
284-
dereference `{2}`: `*{2}`",
285-
op.node.as_str(),
286-
rty,
287-
lstring
288+
"`{}=` can be used on '{}', you can dereference `{}`",
289+
op.node.as_str(),
290+
rty,
291+
lstring,
292+
);
293+
err.span_suggestion_with_applicability(
294+
lhs_expr.span,
295+
msg,
296+
format!("*{}", lstring),
297+
errors::Applicability::MachineApplicable,
288298
);
289-
err.help(msg);
290299
suggested_deref = true;
291300
}
292301
}

src/test/ui/issues/issue-5239-1.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ LL | let x = |ref x: isize| { x += 1; };
55
| -^^^^^
66
| |
77
| cannot use `+=` on type `&isize`
8+
help: `+=` can be used on 'isize', you can dereference `x`
89
|
9-
= help: `+=` can be used on 'isize', you can dereference `x`: `*x`
10+
LL | let x = |ref x: isize| { *x += 1; };
11+
| ^^
1012

1113
error: aborting due to previous error
1214

0 commit comments

Comments
 (0)