Skip to content

Commit 8bd140c

Browse files
dylwil3MichaReiser
authored andcommitted
Make noqa parsing consistent and more robust (#16483)
# Summary The goal of this PR is to address various issues around parsing suppression comments by 1. Unifying the logic used to parse in-line (`# noqa`) and file-level (`# ruff: noqa`) noqa comments 2. Recovering from certain errors and surfacing warnings in these cases Closes #15682 Supersedes #12811 Addresses #14229 (comment) Related: #14229 , #12809
1 parent a04347b commit 8bd140c

File tree

57 files changed

+1538
-820
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1538
-820
lines changed

crates/ruff_linter/resources/test/fixtures/ruff/noqa.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ def f():
1919

2020

2121
def f():
22-
# Only `E741` should be ignored by the `noqa`.
22+
# Neither of these are ignored and warning is
23+
# logged to user
2324
I = 1 # noqa: E741.F841

crates/ruff_linter/src/checkers/ast/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,14 @@ impl<'a> Checker<'a> {
293293
if !self.noqa.is_enabled() {
294294
return false;
295295
}
296-
noqa::rule_is_ignored(code, offset, self.noqa_line_for, self.locator)
296+
297+
noqa::rule_is_ignored(
298+
code,
299+
offset,
300+
self.noqa_line_for,
301+
self.comment_ranges(),
302+
self.locator,
303+
)
297304
}
298305

299306
/// Create a [`Generator`] to generate source code based on the current AST state.

crates/ruff_linter/src/checkers/noqa.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ pub(crate) fn check_noqa(
3838
let exemption = FileExemption::from(&file_noqa_directives);
3939

4040
// Extract all `noqa` directives.
41-
let mut noqa_directives = NoqaDirectives::from_commented_ranges(comment_ranges, path, locator);
41+
let mut noqa_directives =
42+
NoqaDirectives::from_commented_ranges(comment_ranges, &settings.external, path, locator);
4243

4344
// Indices of diagnostics that were ignored by a `noqa` directive.
4445
let mut ignored_diagnostics = vec![];

0 commit comments

Comments
 (0)