Skip to content

Commit

Permalink
Avoid off-by-one error in stripping noqa following multi-byte char (#…
Browse files Browse the repository at this point in the history
…8979)

Closes #8976.
  • Loading branch information
charliermarsh authored Dec 3, 2023
1 parent 1dda669 commit 17c8817
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
3 changes: 3 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF100_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@
print(a) # noqa: E501, F821 # comment
print(a) # noqa: E501, F821 comment
print(a) # noqa: E501, F821 comment

print(a) # comment with unicode µ # noqa: E501
print(a) # comment with unicode µ # noqa: E501, F821
14 changes: 4 additions & 10 deletions crates/ruff_linter/src/checkers/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
use std::path::Path;

use itertools::Itertools;
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
use ruff_text_size::{Ranged, TextLen, TextRange};

use ruff_diagnostics::{Diagnostic, Edit, Fix};
use ruff_python_trivia::CommentRanges;
use ruff_python_trivia::{CommentRanges, PythonWhitespace};
use ruff_source_file::Locator;

use crate::noqa;
Expand Down Expand Up @@ -200,17 +200,11 @@ fn delete_noqa(range: TextRange, locator: &Locator) -> Edit {

// Compute the leading space.
let prefix = locator.slice(TextRange::new(line_range.start(), range.start()));
let leading_space = prefix
.rfind(|c: char| !c.is_whitespace())
.map_or(prefix.len(), |i| prefix.len() - i - 1);
let leading_space_len = TextSize::try_from(leading_space).unwrap();
let leading_space_len = prefix.text_len() - prefix.trim_whitespace_end().text_len();

// Compute the trailing space.
let suffix = locator.slice(TextRange::new(range.end(), line_range.end()));
let trailing_space = suffix
.find(|c: char| !c.is_whitespace())
.map_or(suffix.len(), |i| i);
let trailing_space_len = TextSize::try_from(trailing_space).unwrap();
let trailing_space_len = suffix.text_len() - suffix.trim_whitespace_start().text_len();

// Ex) `# noqa`
if line_range
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ RUF100_3.py:23:11: RUF100 [*] Unused `noqa` directive (unused: `E501`)
23 |+print(a) # noqa: F821 # comment
24 24 | print(a) # noqa: E501, F821 comment
25 25 | print(a) # noqa: E501, F821 comment
26 26 |

RUF100_3.py:24:11: RUF100 [*] Unused `noqa` directive (unused: `E501`)
|
Expand All @@ -362,13 +363,17 @@ RUF100_3.py:24:11: RUF100 [*] Unused `noqa` directive (unused: `E501`)
24 |-print(a) # noqa: E501, F821 comment
24 |+print(a) # noqa: F821 comment
25 25 | print(a) # noqa: E501, F821 comment
26 26 |
27 27 | print(a) # comment with unicode µ # noqa: E501

RUF100_3.py:25:11: RUF100 [*] Unused `noqa` directive (unused: `E501`)
|
23 | print(a) # noqa: E501, F821 # comment
24 | print(a) # noqa: E501, F821 comment
25 | print(a) # noqa: E501, F821 comment
| ^^^^^^^^^^^^^^^^^^ RUF100
26 |
27 | print(a) # comment with unicode µ # noqa: E501
|
= help: Remove unused `noqa` directive

Expand All @@ -378,5 +383,50 @@ RUF100_3.py:25:11: RUF100 [*] Unused `noqa` directive (unused: `E501`)
24 24 | print(a) # noqa: E501, F821 comment
25 |-print(a) # noqa: E501, F821 comment
25 |+print(a) # noqa: F821 comment
26 26 |
27 27 | print(a) # comment with unicode µ # noqa: E501
28 28 | print(a) # comment with unicode µ # noqa: E501, F821

RUF100_3.py:27:7: F821 Undefined name `a`
|
25 | print(a) # noqa: E501, F821 comment
26 |
27 | print(a) # comment with unicode µ # noqa: E501
| ^ F821
28 | print(a) # comment with unicode µ # noqa: E501, F821
|

RUF100_3.py:27:39: RUF100 [*] Unused `noqa` directive (unused: `E501`)
|
25 | print(a) # noqa: E501, F821 comment
26 |
27 | print(a) # comment with unicode µ # noqa: E501
| ^^^^^^^^^^^^ RUF100
28 | print(a) # comment with unicode µ # noqa: E501, F821
|
= help: Remove unused `noqa` directive

Safe fix
24 24 | print(a) # noqa: E501, F821 comment
25 25 | print(a) # noqa: E501, F821 comment
26 26 |
27 |-print(a) # comment with unicode µ # noqa: E501
27 |+print(a) # comment with unicode µ
28 28 | print(a) # comment with unicode µ # noqa: E501, F821

RUF100_3.py:28:39: RUF100 [*] Unused `noqa` directive (unused: `E501`)
|
27 | print(a) # comment with unicode µ # noqa: E501
28 | print(a) # comment with unicode µ # noqa: E501, F821
| ^^^^^^^^^^^^^^^^^^ RUF100
|
= help: Remove unused `noqa` directive

Safe fix
25 25 | print(a) # noqa: E501, F821 comment
26 26 |
27 27 | print(a) # comment with unicode µ # noqa: E501
28 |-print(a) # comment with unicode µ # noqa: E501, F821
28 |+print(a) # comment with unicode µ # noqa: F821


0 comments on commit 17c8817

Please sign in to comment.