Suggest using ref inline in an error

This commit is contained in:
P1start 2015-07-06 18:16:46 +12:00
parent fe0b5c0d38
commit 0bf9fbb6dd
2 changed files with 46 additions and 2 deletions

View File

@ -705,8 +705,19 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
ol,
moved_lp_msg,
pat_ty));
self.tcx.sess.fileline_help(span,
"use `ref` to override");
match self.tcx.sess.codemap().span_to_snippet(span) {
Ok(string) => {
self.tcx.sess.span_suggestion(
span,
&format!("if you would like to borrow the value instead, \
use a `ref` binding as shown:"),
format!("ref {}", string));
},
Err(_) => {
self.tcx.sess.fileline_help(span,
"use `ref` to override");
},
}
}
move_data::Captured => {

View File

@ -0,0 +1,33 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
fn main() {
let x = vec![1];
let y = x;
//~^ HELP use a `ref` binding as shown
//~| SUGGESTION let ref y = x;
x; //~ ERROR use of moved value
let x = vec![1];
let mut y = x;
//~^ HELP use a `ref` binding as shown
//~| SUGGESTION let ref mut y = x;
x; //~ ERROR use of moved value
let x = (Some(vec![1]), ());
match x {
(Some(y), ()) => {},
//~^ HELP use a `ref` binding as shown
//~| SUGGESTION (Some(ref y), ()) => {},
_ => {},
}
x; //~ ERROR use of partially moved value
}