Auto merge of #57158 - estebank:as-ref, r=zackmdavis

Suggest `.as_ref()` when appropriate for `Option` and `Result`

Fix #55198.
This commit is contained in:
bors 2018-12-30 09:51:44 +00:00
commit 7155690ffc
5 changed files with 172 additions and 61 deletions

View file

@ -1,47 +0,0 @@
error[E0308]: mismatched types
--> $DIR/as-ref.rs:6: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:8: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:11: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:13: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`.

View file

@ -12,4 +12,14 @@ fn main() {
//~^ ERROR mismatched types [E0308]
opt.and_then(|arg| Ok(takes_ref(arg)));
//~^ ERROR mismatched types [E0308]
let x: &Option<usize> = &Some(3);
let y: Option<&usize> = x;
//~^ ERROR mismatched types [E0308]
let x: &Result<usize, usize> = &Ok(3);
let y: Result<&usize, &usize> = x;
//~^ ERROR mismatched types [E0308]
// note: do not suggest because of `E: usize`
let x: &Result<usize, usize> = &Ok(3);
let y: Result<&usize, usize> = x;
//~^ ERROR mismatched types [E0308]
}

View file

@ -0,0 +1,81 @@
error[E0308]: mismatched types
--> $DIR/as-ref.rs:6: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:8: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:11: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:13: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[E0308]: mismatched types
--> $DIR/as-ref.rs:16:27
|
LL | let y: Option<&usize> = x;
| ^
| |
| expected enum `std::option::Option`, found reference
| help: you can convert from `&Option<T>` to `Option<&T>` using `.as_ref()`: `x.as_ref()`
|
= note: expected type `std::option::Option<&usize>`
found type `&std::option::Option<usize>`
error[E0308]: mismatched types
--> $DIR/as-ref.rs:19:35
|
LL | let y: Result<&usize, &usize> = x;
| ^ expected enum `std::result::Result`, found reference
|
= note: expected type `std::result::Result<&usize, &usize>`
found type `&std::result::Result<usize, usize>`
help: you can convert from `&Result<T, E>` to `Result<&T, &E>` using `.as_ref()`
|
LL | let y: Result<&usize, &usize> = x.as_ref();
| ^^^^^^^^^^
error[E0308]: mismatched types
--> $DIR/as-ref.rs:23:34
|
LL | let y: Result<&usize, usize> = x;
| ^ expected enum `std::result::Result`, found reference
|
= note: expected type `std::result::Result<&usize, usize>`
found type `&std::result::Result<usize, usize>`
error: aborting due to 7 previous errors
For more information about this error, try `rustc --explain E0308`.