Skip to content

Commit 9b9b891

Browse files
committed
Add ignore value suggestion in closure body
1 parent 2ae9916 commit 9b9b891

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,17 +1897,18 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
18971897
kind: hir::ExprKind::Closure(&hir::Closure { body, .. }),
18981898
..
18991899
}) = parent
1900-
&& !matches!(fcx.tcx.hir().body(body).value.kind, hir::ExprKind::Block(..))
19011900
{
1902-
fcx.suggest_missing_semicolon(&mut err, expr, expected, true);
1901+
let needs_block =
1902+
!matches!(fcx.tcx.hir().body(body).value.kind, hir::ExprKind::Block(..));
1903+
fcx.suggest_missing_semicolon(&mut err, expr, expected, needs_block, true);
19031904
}
19041905
// Verify that this is a tail expression of a function, otherwise the
19051906
// label pointing out the cause for the type coercion will be wrong
19061907
// as prior return coercions would not be relevant (#57664).
19071908
if let Some(expr) = expression
19081909
&& due_to_block
19091910
{
1910-
fcx.suggest_missing_semicolon(&mut err, expr, expected, false);
1911+
fcx.suggest_missing_semicolon(&mut err, expr, expected, false, false);
19111912
let pointing_at_return_type = fcx.suggest_mismatched_types_on_tail(
19121913
&mut err,
19131914
expr,

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
919919
self,
920920
&cause,
921921
|mut err| {
922-
self.suggest_missing_semicolon(&mut err, expr, e_ty, false);
922+
self.suggest_missing_semicolon(&mut err, expr, e_ty, false, false);
923923
self.suggest_mismatched_types_on_tail(
924924
&mut err, expr, ty, e_ty, target_id,
925925
);

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
752752
expression: &'tcx hir::Expr<'tcx>,
753753
expected: Ty<'tcx>,
754754
needs_block: bool,
755+
parent_is_closure: bool,
755756
) {
756757
if expected.is_unit() {
757758
// `BlockTailExpression` only relevant if the tail expr would be
@@ -787,6 +788,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
787788
);
788789
}
789790
}
791+
ExprKind::Path(..) | ExprKind::Lit(_) if parent_is_closure => {
792+
err.span_suggestion(
793+
expression.span.shrink_to_lo(),
794+
"consider ignore the value here",
795+
"_ = ",
796+
if in_external_macro(self.tcx.sess, expression.span) {
797+
Applicability::MaybeIncorrect
798+
} else {
799+
Applicability::MachineApplicable
800+
},
801+
);
802+
}
790803
_ => (),
791804
}
792805
}
Lines changed: 10 additions & 0 deletions
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+
}
Lines changed: 26 additions & 0 deletions
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)