Fix bad suggestion for clone/is_some in field init shorthand

This commit is contained in:
yukang 2023-03-20 01:41:48 +08:00
parent c90eb4825a
commit 64f6e4f21c
4 changed files with 104 additions and 10 deletions

View File

@ -985,13 +985,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
)
.must_apply_modulo_regions()
{
diag.span_suggestion_verbose(
expr.span.shrink_to_hi(),
"consider using clone here",
".clone()",
Applicability::MachineApplicable,
);
return true;
let suggestion = match self.maybe_get_struct_pattern_shorthand_field(expr) {
Some(ident) => format!(": {}.clone()", ident),
None => ".clone()".to_string()
};
diag.span_suggestion_verbose(
expr.span.shrink_to_hi(),
"consider using clone here",
suggestion,
Applicability::MachineApplicable,
);
return true;
}
false
}
@ -1153,13 +1158,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return false;
}
diag.span_suggestion(
let suggestion = match self.maybe_get_struct_pattern_shorthand_field(expr) {
Some(ident) => format!(": {}.is_some()", ident),
None => ".is_some()".to_string(),
};
diag.span_suggestion_verbose(
expr.span.shrink_to_hi(),
"use `Option::is_some` to test if the `Option` has a value",
".is_some()",
suggestion,
Applicability::MachineApplicable,
);
true
}

View File

@ -0,0 +1,29 @@
// run-rustfix
#![allow(dead_code)]
struct Foo {
t: Thing
}
#[derive(Clone)]
struct Thing;
fn test_clone() {
let t = &Thing;
let _f = Foo {
t: t.clone() //~ ERROR mismatched types
};
}
struct Bar {
t: bool
}
fn test_is_some() {
let t = Option::<i32>::Some(1);
let _f = Bar {
t: t.is_some() //~ ERROR mismatched types
};
}
fn main() {}

View File

@ -0,0 +1,29 @@
// run-rustfix
#![allow(dead_code)]
struct Foo {
t: Thing
}
#[derive(Clone)]
struct Thing;
fn test_clone() {
let t = &Thing;
let _f = Foo {
t //~ ERROR mismatched types
};
}
struct Bar {
t: bool
}
fn test_is_some() {
let t = Option::<i32>::Some(1);
let _f = Bar {
t //~ ERROR mismatched types
};
}
fn main() {}

View File

@ -0,0 +1,27 @@
error[E0308]: mismatched types
--> $DIR/issue-108470.rs:14:9
|
LL | t
| ^ expected `Thing`, found `&Thing`
|
help: consider using clone here
|
LL | t: t.clone()
| +++++++++++
error[E0308]: mismatched types
--> $DIR/issue-108470.rs:25:9
|
LL | t
| ^ expected `bool`, found `Option<i32>`
|
= note: expected type `bool`
found enum `Option<i32>`
help: use `Option::is_some` to test if the `Option` has a value
|
LL | t: t.is_some()
| +++++++++++++
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0308`.