-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Suggest using
as_ref
on some borrow errors [hack]
When encountering the following code: ```rust struct Foo; fn takes_ref(_: &Foo) {} let ref opt = Some(Foo); opt.map(|arg| takes_ref(arg)); ``` Suggest using `opt.as_ref().map(|arg| takes_ref(arg));` instead. This is a stop gap solution until we expand typeck to deal with these cases in a more graceful way.
- Loading branch information
Showing
3 changed files
with
144 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2018 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. | ||
|
||
struct Foo; | ||
fn takes_ref(_: &Foo) {} | ||
|
||
fn main() { | ||
let ref opt = Some(Foo); | ||
opt.map(|arg| takes_ref(arg)); | ||
//~^ ERROR mismatched types [E0308] | ||
opt.and_then(|arg| Some(takes_ref(arg))); | ||
//~^ ERROR mismatched types [E0308] | ||
let ref opt: Result<_, ()> = Ok(Foo); | ||
opt.map(|arg| takes_ref(arg)); | ||
//~^ ERROR mismatched types [E0308] | ||
opt.and_then(|arg| Ok(takes_ref(arg))); | ||
//~^ ERROR mismatched types [E0308] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/as-ref.rs:16:27 | ||
| | ||
LL | opt.map(|arg| takes_ref(arg)); | ||
| - ^^^ expected &Foo, found struct `Foo` | ||
| | | ||
| help: consider using `as_ref` instead: `as_ref().` | ||
| | ||
= note: expected type `&Foo` | ||
found type `Foo` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/as-ref.rs:18:37 | ||
| | ||
LL | opt.and_then(|arg| Some(takes_ref(arg))); | ||
| - ^^^ expected &Foo, found struct `Foo` | ||
| | | ||
| help: consider using `as_ref` instead: `as_ref().` | ||
| | ||
= note: expected type `&Foo` | ||
found type `Foo` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/as-ref.rs:21:27 | ||
| | ||
LL | opt.map(|arg| takes_ref(arg)); | ||
| - ^^^ expected &Foo, found struct `Foo` | ||
| | | ||
| help: consider using `as_ref` instead: `as_ref().` | ||
| | ||
= note: expected type `&Foo` | ||
found type `Foo` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/as-ref.rs:23:35 | ||
| | ||
LL | opt.and_then(|arg| Ok(takes_ref(arg))); | ||
| - ^^^ expected &Foo, found struct `Foo` | ||
| | | ||
| help: consider using `as_ref` instead: `as_ref().` | ||
| | ||
= note: expected type `&Foo` | ||
found type `Foo` | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |