diff --git a/crates/ruff_linter/src/noqa.rs b/crates/ruff_linter/src/noqa.rs index f734448c795e21..0b51ac3008df42 100644 --- a/crates/ruff_linter/src/noqa.rs +++ b/crates/ruff_linter/src/noqa.rs @@ -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]"; diff --git a/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__noqa_non_code.snap b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__noqa_non_code.snap new file mode 100644 index 00000000000000..e5c3ff736563ad --- /dev/null +++ b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__noqa_non_code.snap @@ -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, + }, + ], + }, + ), + ), +) diff --git a/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__noqa_squashed_codes.snap b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__noqa_squashed_codes.snap index 8a6adc1cc7a118..fe65b9cc7ff021 100644 --- a/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__noqa_squashed_codes.snap +++ b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__noqa_squashed_codes.snap @@ -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, }, ], },