mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Rollup merge of #109355 - chenyukang:yukang/fix-108470, r=compiler-errors
Fix bad suggestion for clone/is_some in field init shorthand Fixes #108470
This commit is contained in:
commit
f3d3f350cc
@ -983,13 +983,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||||||
)
|
)
|
||||||
.must_apply_modulo_regions()
|
.must_apply_modulo_regions()
|
||||||
{
|
{
|
||||||
diag.span_suggestion_verbose(
|
let suggestion = match self.maybe_get_struct_pattern_shorthand_field(expr) {
|
||||||
expr.span.shrink_to_hi(),
|
Some(ident) => format!(": {}.clone()", ident),
|
||||||
"consider using clone here",
|
None => ".clone()".to_string()
|
||||||
".clone()",
|
};
|
||||||
Applicability::MachineApplicable,
|
|
||||||
);
|
diag.span_suggestion_verbose(
|
||||||
return true;
|
expr.span.shrink_to_hi(),
|
||||||
|
"consider using clone here",
|
||||||
|
suggestion,
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
@ -1150,13 +1155,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||||||
return false;
|
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(),
|
expr.span.shrink_to_hi(),
|
||||||
"use `Option::is_some` to test if the `Option` has a value",
|
"use `Option::is_some` to test if the `Option` has a value",
|
||||||
".is_some()",
|
suggestion,
|
||||||
Applicability::MachineApplicable,
|
Applicability::MachineApplicable,
|
||||||
);
|
);
|
||||||
|
|
||||||
true
|
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 New Issue
Block a user