Skip to content

Commit

Permalink
Add support for suggesting as_ref to Result accesses
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed May 25, 2019
1 parent 4b09636 commit 1cc42ea
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/librustc_mir/borrow_check/move_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,16 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
original_path.ty(self.mir, self.infcx.tcx).ty,
);
let snippet = self.infcx.tcx.sess.source_map().span_to_snippet(span).unwrap();
if orig_path_ty.starts_with("std::option::Option") {
let is_option = orig_path_ty.starts_with("std::option::Option");
let is_result = orig_path_ty.starts_with("std::result::Result");
if is_option || is_result {
err.span_suggestion(
span,
"consider borrowing the `Option`'s content",
&format!("consider borrowing the `{}`'s content", if is_option {
"Option"
} else {
"Result"
}),
format!("{}.as_ref()", snippet),
Applicability::MaybeIncorrect,
);
Expand Down
18 changes: 18 additions & 0 deletions src/test/ui/suggestions/option-content-move.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,22 @@ impl LipogramCorpora {
}
}

pub struct LipogramCorpora2 {
selections: Vec<(char, Result<String, String>)>,
}

impl LipogramCorpora2 {
pub fn validate_all(&mut self) -> Result<(), char> {
for selection in &self.selections {
if selection.1.is_ok() {
if selection.1.as_ref().unwrap().contains(selection.0) {
//~^ ERROR cannot move out of borrowed content
return Err(selection.0);
}
}
}
Ok(())
}
}

fn main() {}
18 changes: 18 additions & 0 deletions src/test/ui/suggestions/option-content-move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,22 @@ impl LipogramCorpora {
}
}

pub struct LipogramCorpora2 {
selections: Vec<(char, Result<String, String>)>,
}

impl LipogramCorpora2 {
pub fn validate_all(&mut self) -> Result<(), char> {
for selection in &self.selections {
if selection.1.is_ok() {
if selection.1.unwrap().contains(selection.0) {
//~^ ERROR cannot move out of borrowed content
return Err(selection.0);
}
}
}
Ok(())
}
}

fn main() {}
11 changes: 10 additions & 1 deletion src/test/ui/suggestions/option-content-move.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ LL | if selection.1.unwrap().contains(selection.0) {
| cannot move out of borrowed content
| help: consider borrowing the `Option`'s content: `selection.1.as_ref()`

error: aborting due to previous error
error[E0507]: cannot move out of borrowed content
--> $DIR/option-content-move.rs:29:20
|
LL | if selection.1.unwrap().contains(selection.0) {
| ^^^^^^^^^^^
| |
| cannot move out of borrowed content
| help: consider borrowing the `Result`'s content: `selection.1.as_ref()`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0507`.

0 comments on commit 1cc42ea

Please sign in to comment.