Skip to content

Commit 3d50334

Browse files
committed
Add ignore value suggestion in closure body
1 parent dc37ff8 commit 3d50334

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

compiler/rustc_hir_typeck/src/coercion.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1891,17 +1891,18 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
18911891
kind: hir::ExprKind::Closure(&hir::Closure { body, .. }),
18921892
..
18931893
}) = parent
1894-
&& !matches!(fcx.tcx.hir_body(body).value.kind, hir::ExprKind::Block(..))
18951894
{
1896-
fcx.suggest_missing_semicolon(&mut err, expr, expected, true);
1895+
let needs_block =
1896+
!matches!(fcx.tcx.hir_body(body).value.kind, hir::ExprKind::Block(..));
1897+
fcx.suggest_missing_semicolon(&mut err, expr, expected, needs_block, true);
18971898
}
18981899
// Verify that this is a tail expression of a function, otherwise the
18991900
// label pointing out the cause for the type coercion will be wrong
19001901
// as prior return coercions would not be relevant (#57664).
19011902
if let Some(expr) = expression
19021903
&& due_to_block
19031904
{
1904-
fcx.suggest_missing_semicolon(&mut err, expr, expected, false);
1905+
fcx.suggest_missing_semicolon(&mut err, expr, expected, false, false);
19051906
let pointing_at_return_type = fcx.suggest_mismatched_types_on_tail(
19061907
&mut err,
19071908
expr,

compiler/rustc_hir_typeck/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
921921
self,
922922
&cause,
923923
|mut err| {
924-
self.suggest_missing_semicolon(&mut err, expr, e_ty, false);
924+
self.suggest_missing_semicolon(&mut err, expr, e_ty, false, false);
925925
self.suggest_mismatched_types_on_tail(
926926
&mut err, expr, ty, e_ty, target_id,
927927
);

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+13
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
754754
expression: &'tcx hir::Expr<'tcx>,
755755
expected: Ty<'tcx>,
756756
needs_block: bool,
757+
parent_is_closure: bool,
757758
) {
758759
if expected.is_unit() {
759760
// `BlockTailExpression` only relevant if the tail expr would be
@@ -789,6 +790,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
789790
);
790791
}
791792
}
793+
ExprKind::Path(..) | ExprKind::Lit(_) if parent_is_closure => {
794+
err.span_suggestion(
795+
expression.span.shrink_to_lo(),
796+
"consider ignore the value here",
797+
"_ = ",
798+
if in_external_macro(self.tcx.sess, expression.span) {
799+
Applicability::MaybeIncorrect
800+
} else {
801+
Applicability::MachineApplicable
802+
},
803+
);
804+
}
792805
_ => (),
793806
}
794807
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn main() {
2+
b"abc".iter().for_each(|x| x); //~ ERROR: mismatched types
3+
4+
b"abc".iter().for_each(|x| dbg!(x)); //~ ERROR: mismatched types
5+
6+
b"abc".iter().for_each(|x| {
7+
println!("{}", x);
8+
x //~ ERROR: mismatched types
9+
})
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/closure-ty-mismatch-issue-128561.rs:2:32
3+
|
4+
LL | b"abc".iter().for_each(|x| x);
5+
| ^
6+
| |
7+
| expected `()`, found `&u8`
8+
| help: consider ignore the value here: `_ =`
9+
10+
error[E0308]: mismatched types
11+
--> $DIR/closure-ty-mismatch-issue-128561.rs:4:32
12+
|
13+
LL | b"abc".iter().for_each(|x| dbg!(x));
14+
| ^^^^^^^ expected `()`, found `&u8`
15+
|
16+
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
17+
18+
error[E0308]: mismatched types
19+
--> $DIR/closure-ty-mismatch-issue-128561.rs:8:9
20+
|
21+
LL | x
22+
| ^ expected `()`, found `&u8`
23+
24+
error: aborting due to 3 previous errors
25+
26+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)