Skip to content
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
10 changes: 9 additions & 1 deletion crates/ruff_linter/src/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,11 @@ impl<'a> Directive<'a> {
// Extract, e.g., the `401` in `F401`.
let suffix = line[prefix..]
.chars()
.take_while(char::is_ascii_alphanumeric)
.take_while(char::is_ascii_digit)
Copy link
Copy Markdown
Contributor

@InSyncWithFoo InSyncWithFoo Nov 9, 2024

Choose a reason for hiding this comment

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

What about ParsedFileExemption::lex_code()?

There are multiple reimplementations of the # noqa parsing algorithm, including two by me (RyeCharm, #14111) and another by @koxudaxi (Ruff PyCharm Plugin). I would appreciate it if you could keep it consistent.

.count();
if prefix > 0 && suffix > 0 {
// SAFETY: we can use `prefix` and `suffix` to index into `line` because we know that
// all characters in `line` are ASCII, i.e., a single byte.
Some(&line[..prefix + suffix])
} else {
None
Expand Down Expand Up @@ -1209,6 +1211,12 @@ mod tests {
assert_debug_snapshot!(Directive::try_extract(source, TextSize::default()));
}

#[test]
fn noqa_non_code() {
let source = "# noqa: F401 We're ignoring an import";
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
@@ -0,0 +1,19 @@
---
source: crates/ruff_linter/src/noqa.rs
expression: "Directive::try_extract(source, TextSize::default())"
---
Ok(
Some(
Codes(
Codes {
range: 0..12,
codes: [
Code {
code: "F401",
range: 8..12,
},
],
},
),
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ Ok(
range: 0..16,
codes: [
Code {
code: "F401F841",
range: 8..16,
code: "F401",
range: 8..12,
},
Code {
code: "F841",
range: 12..16,
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This seems "fine", it's not ambiguous.

},
],
},
Expand Down