diff options
| author | Yiming Lei <yiming.lei@futurewei.com> | 2022-08-09 11:27:04 -0700 |
|---|---|---|
| committer | Yiming Lei <yiming.lei@futurewei.com> | 2022-08-15 13:31:14 -0700 |
| commit | 0471e2780fe528dae8b67ea3448e8ab641e8510e (patch) | |
| tree | f466caa13f7b78fc8728e91e03d1538f7b4466da /compiler | |
| parent | 6d3f1beae1720055e5a30f4dbe7a9e7fb810c65e (diff) | |
| download | rust-0471e2780fe528dae8b67ea3448e8ab641e8510e.tar.gz rust-0471e2780fe528dae8b67ea3448e8ab641e8510e.zip | |
when there are 3 or more return statements in the loop
emit the first 3 errors and duplicated diagnostic information using take of iterator for the first third return modified: compiler/rustc_typeck/src/check/coercion.rs new file: src/test/ui/typeck/issue-100285.rs new file: src/test/ui/typeck/issue-100285.stderr
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_typeck/src/check/coercion.rs | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/compiler/rustc_typeck/src/check/coercion.rs b/compiler/rustc_typeck/src/check/coercion.rs index 540a8c3a83d..cc400e1ed75 100644 --- a/compiler/rustc_typeck/src/check/coercion.rs +++ b/compiler/rustc_typeck/src/check/coercion.rs @@ -1590,7 +1590,9 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { let hir::ExprKind::Loop(_, _, _, loop_span) = expr.kind else { return;}; let mut span: MultiSpan = vec![loop_span].into(); span.push_span_label(loop_span, "this might have zero elements to iterate on"); - for ret_expr in ret_exprs { + const MAXITER: usize = 3; + let iter = ret_exprs.iter().take(MAXITER); + for ret_expr in iter { span.push_span_label( ret_expr.span, "if the loop doesn't execute, this value would never get returned", @@ -1600,6 +1602,12 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { span, "the function expects a value to always be returned, but loops might run zero times", ); + if MAXITER < ret_exprs.len() { + err.note(&format!( + "if the loop doesn't execute, {} other values would never get returned", + ret_exprs.len() - MAXITER + )); + } err.help( "return a value for the case when the loop has zero elements to iterate on, or \ consider changing the return type to account for that possibility", |
