Skip to content

Commit 0785793

Browse files
authored
Fix handling of ignore comments within a disable/enable pair (#25845)
Summary -- This PR addresses the part of #25791 (review) that reproduced on main. Both of these cases were mishandled: ```py # ruff:disable[F401, F402] # ruff:ignore[F401] # ruff:enable[F401, F402] # ruff:disable[E501] import os # ruff:ignore[F401] # ruff:enable[E501] ``` [Playground](https://play.ruff.rs/9095ff69-5a67-45f2-bb96-81b9d36a361c) with both `ruff:ignore` comments generating a `RUF103` diagnostic and with a particularly strange error message in the second case: ``` Invalid suppression comment: trailing comments are only support for ruff:ignore suppressions ``` The fix was to factor out the code for handling standalone `ruff:ignore` and `ruff:file-ignore` comments to allow also calling back into it while looking ahead for the the `enable` comment that matches a `disable` comment. Test Plan -- New mdtests based on the examples in Micha's comment
1 parent 7a91fa2 commit 0785793

2 files changed

Lines changed: 144 additions & 63 deletions

File tree

‎crates/ruff_linter/resources/mdtest/suppression/ignore.md‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,65 @@ values = [
220220
```
221221

222222
<!-- fmt:on -->
223+
224+
## `ruff:ignore` comments within a `disable`/`enable` pair
225+
226+
```toml
227+
[lint]
228+
preview = true
229+
select = ["E501", "F401", "RUF10"]
230+
```
231+
232+
An intervening `ruff:ignore` directive shouldn't cause a `disable`/`enable` pair to be reported as
233+
unmatched. Instead, the range suppression should take precedence, and the inner `ruff:ignore` should
234+
be unused, just like a `noqa` comment:
235+
236+
```py
237+
# ruff:disable[F401]
238+
# error: [unused-noqa]
239+
import os # ruff:ignore[F401]
240+
# ruff:enable[F401]
241+
242+
# ruff:disable[F401]
243+
# error: [unused-noqa]
244+
import sys # noqa: F401
245+
# ruff:enable[F401]
246+
```
247+
248+
This applies to own-line comments too:
249+
250+
```py
251+
# ruff:disable[F401]
252+
# error: [unused-noqa]
253+
# ruff:ignore[F401]
254+
import os
255+
# ruff:enable[F401]
256+
```
257+
258+
and cases where the `disable` and `ignore` suppress different codes:
259+
260+
```py
261+
# ruff:disable[E501]
262+
import os # ruff:ignore[F401]
263+
message = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
264+
# ruff:enable[E501]
265+
```
266+
267+
## `file-ignore` comments within a `disable`/`enable` pair
268+
269+
```toml
270+
[lint]
271+
preview = true
272+
select = ["F401", "RUF10"]
273+
```
274+
275+
A `file-ignore` within a range suppression takes precedence and marks the `disable` as unused:
276+
277+
```py
278+
# error: [unused-noqa]
279+
# ruff:disable[F401]
280+
# ruff:file-ignore[F401]
281+
import os
282+
message = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
283+
# ruff:enable[F401]
284+
```

‎crates/ruff_linter/src/suppression.rs‎

Lines changed: 82 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -575,76 +575,15 @@ impl<'a> SuppressionsBuilder<'a> {
575575

576576
'comments: while let Some(suppression) = suppressions.peek() {
577577
indents.clear();
578-
let (before, after) = tokens.split_at(suppression.range.start());
579578

580579
// Standalone suppression comments
581-
582-
if suppression.action == SuppressionAction::Ignore {
583-
if is_ruff_ignore_enabled(self.settings) {
584-
let range = if indentation_at_offset(suppression.range.start(), self.source)
585-
.is_some()
586-
{
587-
// own-line ignore
588-
Self::standalone_comment_range(suppression.range, before, after)
589-
} else {
590-
// trailing ignore
591-
self.trailing_comment_range(suppression.range, before)
592-
};
593-
for code in suppression.codes_as_str(self.source) {
594-
self.valid.push(Suppression {
595-
code: code.into(),
596-
range,
597-
used: false.into(),
598-
comments: SuppressionComments::Single(suppression.clone()),
599-
});
600-
}
601-
} else {
602-
warn_user_once!(
603-
"#ruff:ignore comment found but not active, enable preview mode"
604-
);
605-
}
606-
suppressions.next();
607-
continue;
608-
} else if suppression.action == SuppressionAction::FileIgnore {
609-
if is_ruff_ignore_enabled(self.settings) {
610-
match indentation_at_offset(suppression.range.start(), self.source) {
611-
// Module scope
612-
Some("") => {
613-
let range = TextRange::up_to(self.source.text_len());
614-
for code in suppression.codes_as_str(self.source) {
615-
self.valid.push(Suppression {
616-
code: code.into(),
617-
range,
618-
used: false.into(),
619-
comments: SuppressionComments::Single(suppression.clone()),
620-
});
621-
}
622-
}
623-
// Indented/inside block
624-
Some(_) => {
625-
self.invalid.push(InvalidSuppression {
626-
kind: InvalidSuppressionKind::NotModuleScope,
627-
comment: suppression.clone(),
628-
});
629-
}
630-
// Trailing
631-
None => {
632-
self.invalid.push(InvalidSuppression {
633-
kind: InvalidSuppressionKind::Trailing,
634-
comment: suppression.clone(),
635-
});
636-
}
637-
}
638-
} else {
639-
warn_user_once!(
640-
"#ruff:file-ignore comment found but not active, enable preview mode"
641-
);
642-
}
580+
if self.register_standalone_suppression(suppression, tokens) {
643581
suppressions.next();
644582
continue;
645583
}
646584

647585
// Matched suppression comments
586+
let (before, after) = tokens.split_at(suppression.range.start());
648587

649588
let mut count = 0;
650589
let last_indent = before
@@ -687,6 +626,10 @@ impl<'a> SuppressionsBuilder<'a> {
687626
continue;
688627
};
689628

629+
if self.register_standalone_suppression(&suppression, tokens) {
630+
continue;
631+
}
632+
690633
let Some(indent) =
691634
indentation_at_offset(suppression.range.start(), self.source)
692635
else {
@@ -746,6 +689,82 @@ impl<'a> SuppressionsBuilder<'a> {
746689
}
747690
}
748691

692+
/// Handles a single-comment suppression like `ruff:ignore` or `ruff:file-ignore` and returns
693+
/// `true` if such a comment was found.
694+
fn register_standalone_suppression(
695+
&mut self,
696+
suppression: &SuppressionComment,
697+
tokens: &Tokens,
698+
) -> bool {
699+
match suppression.action {
700+
SuppressionAction::Ignore => {
701+
if is_ruff_ignore_enabled(self.settings) {
702+
let (before, after) = tokens.split_at(suppression.range.start());
703+
let range = if indentation_at_offset(suppression.range.start(), self.source)
704+
.is_some()
705+
{
706+
// own-line ignore
707+
Self::standalone_comment_range(suppression.range, before, after)
708+
} else {
709+
// trailing ignore
710+
self.trailing_comment_range(suppression.range, before)
711+
};
712+
for code in suppression.codes_as_str(self.source) {
713+
self.valid.push(Suppression {
714+
code: code.into(),
715+
range,
716+
used: false.into(),
717+
comments: SuppressionComments::Single(suppression.clone()),
718+
});
719+
}
720+
} else {
721+
warn_user_once!(
722+
"#ruff:ignore comment found but not active, enable preview mode"
723+
);
724+
}
725+
true
726+
}
727+
SuppressionAction::FileIgnore => {
728+
if is_ruff_ignore_enabled(self.settings) {
729+
match indentation_at_offset(suppression.range.start(), self.source) {
730+
// Module scope
731+
Some("") => {
732+
let range = TextRange::up_to(self.source.text_len());
733+
for code in suppression.codes_as_str(self.source) {
734+
self.valid.push(Suppression {
735+
code: code.into(),
736+
range,
737+
used: false.into(),
738+
comments: SuppressionComments::Single(suppression.clone()),
739+
});
740+
}
741+
}
742+
// Indented/inside block
743+
Some(_) => {
744+
self.invalid.push(InvalidSuppression {
745+
kind: InvalidSuppressionKind::NotModuleScope,
746+
comment: suppression.clone(),
747+
});
748+
}
749+
// Trailing
750+
None => {
751+
self.invalid.push(InvalidSuppression {
752+
kind: InvalidSuppressionKind::Trailing,
753+
comment: suppression.clone(),
754+
});
755+
}
756+
}
757+
} else {
758+
warn_user_once!(
759+
"#ruff:file-ignore comment found but not active, enable preview mode"
760+
);
761+
}
762+
true
763+
}
764+
SuppressionAction::Disable | SuppressionAction::Enable => false,
765+
}
766+
}
767+
749768
fn match_comments(&mut self, current_indent: &str, dedent_range: TextRange) {
750769
let mut comment_index = 0;
751770

0 commit comments

Comments
 (0)