Skip to content

Commit 8a54e01

Browse files
committed
Handle external codes and redirects
1 parent ace436a commit 8a54e01

File tree

3 files changed

+16
-48
lines changed

3 files changed

+16
-48
lines changed

crates/ruff_linter/src/rules/ruff/rules/invalid_rule_code.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,17 @@ pub(crate) fn invalid_noqa_code(
8181
continue;
8282
};
8383

84-
let all_valid = directive.iter().all(|code| code_is_valid(code, external));
84+
let all_valid = directive
85+
.iter()
86+
.all(|code| code_is_valid(code.as_str(), external));
8587

8688
if all_valid {
8789
continue;
8890
}
8991

9092
let (valid_codes, invalid_codes): (Vec<_>, Vec<_>) = directive
9193
.iter()
92-
.partition(|&code| code_is_valid(code, external));
94+
.partition(|&code| code_is_valid(code.as_str(), external));
9395

9496
if valid_codes.is_empty() {
9597
all_codes_invalid_diagnostic(directive, invalid_codes, context);
@@ -101,10 +103,9 @@ pub(crate) fn invalid_noqa_code(
101103
}
102104
}
103105

104-
fn code_is_valid(code: &Code, external: &[String]) -> bool {
105-
let code_str = code.as_str();
106-
Rule::from_code(get_redirect_target(code_str).unwrap_or(code_str)).is_ok()
107-
|| external.iter().any(|ext| code_str.starts_with(ext))
106+
pub(crate) fn code_is_valid(code: &str, external: &[String]) -> bool {
107+
Rule::from_code(get_redirect_target(code).unwrap_or(code)).is_ok()
108+
|| external.iter().any(|ext| code.starts_with(ext))
108109
}
109110

110111
fn all_codes_invalid_diagnostic(

crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__range_suppressions.snap

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ source: crates/ruff_linter/src/rules/ruff/mod.rs
77

88
--- Summary ---
99
Removed: 15
10-
Added: 25
10+
Added: 23
1111

1212
--- Removed ---
1313
E741 Ambiguous variable name: `I`
@@ -653,37 +653,3 @@ help: Remove the rule code
653653
97 |
654654
98 |
655655
99 | def f():
656-
657-
658-
RUF102 [*] Invalid rule code in suppression: TK421
659-
--> suppressions.py:102:5
660-
|
661-
100 | def f():
662-
101 | # External rule codes should be ignored
663-
102 | # ruff: disable[TK421]
664-
| ^^^^^^^^^^^^^^^^^^^^^^
665-
103 | print("hello")
666-
104 | # ruff: enable[TK421]
667-
|
668-
help: Remove the rule code
669-
99 |
670-
100 | def f():
671-
101 | # External rule codes should be ignored
672-
- # ruff: disable[TK421]
673-
102 | print("hello")
674-
103 | # ruff: enable[TK421]
675-
676-
677-
RUF102 [*] Invalid rule code in suppression: TK421
678-
--> suppressions.py:104:5
679-
|
680-
102 | # ruff: disable[TK421]
681-
103 | print("hello")
682-
104 | # ruff: enable[TK421]
683-
| ^^^^^^^^^^^^^^^^^^^^^
684-
|
685-
help: Remove the rule code
686-
101 | # External rule codes should be ignored
687-
102 | # ruff: disable[TK421]
688-
103 | print("hello")
689-
- # ruff: enable[TK421]

crates/ruff_linter/src/suppression.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ use crate::checkers::ast::LintContext;
1818
use crate::codes::Rule;
1919
use crate::fix::edits::delete_comment;
2020
use crate::preview::is_range_suppressions_enabled;
21+
use crate::rule_redirects::get_redirect_target;
2122
use crate::rules::ruff::rules::{
2223
InvalidRuleCode, InvalidRuleCodeKind, InvalidSuppressionComment, InvalidSuppressionCommentKind,
23-
UnmatchedSuppressionComment, UnusedCodes, UnusedNOQA, UnusedNOQAKind,
24+
UnmatchedSuppressionComment, UnusedCodes, UnusedNOQA, UnusedNOQAKind, code_is_valid,
2425
};
2526
use crate::settings::LinterSettings;
2627

@@ -154,7 +155,9 @@ impl Suppressions {
154155
};
155156

156157
for suppression in &self.valid {
157-
if *code == suppression.code.as_str() && suppression.range.contains_range(range) {
158+
let suppression_code =
159+
get_redirect_target(suppression.code.as_str()).unwrap_or(suppression.code.as_str());
160+
if *code == suppression_code && suppression.range.contains_range(range) {
158161
suppression.used.set(true);
159162
return true;
160163
}
@@ -220,11 +223,9 @@ impl Suppressions {
220223
}
221224

222225
if context.is_rule_enabled(Rule::InvalidRuleCode) {
223-
for suppression in self
224-
.valid
225-
.iter()
226-
.filter(|suppression| Rule::from_code(&suppression.code).is_err())
227-
{
226+
for suppression in self.valid.iter().filter(|suppression| {
227+
!code_is_valid(&suppression.code, &context.settings().external)
228+
}) {
228229
for comment in &suppression.comments {
229230
let (range, edit) =
230231
Suppressions::delete_code_or_comment(locator, suppression, comment);

0 commit comments

Comments
 (0)