diff options
| author | Yuki Okushi <jtitor@2k36.org> | 2022-07-26 13:12:20 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-26 13:12:20 +0900 |
| commit | d89e99a805838bb03e6f81a461efcdab8e616aee (patch) | |
| tree | 8ee6295ea78064272dc4d9579534fff4c04e8f11 /compiler | |
| parent | 29444545405471a106c67adf48593c5943b6f483 (diff) | |
| parent | f85f37583d91152b967cd0d1bd7253b339a26d42 (diff) | |
| download | rust-d89e99a805838bb03e6f81a461efcdab8e616aee.tar.gz rust-d89e99a805838bb03e6f81a461efcdab8e616aee.zip | |
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
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_typeck/src/check/demand.rs | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/compiler/rustc_typeck/src/check/demand.rs b/compiler/rustc_typeck/src/check/demand.rs index a2d8765289c..381c3a4ea1f 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 .. { .. })`.) |
