mirror of
https://github.com/rust-lang/rust
synced 2024-10-14 12:33:57 +00:00
Fix bad suggestion for clone/is_some in field init shorthand
This commit is contained in:
parent
c90eb4825a
commit
64f6e4f21c
|
@ -985,13 +985,18 @@ pub(crate) fn suggest_clone_for_ref(
|
|||
)
|
||||
.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 @@ pub(crate) fn suggest_option_to_bool(
|
|||
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
|
||||
}
|
||||
|
||||
|
|
29
tests/ui/suggestions/issue-108470.fixed
Normal file
29
tests/ui/suggestions/issue-108470.fixed
Normal 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() {}
|
29
tests/ui/suggestions/issue-108470.rs
Normal file
29
tests/ui/suggestions/issue-108470.rs
Normal 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() {}
|
27
tests/ui/suggestions/issue-108470.stderr
Normal file
27
tests/ui/suggestions/issue-108470.stderr
Normal 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`.
|
Loading…
Reference in a new issue