Skip to content

Commit 383676e

Browse files
committed
Avoid parsing joint rule codes as distinct codes in # noqa
1 parent feba503 commit 383676e

3 files changed

Lines changed: 57 additions & 3 deletions

File tree

crates/ruff_linter/src/noqa.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ 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_digit)
186+
.take_while(char::is_ascii_alphanumeric)
187187
.count();
188188
if prefix > 0 && suffix > 0 {
189189
Some(&line[..prefix + suffix])
@@ -550,7 +550,7 @@ impl<'a> ParsedFileExemption<'a> {
550550
// Extract, e.g., the `401` in `F401`.
551551
let suffix = line[prefix..]
552552
.chars()
553-
.take_while(char::is_ascii_digit)
553+
.take_while(char::is_ascii_alphanumeric)
554554
.count();
555555
if prefix > 0 && suffix > 0 {
556556
Some(&line[..prefix + suffix])
@@ -897,7 +897,7 @@ pub(crate) struct NoqaDirectiveLine<'a> {
897897
pub(crate) directive: Directive<'a>,
898898
/// The codes that are ignored by the directive.
899899
pub(crate) matches: Vec<NoqaCode>,
900-
// Whether the directive applies to range.end
900+
/// Whether the directive applies to `range.end`.
901901
pub(crate) includes_end: bool,
902902
}
903903

@@ -1193,6 +1193,18 @@ mod tests {
11931193
assert_debug_snapshot!(Directive::try_extract(source, TextSize::default()));
11941194
}
11951195

1196+
#[test]
1197+
fn noqa_squashed_codes() {
1198+
let source = "# noqa: F401F841";
1199+
assert_debug_snapshot!(Directive::try_extract(source, TextSize::default()));
1200+
}
1201+
1202+
#[test]
1203+
fn noqa_empty_comma() {
1204+
let source = "# noqa: F401,,F841";
1205+
assert_debug_snapshot!(Directive::try_extract(source, TextSize::default()));
1206+
}
1207+
11961208
#[test]
11971209
fn noqa_invalid_suffix() {
11981210
let source = "# noqa[F401]";
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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..18,
10+
codes: [
11+
Code {
12+
code: "F401",
13+
range: 8..12,
14+
},
15+
Code {
16+
code: "F841",
17+
range: 14..18,
18+
},
19+
],
20+
},
21+
),
22+
),
23+
)
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..16,
10+
codes: [
11+
Code {
12+
code: "F401F841",
13+
range: 8..16,
14+
},
15+
],
16+
},
17+
),
18+
),
19+
)

0 commit comments

Comments
 (0)