Skip to content

Commit 1cc42ea

Browse files
committed
Add support for suggesting as_ref to Result accesses
1 parent 4b09636 commit 1cc42ea

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

src/librustc_mir/borrow_check/move_errors.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,16 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
319319
original_path.ty(self.mir, self.infcx.tcx).ty,
320320
);
321321
let snippet = self.infcx.tcx.sess.source_map().span_to_snippet(span).unwrap();
322-
if orig_path_ty.starts_with("std::option::Option") {
322+
let is_option = orig_path_ty.starts_with("std::option::Option");
323+
let is_result = orig_path_ty.starts_with("std::result::Result");
324+
if is_option || is_result {
323325
err.span_suggestion(
324326
span,
325-
"consider borrowing the `Option`'s content",
327+
&format!("consider borrowing the `{}`'s content", if is_option {
328+
"Option"
329+
} else {
330+
"Result"
331+
}),
326332
format!("{}.as_ref()", snippet),
327333
Applicability::MaybeIncorrect,
328334
);

src/test/ui/suggestions/option-content-move.fixed

+18
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,22 @@ impl LipogramCorpora {
1818
}
1919
}
2020

21+
pub struct LipogramCorpora2 {
22+
selections: Vec<(char, Result<String, String>)>,
23+
}
24+
25+
impl LipogramCorpora2 {
26+
pub fn validate_all(&mut self) -> Result<(), char> {
27+
for selection in &self.selections {
28+
if selection.1.is_ok() {
29+
if selection.1.as_ref().unwrap().contains(selection.0) {
30+
//~^ ERROR cannot move out of borrowed content
31+
return Err(selection.0);
32+
}
33+
}
34+
}
35+
Ok(())
36+
}
37+
}
38+
2139
fn main() {}

src/test/ui/suggestions/option-content-move.rs

+18
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,22 @@ impl LipogramCorpora {
1818
}
1919
}
2020

21+
pub struct LipogramCorpora2 {
22+
selections: Vec<(char, Result<String, String>)>,
23+
}
24+
25+
impl LipogramCorpora2 {
26+
pub fn validate_all(&mut self) -> Result<(), char> {
27+
for selection in &self.selections {
28+
if selection.1.is_ok() {
29+
if selection.1.unwrap().contains(selection.0) {
30+
//~^ ERROR cannot move out of borrowed content
31+
return Err(selection.0);
32+
}
33+
}
34+
}
35+
Ok(())
36+
}
37+
}
38+
2139
fn main() {}

src/test/ui/suggestions/option-content-move.stderr

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ LL | if selection.1.unwrap().contains(selection.0) {
77
| cannot move out of borrowed content
88
| help: consider borrowing the `Option`'s content: `selection.1.as_ref()`
99

10-
error: aborting due to previous error
10+
error[E0507]: cannot move out of borrowed content
11+
--> $DIR/option-content-move.rs:29:20
12+
|
13+
LL | if selection.1.unwrap().contains(selection.0) {
14+
| ^^^^^^^^^^^
15+
| |
16+
| cannot move out of borrowed content
17+
| help: consider borrowing the `Result`'s content: `selection.1.as_ref()`
18+
19+
error: aborting due to 2 previous errors
1120

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

0 commit comments

Comments
 (0)