Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-diagnostics/src/handlers/unreachable_label.rs')
-rw-r--r--crates/ide-diagnostics/src/handlers/unreachable_label.rs91
1 files changed, 91 insertions, 0 deletions
diff --git a/crates/ide-diagnostics/src/handlers/unreachable_label.rs b/crates/ide-diagnostics/src/handlers/unreachable_label.rs
new file mode 100644
index 0000000000..9fedadeae0
--- /dev/null
+++ b/crates/ide-diagnostics/src/handlers/unreachable_label.rs
@@ -0,0 +1,91 @@
+use crate::{Diagnostic, DiagnosticsContext};
+
+// Diagnostic: unreachable-label
+pub(crate) fn unreachable_label(
+ ctx: &DiagnosticsContext<'_>,
+ d: &hir::UnreachableLabel,
+) -> Diagnostic {
+ let name = &d.name;
+ Diagnostic::new(
+ "unreachable-label",
+ format!("use of unreachable label `{}`", name.display(ctx.sema.db)),
+ ctx.sema.diagnostics_display_range(d.node.clone().map(|it| it.into())).range,
+ )
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::tests::check_diagnostics;
+
+ #[test]
+ fn async_blocks_are_borders() {
+ check_diagnostics(
+ r#"
+fn foo() {
+ 'a: loop {
+ async {
+ break 'a;
+ //^^^^^^^^ error: break outside of loop
+ // ^^ error: use of unreachable label `'a`
+ continue 'a;
+ //^^^^^^^^^^^ error: continue outside of loop
+ // ^^ error: use of unreachable label `'a`
+ };
+ }
+}
+"#,
+ );
+ }
+
+ #[test]
+ fn closures_are_borders() {
+ check_diagnostics(
+ r#"
+fn foo() {
+ 'a: loop {
+ || {
+ break 'a;
+ //^^^^^^^^ error: break outside of loop
+ // ^^ error: use of unreachable label `'a`
+ continue 'a;
+ //^^^^^^^^^^^ error: continue outside of loop
+ // ^^ error: use of unreachable label `'a`
+ };
+ }
+}
+"#,
+ );
+ }
+
+ #[test]
+ fn blocks_pass_through() {
+ check_diagnostics(
+ r#"
+fn foo() {
+ 'a: loop {
+ {
+ break 'a;
+ continue 'a;
+ }
+ }
+}
+"#,
+ );
+ }
+
+ #[test]
+ fn try_blocks_pass_through() {
+ check_diagnostics(
+ r#"
+fn foo() {
+ 'a: loop {
+ try {
+ break 'a;
+ continue 'a;
+ };
+ }
+}
+"#,
+ );
+ }
+}