2021-06-14 10:15:05 +00:00
|
|
|
use crate::{Diagnostic, DiagnosticsContext};
|
2021-06-13 16:51:19 +00:00
|
|
|
|
|
|
|
// Diagnostic: break-outside-of-loop
|
|
|
|
//
|
|
|
|
// This diagnostic is triggered if the `break` keyword is used outside of a loop.
|
2021-06-14 16:32:39 +00:00
|
|
|
pub(crate) fn break_outside_of_loop(
|
2021-06-13 16:51:19 +00:00
|
|
|
ctx: &DiagnosticsContext<'_>,
|
|
|
|
d: &hir::BreakOutsideOfLoop,
|
|
|
|
) -> Diagnostic {
|
2022-09-01 12:30:57 +00:00
|
|
|
let construct = if d.is_break { "break" } else { "continue" };
|
2021-06-13 16:51:19 +00:00
|
|
|
Diagnostic::new(
|
|
|
|
"break-outside-of-loop",
|
2022-09-01 12:30:57 +00:00
|
|
|
format!("{construct} outside of loop"),
|
2021-06-13 16:51:19 +00:00
|
|
|
ctx.sema.diagnostics_display_range(d.expr.clone().map(|it| it.into())).range,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-06-14 10:15:05 +00:00
|
|
|
use crate::tests::check_diagnostics;
|
2021-06-13 16:51:19 +00:00
|
|
|
|
|
|
|
#[test]
|
2022-09-01 12:30:57 +00:00
|
|
|
fn outside_of_loop() {
|
2021-06-13 16:51:19 +00:00
|
|
|
check_diagnostics(
|
|
|
|
r#"
|
2022-09-01 12:30:57 +00:00
|
|
|
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
|
|
|
|
}
|
2022-09-01 12:54:47 +00:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn try_blocks_are_borders() {
|
|
|
|
check_diagnostics(
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
'a: loop {
|
|
|
|
try {
|
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn async_blocks_are_borders() {
|
|
|
|
check_diagnostics(
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
'a: loop {
|
|
|
|
try {
|
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn closures_are_borders() {
|
|
|
|
check_diagnostics(
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
'a: loop {
|
|
|
|
try {
|
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn blocks_pass_through() {
|
|
|
|
check_diagnostics(
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
'a: loop {
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
break 'a;
|
|
|
|
continue;
|
|
|
|
continue 'a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn label_blocks() {
|
|
|
|
check_diagnostics(
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
'a: {
|
|
|
|
break;
|
|
|
|
//^^^^^ error: break outside of loop
|
|
|
|
break 'a;
|
|
|
|
continue;
|
|
|
|
//^^^^^^^^ error: continue outside of loop
|
|
|
|
continue 'a;
|
|
|
|
//^^^^^^^^^^^ error: continue outside of loop
|
|
|
|
}
|
|
|
|
}
|
2021-06-13 16:51:19 +00:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|