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

Avoid parsing joint rule codes as distinct codes in # noqa #12809

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,6 +23,9 @@
print(a) # noqa: E501, F821 # comment
print(a) # noqa: E501, F821 comment
print(a) # noqa: E501, F821 comment
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
24 changes: 21 additions & 3 deletions crates/ruff_linter/src/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl<'a> Directive<'a> {
// Extract, e.g., the `401` in `F401`.
let suffix = line[prefix..]
.chars()
.take_while(char::is_ascii_digit)
.take_while(char::is_ascii_alphanumeric)
.count();
if prefix > 0 && suffix > 0 {
Some(&line[..prefix + suffix])
Expand Down Expand Up @@ -549,7 +549,7 @@ impl<'a> ParsedFileExemption<'a> {
// Extract, e.g., the `401` in `F401`.
let suffix = line[prefix..]
.chars()
.take_while(char::is_ascii_digit)
.take_while(char::is_ascii_alphanumeric)
.count();
if prefix > 0 && suffix > 0 {
Some(&line[..prefix + suffix])
Expand Down Expand Up @@ -895,7 +895,7 @@ pub(crate) struct NoqaDirectiveLine<'a> {
pub(crate) directive: Directive<'a>,
/// The codes that are ignored by the directive.
pub(crate) matches: Vec<NoqaCode>,
// Whether the directive applies to range.end
/// Whether the directive applies to `range.end`.
pub(crate) includes_end: bool,
}

Expand Down Expand Up @@ -1191,6 +1191,24 @@ mod tests {
assert_debug_snapshot!(Directive::try_extract(source, TextSize::default()));
}

#[test]
fn noqa_squashed_codes() {
let source = "# noqa: F401F841";
assert_debug_snapshot!(Directive::try_extract(source, TextSize::default()));
}

#[test]
fn noqa_empty_comma() {
let source = "# noqa: F401,,F841";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should show a warning in this case but still parse the remaining codes (if that's possible currently). This would be similar to how we recover from a missing element in list parsing: https://play.ruff.rs/308ab356-9355-43f3-99b7-12c7c2de7334.

assert_debug_snapshot!(Directive::try_extract(source, TextSize::default()));
}

#[test]
fn noqa_empty_comma_space() {
let source = "# noqa: F401, ,F841";
assert_debug_snapshot!(Directive::try_extract(source, TextSize::default()));
}

#[test]
fn noqa_invalid_suffix() {
let source = "# noqa[F401]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +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 |
26 26 | print(a) # noqa: E501,,F821 comment

RUF100_3.py:24:11: RUF100 [*] Unused `noqa` directive (unused: `E501`)
|
Expand All @@ -353,6 +353,7 @@ RUF100_3.py:24:11: RUF100 [*] Unused `noqa` directive (unused: `E501`)
24 | print(a) # noqa: E501, F821 comment
| ^^^^^^^^^^^^^^^^^^ RUF100
25 | print(a) # noqa: E501, F821 comment
26 | print(a) # noqa: E501,,F821 comment
|
= help: Remove unused `noqa` directive

Expand All @@ -363,17 +364,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
26 26 | print(a) # noqa: E501,,F821 comment
27 27 | print(a) # noqa: E501, ,F821 comment

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
26 | print(a) # noqa: E501,,F821 comment
27 | print(a) # noqa: E501, ,F821 comment
|
= help: Remove unused `noqa` directive

Expand All @@ -383,48 +384,110 @@ 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
26 26 | print(a) # noqa: E501,,F821 comment
27 27 | print(a) # noqa: E501, ,F821 comment
28 28 | print(a) # noqa: E501 F821 comment

RUF100_3.py:27:7: F821 Undefined name `a`
RUF100_3.py:26:11: RUF100 [*] Unused `noqa` directive (unused: `E501`)
|
24 | print(a) # noqa: E501, F821 comment
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
26 | print(a) # noqa: E501,,F821 comment
| ^^^^^^^^^^^^^^^^^^ RUF100
27 | print(a) # noqa: E501, ,F821 comment
28 | print(a) # noqa: E501 F821 comment
|
= help: Remove unused `noqa` directive

RUF100_3.py:27:39: RUF100 [*] Unused `noqa` directive (unused: `E501`)
ℹ Safe fix
23 23 | print(a) # noqa: E501, F821 # comment
24 24 | print(a) # noqa: E501, F821 comment
25 25 | print(a) # noqa: E501, F821 comment
26 |-print(a) # noqa: E501,,F821 comment
26 |+print(a) # noqa: F821 comment
27 27 | print(a) # noqa: E501, ,F821 comment
28 28 | print(a) # noqa: E501 F821 comment
29 29 |

RUF100_3.py:27:11: 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
26 | print(a) # noqa: E501,,F821 comment
27 | print(a) # noqa: E501, ,F821 comment
| ^^^^^^^^^^^^^^^^^^^ RUF100
28 | print(a) # noqa: E501 F821 comment
|
= 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
26 26 | print(a) # noqa: E501,,F821 comment
27 |-print(a) # noqa: E501, ,F821 comment
27 |+print(a) # noqa: F821 comment
28 28 | print(a) # noqa: E501 F821 comment
29 29 |
30 30 | print(a) # comment with unicode µ # noqa: E501

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

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

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

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

ℹ Safe fix
27 27 | print(a) # noqa: E501, ,F821 comment
28 28 | print(a) # noqa: E501 F821 comment
29 29 |
30 |-print(a) # comment with unicode µ # noqa: E501
30 |+print(a) # comment with unicode µ
31 31 | print(a) # comment with unicode µ # noqa: E501, F821

RUF100_3.py:31:39: RUF100 [*] Unused `noqa` directive (unused: `E501`)
|
30 | print(a) # comment with unicode µ # noqa: E501
31 | 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
28 28 | print(a) # noqa: E501 F821 comment
29 29 |
30 30 | print(a) # comment with unicode µ # noqa: E501
31 |-print(a) # comment with unicode µ # noqa: E501, F821
31 |+print(a) # comment with unicode µ # noqa: F821
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: crates/ruff_linter/src/noqa.rs
expression: "Directive::try_extract(source, TextSize::default())"
---
Ok(
Some(
Codes(
Codes {
range: 0..18,
codes: [
Code {
code: "F401",
range: 8..12,
},
Code {
code: "F841",
range: 14..18,
},
],
},
),
),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: crates/ruff_linter/src/noqa.rs
expression: "Directive::try_extract(source, TextSize::default())"
---
Ok(
Some(
Codes(
Codes {
range: 0..19,
codes: [
Code {
code: "F401",
range: 8..12,
},
Code {
code: "F841",
range: 15..19,
},
],
},
),
),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
source: crates/ruff_linter/src/noqa.rs
expression: "Directive::try_extract(source, TextSize::default())"
---
Ok(
Some(
Codes(
Codes {
range: 0..16,
codes: [
Code {
code: "F401F841",
range: 8..16,
},
],
},
),
),
)
Loading