Avoid treating lowercase letters as `# noqa` codes (#14229)

## Summary

An oversight from the original implementation.

Closes https://github.com/astral-sh/ruff/issues/14228.
This commit is contained in:
Charlie Marsh 2024-11-09 12:49:35 -05:00 committed by GitHub
parent 71da1d6df5
commit ce3af27f59
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 34 additions and 3 deletions

View File

@ -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
@ -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]";

View File

@ -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,
},
],
},
),
),
)

View File

@ -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,
},
],
},