Skip to content

Commit

Permalink
Avoid treating lowercase letters as # noqa codes (#14229)
Browse files Browse the repository at this point in the history
## Summary

An oversight from the original implementation.

Closes #14228.
  • Loading branch information
charliermarsh authored Nov 9, 2024
1 parent 71da1d6 commit ce3af27
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
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)
.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,
},
],
},
Expand Down

0 comments on commit ce3af27

Please sign in to comment.