diff --git a/crates/ruff_linter/resources/test/fixtures/syntax_errors/load_before_global_declaration.py b/crates/ruff_linter/resources/test/fixtures/syntax_errors/load_before_global_declaration.py new file mode 100644 index 0000000000000..70183b0891cd3 --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/syntax_errors/load_before_global_declaration.py @@ -0,0 +1,23 @@ +x = 10 +def test_1(): + global x # ok + x += 1 + + +x = 10 +def test_2(): + x += 1 # error + global x + +def test_3(): + print(x) # error + global x + x = 5 + +def test_4(): + global x + print(x) + x = 1 + +x = 0 +test_4() \ No newline at end of file diff --git a/crates/ruff_linter/src/linter.rs b/crates/ruff_linter/src/linter.rs index b266a82e54198..5bef0a5685162 100644 --- a/crates/ruff_linter/src/linter.rs +++ b/crates/ruff_linter/src/linter.rs @@ -1063,6 +1063,10 @@ mod tests { #[test_case(Rule::YieldOutsideFunction, Path::new("yield_scope.py"))] #[test_case(Rule::ReturnOutsideFunction, Path::new("return_outside_function.py"))] + #[test_case( + Rule::LoadBeforeGlobalDeclaration, + Path::new("load_before_global_declaration.py") + )] fn test_syntax_errors(rule: Rule, path: &Path) -> Result<()> { let snapshot = path.to_string_lossy().to_string(); let path = Path::new("resources/test/fixtures/syntax_errors").join(path); diff --git a/crates/ruff_linter/src/snapshots/ruff_linter__linter__tests__load_before_global_declaration.py.snap b/crates/ruff_linter/src/snapshots/ruff_linter__linter__tests__load_before_global_declaration.py.snap new file mode 100644 index 0000000000000..a917491f4125a --- /dev/null +++ b/crates/ruff_linter/src/snapshots/ruff_linter__linter__tests__load_before_global_declaration.py.snap @@ -0,0 +1,20 @@ +--- +source: crates/ruff_linter/src/linter.rs +--- +resources/test/fixtures/syntax_errors/load_before_global_declaration.py:9:5: PLE0118 Name `x` is used prior to global declaration on line 10 + | + 7 | x = 10 + 8 | def test_2(): + 9 | x += 1 # error + | ^ PLE0118 +10 | global x + | + +resources/test/fixtures/syntax_errors/load_before_global_declaration.py:13:11: PLE0118 Name `x` is used prior to global declaration on line 14 + | +12 | def test_3(): +13 | print(x) # error + | ^ PLE0118 +14 | global x +15 | x = 5 + |