summaryrefslogtreecommitdiffstats
path: root/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/break_outside_of_loop.rs
blob: 0c92e706b3916f65c73df4b6e3945b4e6501a052 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use crate::{Diagnostic, DiagnosticsContext};

// Diagnostic: break-outside-of-loop
//
// This diagnostic is triggered if the `break` keyword is used outside of a loop.
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",
        format!("{construct} outside of loop"),
        ctx.sema.diagnostics_display_range(d.expr.clone().map(|it| it.into())).range,
    )
}

#[cfg(test)]
mod tests {
    use crate::tests::check_diagnostics;

    #[test]
    fn outside_of_loop() {
        check_diagnostics(
            r#"
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
}
"#,
        );
    }

    #[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
    }
}
"#,
        );
    }
}