-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More fix on mismatched type suggestion for shorthand fields
- Loading branch information
1 parent
9386e14
commit 3a4edf0
Showing
5 changed files
with
266 additions
and
9 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
77 changes: 77 additions & 0 deletions
77
tests/ui/mismatched_types/mismatch-sugg-for-shorthand-field.fixed
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,77 @@ | ||
// run-rustfix | ||
// edition:2021 | ||
#![allow(dead_code)] | ||
#![allow(unused_variables)] | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
|
||
fn test1() { | ||
let string = String::from("Hello, world"); | ||
|
||
struct Demo<'a> { | ||
option: Option<&'a str>, | ||
} | ||
|
||
let option: Option<String> = Some(string.clone()); | ||
let s = Demo { option: option.as_deref() }; //~ ERROR mismatched types | ||
} | ||
|
||
fn test2() { | ||
let string = String::from("Hello, world"); | ||
|
||
struct Demo<'a> { | ||
option_ref: Option<&'a str>, | ||
} | ||
|
||
let option_ref = Some(&string); | ||
let s = Demo { option_ref: option_ref.map(|x| x.as_str()) }; //~ ERROR mismatched types | ||
} | ||
|
||
fn test3() { | ||
let string = String::from("Hello, world"); | ||
|
||
struct Demo<'a> { | ||
option_ref_ref: Option<&'a str>, | ||
} | ||
|
||
let option_ref = Some(&string); | ||
let option_ref_ref = option_ref.as_ref(); | ||
|
||
let s = Demo { option_ref_ref: option_ref_ref.map(|x| x.as_str()) }; //~ ERROR mismatched types | ||
} | ||
|
||
fn test4() { | ||
let a = 1; | ||
struct Demo { | ||
a: String, | ||
} | ||
let s = Demo { a: a.to_string() }; //~ ERROR mismatched types | ||
} | ||
|
||
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>; | ||
fn test5() { | ||
let a = async { 42 }; | ||
struct Demo { | ||
a: BoxFuture<'static, i32>, | ||
} | ||
let s = Demo { a: Box::pin(a) }; //~ ERROR mismatched types | ||
} | ||
|
||
fn test6() { | ||
struct A; | ||
struct B; | ||
|
||
impl From<B> for A { | ||
fn from(_: B) -> Self { | ||
A | ||
} | ||
} | ||
|
||
struct Demo { | ||
a: A, | ||
} | ||
let a = B; | ||
let s = Demo { a: a.into() }; //~ ERROR mismatched types | ||
} | ||
|
||
fn main() {} |
77 changes: 77 additions & 0 deletions
77
tests/ui/mismatched_types/mismatch-sugg-for-shorthand-field.rs
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,77 @@ | ||
// run-rustfix | ||
// edition:2021 | ||
#![allow(dead_code)] | ||
#![allow(unused_variables)] | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
|
||
fn test1() { | ||
let string = String::from("Hello, world"); | ||
|
||
struct Demo<'a> { | ||
option: Option<&'a str>, | ||
} | ||
|
||
let option: Option<String> = Some(string.clone()); | ||
let s = Demo { option }; //~ ERROR mismatched types | ||
} | ||
|
||
fn test2() { | ||
let string = String::from("Hello, world"); | ||
|
||
struct Demo<'a> { | ||
option_ref: Option<&'a str>, | ||
} | ||
|
||
let option_ref = Some(&string); | ||
let s = Demo { option_ref }; //~ ERROR mismatched types | ||
} | ||
|
||
fn test3() { | ||
let string = String::from("Hello, world"); | ||
|
||
struct Demo<'a> { | ||
option_ref_ref: Option<&'a str>, | ||
} | ||
|
||
let option_ref = Some(&string); | ||
let option_ref_ref = option_ref.as_ref(); | ||
|
||
let s = Demo { option_ref_ref }; //~ ERROR mismatched types | ||
} | ||
|
||
fn test4() { | ||
let a = 1; | ||
struct Demo { | ||
a: String, | ||
} | ||
let s = Demo { a }; //~ ERROR mismatched types | ||
} | ||
|
||
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>; | ||
fn test5() { | ||
let a = async { 42 }; | ||
struct Demo { | ||
a: BoxFuture<'static, i32>, | ||
} | ||
let s = Demo { a }; //~ ERROR mismatched types | ||
} | ||
|
||
fn test6() { | ||
struct A; | ||
struct B; | ||
|
||
impl From<B> for A { | ||
fn from(_: B) -> Self { | ||
A | ||
} | ||
} | ||
|
||
struct Demo { | ||
a: A, | ||
} | ||
let a = B; | ||
let s = Demo { a }; //~ ERROR mismatched types | ||
} | ||
|
||
fn main() {} |
80 changes: 80 additions & 0 deletions
80
tests/ui/mismatched_types/mismatch-sugg-for-shorthand-field.stderr
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,80 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/mismatch-sugg-for-shorthand-field.rs:16:20 | ||
| | ||
LL | let s = Demo { option }; | ||
| ^^^^^^ expected `Option<&str>`, found `Option<String>` | ||
| | ||
= note: expected enum `Option<&str>` | ||
found enum `Option<String>` | ||
help: try using `: option.as_deref()` to convert `Option<String>` to `Option<&str>` | ||
| | ||
LL | let s = Demo { option: option.as_deref() }; | ||
| +++++++++++++++++++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/mismatch-sugg-for-shorthand-field.rs:27:20 | ||
| | ||
LL | let s = Demo { option_ref }; | ||
| ^^^^^^^^^^ expected `Option<&str>`, found `Option<&String>` | ||
| | ||
= note: expected enum `Option<&str>` | ||
found enum `Option<&String>` | ||
help: try converting the passed type into a `&str` | ||
| | ||
LL | let s = Demo { option_ref: option_ref.map(|x| x.as_str()) }; | ||
| ++++++++++++++++++++++++++++++++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/mismatch-sugg-for-shorthand-field.rs:40:20 | ||
| | ||
LL | let s = Demo { option_ref_ref }; | ||
| ^^^^^^^^^^^^^^ expected `Option<&str>`, found `Option<&&String>` | ||
| | ||
= note: expected enum `Option<&str>` | ||
found enum `Option<&&String>` | ||
help: try converting the passed type into a `&str` | ||
| | ||
LL | let s = Demo { option_ref_ref: option_ref_ref.map(|x| x.as_str()) }; | ||
| ++++++++++++++++++++++++++++++++++++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/mismatch-sugg-for-shorthand-field.rs:48:20 | ||
| | ||
LL | let s = Demo { a }; | ||
| ^ expected `String`, found integer | ||
| | ||
help: try using a conversion method | ||
| | ||
LL | let s = Demo { a: a.to_string() }; | ||
| ++ ++++++++++++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/mismatch-sugg-for-shorthand-field.rs:57:20 | ||
| | ||
LL | let a = async { 42 }; | ||
| ------------ the found `async` block | ||
... | ||
LL | let s = Demo { a }; | ||
| ^ expected `Pin<Box<...>>`, found `async` block | ||
| | ||
= note: expected struct `Pin<Box<(dyn Future<Output = i32> + Send + 'static)>>` | ||
found `async` block `{async block@$DIR/mismatch-sugg-for-shorthand-field.rs:53:13: 53:25}` | ||
help: you need to pin and box this expression | ||
| | ||
LL | let s = Demo { a: Box::pin(a) }; | ||
| ++++++++++++ + | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/mismatch-sugg-for-shorthand-field.rs:74:20 | ||
| | ||
LL | let s = Demo { a }; | ||
| ^ expected `A`, found `B` | ||
| | ||
help: call `Into::into` on this expression to convert `B` into `A` | ||
| | ||
LL | let s = Demo { a: a.into() }; | ||
| ++ +++++++ | ||
|
||
error: aborting due to 6 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |