From f85f37583d91152b967cd0d1bd7253b339a26d42 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Fri, 22 Jul 2022 15:53:00 +0900 Subject: [PATCH] suggest removing the tuple struct field for the unwrapped value add a test case for macro --- compiler/rustc_typeck/src/check/demand.rs | 15 +++++++ .../suggest-removing-tulpe-struct-field.fixed | 17 ++++++++ .../suggest-removing-tulpe-struct-field.rs | 17 ++++++++ ...suggest-removing-tulpe-struct-field.stderr | 41 +++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 src/test/ui/mismatched_types/suggest-removing-tulpe-struct-field.fixed create mode 100644 src/test/ui/mismatched_types/suggest-removing-tulpe-struct-field.rs create mode 100644 src/test/ui/mismatched_types/suggest-removing-tulpe-struct-field.stderr diff --git a/compiler/rustc_typeck/src/check/demand.rs b/compiler/rustc_typeck/src/check/demand.rs index a2d8765289c55..381c3a4ea1f7e 100644 --- a/compiler/rustc_typeck/src/check/demand.rs +++ b/compiler/rustc_typeck/src/check/demand.rs @@ -287,6 +287,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { expr_ty: Ty<'tcx>, ) { if let ty::Adt(expected_adt, substs) = expected.kind() { + if let hir::ExprKind::Field(base, ident) = expr.kind { + let base_ty = self.typeck_results.borrow().expr_ty(base); + if self.can_eq(self.param_env, base_ty, expected).is_ok() + && let Some(base_span) = base.span.find_ancestor_inside(expr.span) + { + err.span_suggestion_verbose( + expr.span.with_lo(base_span.hi()), + format!("consider removing the tuple struct field `{ident}`"), + "", + Applicability::MaybeIncorrect, + ); + return + } + } + // If the expression is of type () and it's the return expression of a block, // we suggest adding a separate return expression instead. // (To avoid things like suggesting `Ok(while .. { .. })`.) diff --git a/src/test/ui/mismatched_types/suggest-removing-tulpe-struct-field.fixed b/src/test/ui/mismatched_types/suggest-removing-tulpe-struct-field.fixed new file mode 100644 index 0000000000000..63b65ab20fea8 --- /dev/null +++ b/src/test/ui/mismatched_types/suggest-removing-tulpe-struct-field.fixed @@ -0,0 +1,17 @@ +// run-rustfix + +macro_rules! my_wrapper { + ($expr:expr) => { MyWrapper($expr) } +} + +pub struct MyWrapper(u32); + +fn main() { + let value = MyWrapper(123); + some_fn(value); //~ ERROR mismatched types + some_fn(my_wrapper!(123)); //~ ERROR mismatched types +} + +fn some_fn(wrapped: MyWrapper) { + drop(wrapped); +} diff --git a/src/test/ui/mismatched_types/suggest-removing-tulpe-struct-field.rs b/src/test/ui/mismatched_types/suggest-removing-tulpe-struct-field.rs new file mode 100644 index 0000000000000..2ab4e3955f336 --- /dev/null +++ b/src/test/ui/mismatched_types/suggest-removing-tulpe-struct-field.rs @@ -0,0 +1,17 @@ +// run-rustfix + +macro_rules! my_wrapper { + ($expr:expr) => { MyWrapper($expr) } +} + +pub struct MyWrapper(u32); + +fn main() { + let value = MyWrapper(123); + some_fn(value.0); //~ ERROR mismatched types + some_fn(my_wrapper!(123).0); //~ ERROR mismatched types +} + +fn some_fn(wrapped: MyWrapper) { + drop(wrapped); +} diff --git a/src/test/ui/mismatched_types/suggest-removing-tulpe-struct-field.stderr b/src/test/ui/mismatched_types/suggest-removing-tulpe-struct-field.stderr new file mode 100644 index 0000000000000..82a7f276372c9 --- /dev/null +++ b/src/test/ui/mismatched_types/suggest-removing-tulpe-struct-field.stderr @@ -0,0 +1,41 @@ +error[E0308]: mismatched types + --> $DIR/suggest-removing-tulpe-struct-field.rs:11:13 + | +LL | some_fn(value.0); + | ------- ^^^^^^^ expected struct `MyWrapper`, found `u32` + | | + | arguments to this function are incorrect + | +note: function defined here + --> $DIR/suggest-removing-tulpe-struct-field.rs:15:4 + | +LL | fn some_fn(wrapped: MyWrapper) { + | ^^^^^^^ ------------------ +help: consider removing the tuple struct field `0` + | +LL - some_fn(value.0); +LL + some_fn(value); + | + +error[E0308]: mismatched types + --> $DIR/suggest-removing-tulpe-struct-field.rs:12:13 + | +LL | some_fn(my_wrapper!(123).0); + | ------- ^^^^^^^^^^^^^^^^^^ expected struct `MyWrapper`, found `u32` + | | + | arguments to this function are incorrect + | +note: function defined here + --> $DIR/suggest-removing-tulpe-struct-field.rs:15:4 + | +LL | fn some_fn(wrapped: MyWrapper) { + | ^^^^^^^ ------------------ +help: consider removing the tuple struct field `0` + | +LL - some_fn(my_wrapper!(123).0); +LL + some_fn(my_wrapper!(123)); + | + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`.