Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ruff] Fix panic in unused # noqa removal with multi-byte space (RUF100) #10682

Merged
merged 4 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF100_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,9 @@ def f():

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
""" # noqa


def f():
# Invalid - nonexistant error code with multibyte character
d = 1 #…noqa: F841, E50
e = 1 #…noqa: E50
15 changes: 11 additions & 4 deletions crates/ruff_linter/src/fix/edits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,17 @@ pub(crate) fn delete_comment(range: TextRange, locator: &Locator) -> Edit {
}
// Ex) `x = 1 # noqa here`
else {
Edit::deletion(
range.start() + "# ".text_len(),
range.end() + trailing_space_len,
)
if suffix.is_empty() {
// if there's no comment after the deleted noqa, delete the entire range
let full_line_end = locator.full_line_end(line_range.end());
Edit::deletion(line_range.start() - leading_space_len, full_line_end)
} else {
// if there is something after the deleted noqa, keep it in a comment
Edit::range_replacement(
"# ".to_string(),
TextRange::new(range.start(), range.end() + trailing_space_len),
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,51 @@ RUF100_0.py:93:92: RUF100 [*] Unused `noqa` directive (unused: `F401`)
95 95 |
96 96 | def f():

RUF100_0.py:107:12: RUF100 [*] Unused `noqa` directive (unknown: `E50`)
|
105 | def f():
106 | # Invalid - nonexistant error code with multibyte character
107 | d = 1 #…noqa: F841, E50
| ^^^^^^^^^^^^^^^^ RUF100
108 | e = 1 #…noqa: E50
|
= help: Remove unused `noqa` directive

ℹ Safe fix
104 104 |
105 105 | def f():
106 106 | # Invalid - nonexistant error code with multibyte character
107 |- d = 1 #…noqa: F841, E50
107 |+ d = 1 # noqa: F841
108 108 | e = 1 #…noqa: E50

RUF100_0.py:108:5: F841 [*] Local variable `e` is assigned to but never used
|
106 | # Invalid - nonexistant error code with multibyte character
107 | d = 1 #…noqa: F841, E50
108 | e = 1 #…noqa: E50
| ^ F841
|
= help: Remove assignment to unused variable `e`

ℹ Unsafe fix
105 105 | def f():
106 106 | # Invalid - nonexistant error code with multibyte character
107 107 | d = 1 #…noqa: F841, E50
108 |- e = 1 #…noqa: E50

RUF100_0.py:108:12: RUF100 [*] Unused `noqa` directive (unknown: `E50`)
|
106 | # Invalid - nonexistant error code with multibyte character
107 | d = 1 #…noqa: F841, E50
108 | e = 1 #…noqa: E50
| ^^^^^^^^^^ RUF100
|
= help: Remove unused `noqa` directive

ℹ Safe fix
105 105 | def f():
106 106 | # Invalid - nonexistant error code with multibyte character
107 107 | d = 1 #…noqa: F841, E50
108 |- e = 1 #…noqa: E50
108 |+ e = 1
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,51 @@ RUF100_0.py:93:92: RUF100 [*] Unused `noqa` directive (unused: `F401`)
95 95 |
96 96 | def f():

RUF100_0.py:107:12: RUF100 [*] Unused `noqa` directive (unknown: `E50`)
|
105 | def f():
106 | # Invalid - nonexistant error code with multibyte character
107 | d = 1 #…noqa: F841, E50
| ^^^^^^^^^^^^^^^^ RUF100
108 | e = 1 #…noqa: E50
|
= help: Remove unused `noqa` directive

ℹ Safe fix
104 104 |
105 105 | def f():
106 106 | # Invalid - nonexistant error code with multibyte character
107 |- d = 1 #…noqa: F841, E50
107 |+ d = 1 # noqa: F841
108 108 | e = 1 #…noqa: E50

RUF100_0.py:108:5: F841 [*] Local variable `e` is assigned to but never used
|
106 | # Invalid - nonexistant error code with multibyte character
107 | d = 1 #…noqa: F841, E50
108 | e = 1 #…noqa: E50
| ^ F841
|
= help: Remove assignment to unused variable `e`

ℹ Unsafe fix
105 105 | def f():
106 106 | # Invalid - nonexistant error code with multibyte character
107 107 | d = 1 #…noqa: F841, E50
108 |- e = 1 #…noqa: E50

RUF100_0.py:108:12: RUF100 [*] Unused `noqa` directive (unknown: `E50`)
|
106 | # Invalid - nonexistant error code with multibyte character
107 | d = 1 #…noqa: F841, E50
108 | e = 1 #…noqa: E50
| ^^^^^^^^^^ RUF100
|
= help: Remove unused `noqa` directive

ℹ Safe fix
105 105 | def f():
106 106 | # Invalid - nonexistant error code with multibyte character
107 107 | d = 1 #…noqa: F841, E50
108 |- e = 1 #…noqa: E50
108 |+ e = 1