Diagnose incorrect continue expressions

This commit is contained in:
Lukas Wirth 2022-09-01 14:30:57 +02:00
parent d6fc4a9ea6
commit 1e66a5a8ce
5 changed files with 48 additions and 31 deletions

View file

@ -7,9 +7,10 @@ pub(crate) fn break_outside_of_loop(
ctx: &DiagnosticsContext<'_>,
d: &hir::BreakOutsideOfLoop,
) -> Diagnostic {
let construct = if d.is_break { "break" } else { "continue" };
Diagnostic::new(
"break-outside-of-loop",
"break outside of loop",
format!("{construct} outside of loop"),
ctx.sema.diagnostics_display_range(d.expr.clone().map(|it| it.into())).range,
)
}
@ -19,11 +20,19 @@ mod tests {
use crate::tests::check_diagnostics;
#[test]
fn break_outside_of_loop() {
fn outside_of_loop() {
check_diagnostics(
r#"
fn foo() { break; }
//^^^^^ error: break outside of loop
fn foo() {
break;
//^^^^^ error: break outside of loop
break 'a;
//^^^^^^^^ error: break outside of loop
continue;
//^^^^^^^^ error: continue outside of loop
continue 'a;
//^^^^^^^^^^^ error: continue outside of loop
}
"#,
);
}