[syntax-errors] return outside function (#17300)

Summary
--

This PR reimplements [return-outside-function
(F706)](https://docs.astral.sh/ruff/rules/return-outside-function/) as a
semantic syntax error.

These changes are very similar to those in
https://github.com/astral-sh/ruff/pull/17298.

Test Plan
--

New linter tests, plus existing F706 tests.
This commit is contained in:
Brent Westbrook 2025-04-11 13:05:54 -04:00 committed by GitHub
parent 4bfdf54d1a
commit da32a83c9f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 82 additions and 29 deletions

View file

@ -1061,14 +1061,15 @@ mod tests {
Ok(())
}
#[test_case(Path::new("yield_scope.py"); "yield_scope")]
fn test_yield_scope(path: &Path) -> Result<()> {
#[test_case(Rule::YieldOutsideFunction, Path::new("yield_scope.py"))]
#[test_case(Rule::ReturnOutsideFunction, Path::new("return_outside_function.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);
let messages = test_contents_syntax_errors(
&SourceKind::Python(std::fs::read_to_string(&path)?),
&path,
&settings::LinterSettings::for_rule(Rule::YieldOutsideFunction),
&settings::LinterSettings::for_rule(rule),
);
insta::with_settings!({filters => vec![(r"\\", "/")]}, {
assert_messages!(snapshot, messages);