ide_diagnostics/handlers/
unreachable_label.rs

1use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
2
3// Diagnostic: unreachable-label
4pub(crate) fn unreachable_label(
5    ctx: &DiagnosticsContext<'_>,
6    d: &hir::UnreachableLabel,
7) -> Diagnostic {
8    let name = &d.name;
9    Diagnostic::new_with_syntax_node_ptr(
10        ctx,
11        DiagnosticCode::RustcHardError("E0767"),
12        format!("use of unreachable label `{}`", name.display(ctx.sema.db, ctx.edition)),
13        d.node.map(|it| it.into()),
14    )
15    .stable()
16}
17
18#[cfg(test)]
19mod tests {
20    use crate::tests::check_diagnostics;
21
22    #[test]
23    fn async_blocks_are_borders() {
24        check_diagnostics(
25            r#"
26fn foo() {
27    'a: loop {
28        async {
29            break 'a;
30          //^^^^^^^^ error: break outside of loop
31               // ^^ error: use of unreachable label `'a`
32            continue 'a;
33          //^^^^^^^^^^^ error: continue outside of loop
34                  // ^^ error: use of unreachable label `'a`
35        };
36    }
37}
38"#,
39        );
40    }
41
42    #[test]
43    fn closures_are_borders() {
44        check_diagnostics(
45            r#"
46fn foo() {
47    'a: loop {
48        || {
49            break 'a;
50          //^^^^^^^^ error: break outside of loop
51               // ^^ error: use of unreachable label `'a`
52            continue 'a;
53          //^^^^^^^^^^^ error: continue outside of loop
54                  // ^^ error: use of unreachable label `'a`
55        };
56    }
57}
58"#,
59        );
60    }
61
62    #[test]
63    fn blocks_pass_through() {
64        check_diagnostics(
65            r#"
66fn foo() {
67    'a: loop {
68        {
69          break 'a;
70          continue 'a;
71        }
72    }
73}
74"#,
75        );
76    }
77
78    #[test]
79    fn try_blocks_pass_through() {
80        check_diagnostics(
81            r#"
82fn foo() {
83    'a: loop {
84        try {
85            break 'a;
86            continue 'a;
87        };
88    }
89}
90"#,
91        );
92    }
93}