]> git.lizzy.rs Git - rust.git/commitdiff
Use suggestion for dereference help
authorEsteban Küber <esteban@kuber.com.ar>
Thu, 30 Aug 2018 23:01:42 +0000 (16:01 -0700)
committerEsteban Küber <esteban@kuber.com.ar>
Thu, 30 Aug 2018 23:01:42 +0000 (16:01 -0700)
src/librustc_typeck/check/op.rs
src/test/ui/issues/issue-5239-1.stderr

index b7cf6819e21803638ad4ba85f77b54e865354374..edfa62f10953877f9f7e0ef26a99ef0c07edf949 100644 (file)
@@ -256,14 +256,19 @@ fn check_overloaded_binop(&self,
                     let source_map = self.tcx.sess.source_map();
                     match is_assign {
                         IsAssign::Yes => {
-                            let mut err = struct_span_err!(self.tcx.sess, expr.span, E0368,
-                                                "binary assignment operation `{}=` \
-                                                cannot be applied to type `{}`",
-                                                op.node.as_str(),
-                                                lhs_ty);
-                            err.span_label(lhs_expr.span,
-                                    format!("cannot use `{}=` on type `{}`",
-                                    op.node.as_str(), lhs_ty));
+                            let mut err = struct_span_err!(
+                                self.tcx.sess,
+                                expr.span,
+                                E0368,
+                                "binary assignment operation `{}=` cannot be applied to type `{}`",
+                                op.node.as_str(),
+                                lhs_ty,
+                            );
+                            err.span_label(
+                                lhs_expr.span,
+                                format!("cannot use `{}=` on type `{}`",
+                                op.node.as_str(), lhs_ty),
+                            );
                             let mut suggested_deref = false;
                             if let Ref(_, mut rty, _) = lhs_ty.sty {
                                 if {
@@ -280,13 +285,17 @@ fn check_overloaded_binop(&self,
                                             rty = rty_inner;
                                         }
                                         let msg = &format!(
-                                                "`{}=` can be used on '{}', you can \
-                                                dereference `{2}`: `*{2}`",
-                                                op.node.as_str(),
-                                                rty,
-                                                lstring
+                                            "`{}=` can be used on '{}', you can dereference `{}`",
+                                            op.node.as_str(),
+                                            rty,
+                                            lstring,
+                                        );
+                                        err.span_suggestion_with_applicability(
+                                            lhs_expr.span,
+                                            msg,
+                                            format!("*{}", lstring),
+                                            errors::Applicability::MachineApplicable,
                                         );
-                                        err.help(msg);
                                         suggested_deref = true;
                                     }
                                 }
index 7ae01fb7d6012996c1e80ebb82038431117b6aaa..f9191a9fd8229acc496517ffd08bbd07daa5cbaa 100644 (file)
@@ -5,8 +5,10 @@ LL |     let x = |ref x: isize| { x += 1; };
    |                              -^^^^^
    |                              |
    |                              cannot use `+=` on type `&isize`
+help: `+=` can be used on 'isize', you can dereference `x`
    |
-   = help: `+=` can be used on 'isize', you can dereference `x`: `*x`
+LL |     let x = |ref x: isize| { *x += 1; };
+   |                              ^^
 
 error: aborting due to previous error