Treat form feed as valid whitespace before a semicolon (#19343)

fixes #19310
This commit is contained in:
frank 2025-07-16 15:39:05 -05:00 committed by GitHub
parent b2501b45e0
commit ff94fe7447
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 18 additions and 1 deletions

View file

@ -0,0 +1,5 @@
# This is a regression test for https://github.com/astral-sh/ruff/issues/19310
# there is a (potentially invisible) unicode formfeed character (000C) between "docstring" and the semicolon
"docstring" ; print(
f"{__doc__=}",
)

View file

@ -288,7 +288,7 @@ fn match_docstring_end(body: &[Stmt]) -> Option<TextSize> {
fn match_semicolon(s: &str) -> Option<TextSize> {
for (offset, c) in s.char_indices() {
match c {
' ' | '\t' => continue,
_ if is_python_whitespace(c) => continue,
';' => return Some(TextSize::try_from(offset).unwrap()),
_ => break,
}

View file

@ -801,6 +801,7 @@ mod tests {
#[test_case(Path::new("existing_import.py"))]
#[test_case(Path::new("multiline_docstring.py"))]
#[test_case(Path::new("off.py"))]
#[test_case(Path::new("whitespace.py"))]
fn required_import(path: &Path) -> Result<()> {
let snapshot = format!("required_import_{}", path.to_string_lossy());
let diagnostics = test_path(

View file

@ -0,0 +1,11 @@
---
source: crates/ruff_linter/src/rules/isort/mod.rs
---
whitespace.py:1:1: I002 [*] Missing required import: `from __future__ import annotations`
Safe fix
1 1 | # This is a regression test for https://github.com/astral-sh/ruff/issues/19310
2 2 | # there is a (potentially invisible) unicode formfeed character (000C) between "docstring" and the semicolon
3 |-"docstring" ; print(
3 |+"docstring" ; from __future__ import annotations; print(
4 4 | f"{__doc__=}",
5 5 | )