Skip to content

Commit f3d3f35

Browse files
authored
Rollup merge of #109355 - chenyukang:yukang/fix-108470, r=compiler-errors
Fix bad suggestion for clone/is_some in field init shorthand Fixes #108470
2 parents d012d2f + 64f6e4f commit f3d3f35

File tree

4 files changed

+104
-10
lines changed

4 files changed

+104
-10
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -983,13 +983,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
983983
)
984984
.must_apply_modulo_regions()
985985
{
986-
diag.span_suggestion_verbose(
987-
expr.span.shrink_to_hi(),
988-
"consider using clone here",
989-
".clone()",
990-
Applicability::MachineApplicable,
991-
);
992-
return true;
986+
let suggestion = match self.maybe_get_struct_pattern_shorthand_field(expr) {
987+
Some(ident) => format!(": {}.clone()", ident),
988+
None => ".clone()".to_string()
989+
};
990+
991+
diag.span_suggestion_verbose(
992+
expr.span.shrink_to_hi(),
993+
"consider using clone here",
994+
suggestion,
995+
Applicability::MachineApplicable,
996+
);
997+
return true;
993998
}
994999
false
9951000
}
@@ -1150,13 +1155,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11501155
return false;
11511156
}
11521157

1153-
diag.span_suggestion(
1158+
let suggestion = match self.maybe_get_struct_pattern_shorthand_field(expr) {
1159+
Some(ident) => format!(": {}.is_some()", ident),
1160+
None => ".is_some()".to_string(),
1161+
};
1162+
1163+
diag.span_suggestion_verbose(
11541164
expr.span.shrink_to_hi(),
11551165
"use `Option::is_some` to test if the `Option` has a value",
1156-
".is_some()",
1166+
suggestion,
11571167
Applicability::MachineApplicable,
11581168
);
1159-
11601169
true
11611170
}
11621171

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// run-rustfix
2+
#![allow(dead_code)]
3+
4+
struct Foo {
5+
t: Thing
6+
}
7+
8+
#[derive(Clone)]
9+
struct Thing;
10+
11+
fn test_clone() {
12+
let t = &Thing;
13+
let _f = Foo {
14+
t: t.clone() //~ ERROR mismatched types
15+
};
16+
}
17+
18+
struct Bar {
19+
t: bool
20+
}
21+
22+
fn test_is_some() {
23+
let t = Option::<i32>::Some(1);
24+
let _f = Bar {
25+
t: t.is_some() //~ ERROR mismatched types
26+
};
27+
}
28+
29+
fn main() {}

tests/ui/suggestions/issue-108470.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// run-rustfix
2+
#![allow(dead_code)]
3+
4+
struct Foo {
5+
t: Thing
6+
}
7+
8+
#[derive(Clone)]
9+
struct Thing;
10+
11+
fn test_clone() {
12+
let t = &Thing;
13+
let _f = Foo {
14+
t //~ ERROR mismatched types
15+
};
16+
}
17+
18+
struct Bar {
19+
t: bool
20+
}
21+
22+
fn test_is_some() {
23+
let t = Option::<i32>::Some(1);
24+
let _f = Bar {
25+
t //~ ERROR mismatched types
26+
};
27+
}
28+
29+
fn main() {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-108470.rs:14:9
3+
|
4+
LL | t
5+
| ^ expected `Thing`, found `&Thing`
6+
|
7+
help: consider using clone here
8+
|
9+
LL | t: t.clone()
10+
| +++++++++++
11+
12+
error[E0308]: mismatched types
13+
--> $DIR/issue-108470.rs:25:9
14+
|
15+
LL | t
16+
| ^ expected `bool`, found `Option<i32>`
17+
|
18+
= note: expected type `bool`
19+
found enum `Option<i32>`
20+
help: use `Option::is_some` to test if the `Option` has a value
21+
|
22+
LL | t: t.is_some()
23+
| +++++++++++++
24+
25+
error: aborting due to 2 previous errors
26+
27+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)