Skip to content

Commit d89e99a

Browse files
authored
Rollup merge of #99593 - TaKO8Ki:suggest-removing-tuple-struct-field, r=compiler-errors
Suggest removing the tuple struct field for the unwrapped value fixes #99416
2 parents 2944454 + f85f375 commit d89e99a

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

compiler/rustc_typeck/src/check/demand.rs

+15
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
287287
expr_ty: Ty<'tcx>,
288288
) {
289289
if let ty::Adt(expected_adt, substs) = expected.kind() {
290+
if let hir::ExprKind::Field(base, ident) = expr.kind {
291+
let base_ty = self.typeck_results.borrow().expr_ty(base);
292+
if self.can_eq(self.param_env, base_ty, expected).is_ok()
293+
&& let Some(base_span) = base.span.find_ancestor_inside(expr.span)
294+
{
295+
err.span_suggestion_verbose(
296+
expr.span.with_lo(base_span.hi()),
297+
format!("consider removing the tuple struct field `{ident}`"),
298+
"",
299+
Applicability::MaybeIncorrect,
300+
);
301+
return
302+
}
303+
}
304+
290305
// If the expression is of type () and it's the return expression of a block,
291306
// we suggest adding a separate return expression instead.
292307
// (To avoid things like suggesting `Ok(while .. { .. })`.)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// run-rustfix
2+
3+
macro_rules! my_wrapper {
4+
($expr:expr) => { MyWrapper($expr) }
5+
}
6+
7+
pub struct MyWrapper(u32);
8+
9+
fn main() {
10+
let value = MyWrapper(123);
11+
some_fn(value); //~ ERROR mismatched types
12+
some_fn(my_wrapper!(123)); //~ ERROR mismatched types
13+
}
14+
15+
fn some_fn(wrapped: MyWrapper) {
16+
drop(wrapped);
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// run-rustfix
2+
3+
macro_rules! my_wrapper {
4+
($expr:expr) => { MyWrapper($expr) }
5+
}
6+
7+
pub struct MyWrapper(u32);
8+
9+
fn main() {
10+
let value = MyWrapper(123);
11+
some_fn(value.0); //~ ERROR mismatched types
12+
some_fn(my_wrapper!(123).0); //~ ERROR mismatched types
13+
}
14+
15+
fn some_fn(wrapped: MyWrapper) {
16+
drop(wrapped);
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/suggest-removing-tulpe-struct-field.rs:11:13
3+
|
4+
LL | some_fn(value.0);
5+
| ------- ^^^^^^^ expected struct `MyWrapper`, found `u32`
6+
| |
7+
| arguments to this function are incorrect
8+
|
9+
note: function defined here
10+
--> $DIR/suggest-removing-tulpe-struct-field.rs:15:4
11+
|
12+
LL | fn some_fn(wrapped: MyWrapper) {
13+
| ^^^^^^^ ------------------
14+
help: consider removing the tuple struct field `0`
15+
|
16+
LL - some_fn(value.0);
17+
LL + some_fn(value);
18+
|
19+
20+
error[E0308]: mismatched types
21+
--> $DIR/suggest-removing-tulpe-struct-field.rs:12:13
22+
|
23+
LL | some_fn(my_wrapper!(123).0);
24+
| ------- ^^^^^^^^^^^^^^^^^^ expected struct `MyWrapper`, found `u32`
25+
| |
26+
| arguments to this function are incorrect
27+
|
28+
note: function defined here
29+
--> $DIR/suggest-removing-tulpe-struct-field.rs:15:4
30+
|
31+
LL | fn some_fn(wrapped: MyWrapper) {
32+
| ^^^^^^^ ------------------
33+
help: consider removing the tuple struct field `0`
34+
|
35+
LL - some_fn(my_wrapper!(123).0);
36+
LL + some_fn(my_wrapper!(123));
37+
|
38+
39+
error: aborting due to 2 previous errors
40+
41+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)