Skip to content

Commit ce3af27

Browse files
Avoid treating lowercase letters as # noqa codes (#14229)
## Summary An oversight from the original implementation. Closes #14228.
1 parent 71da1d6 commit ce3af27

3 files changed

Lines changed: 34 additions & 3 deletions

File tree

crates/ruff_linter/src/noqa.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,11 @@ impl<'a> Directive<'a> {
183183
// Extract, e.g., the `401` in `F401`.
184184
let suffix = line[prefix..]
185185
.chars()
186-
.take_while(char::is_ascii_alphanumeric)
186+
.take_while(char::is_ascii_digit)
187187
.count();
188188
if prefix > 0 && suffix > 0 {
189+
// SAFETY: we can use `prefix` and `suffix` to index into `line` because we know that
190+
// all characters in `line` are ASCII, i.e., a single byte.
189191
Some(&line[..prefix + suffix])
190192
} else {
191193
None
@@ -1209,6 +1211,12 @@ mod tests {
12091211
assert_debug_snapshot!(Directive::try_extract(source, TextSize::default()));
12101212
}
12111213

1214+
#[test]
1215+
fn noqa_non_code() {
1216+
let source = "# noqa: F401 We're ignoring an import";
1217+
assert_debug_snapshot!(Directive::try_extract(source, TextSize::default()));
1218+
}
1219+
12121220
#[test]
12131221
fn noqa_invalid_suffix() {
12141222
let source = "# noqa[F401]";
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
source: crates/ruff_linter/src/noqa.rs
3+
expression: "Directive::try_extract(source, TextSize::default())"
4+
---
5+
Ok(
6+
Some(
7+
Codes(
8+
Codes {
9+
range: 0..12,
10+
codes: [
11+
Code {
12+
code: "F401",
13+
range: 8..12,
14+
},
15+
],
16+
},
17+
),
18+
),
19+
)

crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__noqa_squashed_codes.snap

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ Ok(
99
range: 0..16,
1010
codes: [
1111
Code {
12-
code: "F401F841",
13-
range: 8..16,
12+
code: "F401",
13+
range: 8..12,
14+
},
15+
Code {
16+
code: "F841",
17+
range: 12..16,
1418
},
1519
],
1620
},

0 commit comments

Comments
 (0)