Skip to content

Commit 4eecc40

Browse files
authored
[semantic-syntax-errors] test for LoadBeforeGlobalDeclaration - ruff linter (#17592)
Hey @ntBre just one easy case to see if I understood the issue #17526 Let me know if is this what you had in mind.
1 parent cf59cee commit 4eecc40

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
x = 10
2+
def test_1():
3+
global x # ok
4+
x += 1
5+
6+
7+
x = 10
8+
def test_2():
9+
x += 1 # error
10+
global x
11+
12+
def test_3():
13+
print(x) # error
14+
global x
15+
x = 5
16+
17+
def test_4():
18+
global x
19+
print(x)
20+
x = 1
21+
22+
x = 0
23+
test_4()

crates/ruff_linter/src/linter.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,10 @@ mod tests {
10641064

10651065
#[test_case(Rule::YieldOutsideFunction, Path::new("yield_scope.py"))]
10661066
#[test_case(Rule::ReturnOutsideFunction, Path::new("return_outside_function.py"))]
1067+
#[test_case(
1068+
Rule::LoadBeforeGlobalDeclaration,
1069+
Path::new("load_before_global_declaration.py")
1070+
)]
10671071
fn test_syntax_errors(rule: Rule, path: &Path) -> Result<()> {
10681072
let snapshot = path.to_string_lossy().to_string();
10691073
let path = Path::new("resources/test/fixtures/syntax_errors").join(path);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
source: crates/ruff_linter/src/linter.rs
3+
---
4+
resources/test/fixtures/syntax_errors/load_before_global_declaration.py:9:5: PLE0118 Name `x` is used prior to global declaration on line 10
5+
|
6+
7 | x = 10
7+
8 | def test_2():
8+
9 | x += 1 # error
9+
| ^ PLE0118
10+
10 | global x
11+
|
12+
13+
resources/test/fixtures/syntax_errors/load_before_global_declaration.py:13:11: PLE0118 Name `x` is used prior to global declaration on line 14
14+
|
15+
12 | def test_3():
16+
13 | print(x) # error
17+
| ^ PLE0118
18+
14 | global x
19+
15 | x = 5
20+
|

0 commit comments

Comments
 (0)