mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-29 19:17:20 +00:00
[ty] continue and break statements outside loops are syntax errors (#20944)
Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
This commit is contained in:
parent
c424007645
commit
c7e2bfd759
3 changed files with 130 additions and 1 deletions
|
|
@ -354,3 +354,25 @@ def f():
|
||||||
x = 1
|
x = 1
|
||||||
global x # error: [invalid-syntax] "name `x` is used prior to global declaration"
|
global x # error: [invalid-syntax] "name `x` is used prior to global declaration"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## `break` and `continue` outside a loop
|
||||||
|
|
||||||
|
<!-- snapshot-diagnostics -->
|
||||||
|
|
||||||
|
```py
|
||||||
|
break # error: [invalid-syntax]
|
||||||
|
continue # error: [invalid-syntax]
|
||||||
|
|
||||||
|
for x in range(42):
|
||||||
|
break # fine
|
||||||
|
continue # fine
|
||||||
|
|
||||||
|
def _():
|
||||||
|
break # error: [invalid-syntax]
|
||||||
|
continue # error: [invalid-syntax]
|
||||||
|
|
||||||
|
class Fine:
|
||||||
|
# this is invalid syntax despite it being in an eager-nested scope!
|
||||||
|
break # error: [invalid-syntax]
|
||||||
|
continue # error: [invalid-syntax]
|
||||||
|
```
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,107 @@
|
||||||
|
---
|
||||||
|
source: crates/ty_test/src/lib.rs
|
||||||
|
expression: snapshot
|
||||||
|
---
|
||||||
|
---
|
||||||
|
mdtest name: semantic_syntax_errors.md - Semantic syntax error diagnostics - `break` and `continue` outside a loop
|
||||||
|
mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/semantic_syntax_errors.md
|
||||||
|
---
|
||||||
|
|
||||||
|
# Python source files
|
||||||
|
|
||||||
|
## mdtest_snippet.py
|
||||||
|
|
||||||
|
```
|
||||||
|
1 | break # error: [invalid-syntax]
|
||||||
|
2 | continue # error: [invalid-syntax]
|
||||||
|
3 |
|
||||||
|
4 | for x in range(42):
|
||||||
|
5 | break # fine
|
||||||
|
6 | continue # fine
|
||||||
|
7 |
|
||||||
|
8 | def _():
|
||||||
|
9 | break # error: [invalid-syntax]
|
||||||
|
10 | continue # error: [invalid-syntax]
|
||||||
|
11 |
|
||||||
|
12 | class Fine:
|
||||||
|
13 | # this is invalid syntax despite it being in an eager-nested scope!
|
||||||
|
14 | break # error: [invalid-syntax]
|
||||||
|
15 | continue # error: [invalid-syntax]
|
||||||
|
```
|
||||||
|
|
||||||
|
# Diagnostics
|
||||||
|
|
||||||
|
```
|
||||||
|
error[invalid-syntax]: `break` outside loop
|
||||||
|
--> src/mdtest_snippet.py:1:1
|
||||||
|
|
|
||||||
|
1 | break # error: [invalid-syntax]
|
||||||
|
| ^^^^^
|
||||||
|
2 | continue # error: [invalid-syntax]
|
||||||
|
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
error[invalid-syntax]: `continue` outside loop
|
||||||
|
--> src/mdtest_snippet.py:2:1
|
||||||
|
|
|
||||||
|
1 | break # error: [invalid-syntax]
|
||||||
|
2 | continue # error: [invalid-syntax]
|
||||||
|
| ^^^^^^^^
|
||||||
|
3 |
|
||||||
|
4 | for x in range(42):
|
||||||
|
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
error[invalid-syntax]: `break` outside loop
|
||||||
|
--> src/mdtest_snippet.py:9:9
|
||||||
|
|
|
||||||
|
8 | def _():
|
||||||
|
9 | break # error: [invalid-syntax]
|
||||||
|
| ^^^^^
|
||||||
|
10 | continue # error: [invalid-syntax]
|
||||||
|
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
error[invalid-syntax]: `continue` outside loop
|
||||||
|
--> src/mdtest_snippet.py:10:9
|
||||||
|
|
|
||||||
|
8 | def _():
|
||||||
|
9 | break # error: [invalid-syntax]
|
||||||
|
10 | continue # error: [invalid-syntax]
|
||||||
|
| ^^^^^^^^
|
||||||
|
11 |
|
||||||
|
12 | class Fine:
|
||||||
|
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
error[invalid-syntax]: `break` outside loop
|
||||||
|
--> src/mdtest_snippet.py:14:9
|
||||||
|
|
|
||||||
|
12 | class Fine:
|
||||||
|
13 | # this is invalid syntax despite it being in an eager-nested scope!
|
||||||
|
14 | break # error: [invalid-syntax]
|
||||||
|
| ^^^^^
|
||||||
|
15 | continue # error: [invalid-syntax]
|
||||||
|
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
error[invalid-syntax]: `continue` outside loop
|
||||||
|
--> src/mdtest_snippet.py:15:9
|
||||||
|
|
|
||||||
|
13 | # this is invalid syntax despite it being in an eager-nested scope!
|
||||||
|
14 | break # error: [invalid-syntax]
|
||||||
|
15 | continue # error: [invalid-syntax]
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
@ -2787,7 +2787,7 @@ impl SemanticSyntaxContext for SemanticIndexBuilder<'_, '_> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn in_loop_context(&self) -> bool {
|
fn in_loop_context(&self) -> bool {
|
||||||
true
|
self.current_scope_info().current_loop.is_some()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue