Fix RUF100 to detect unused file-level noqa directives with specific codes (#17042)#17061
Fix RUF100 to detect unused file-level noqa directives with specific codes (#17042)#17061dylwil3 merged 10 commits intoastral-sh:mainfrom
Conversation
by implementing a check on them. There is different handling necessary as the matches array is prepopulated for file level directives.
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| RUF100 | 17 | 17 | 0 | 0 | 0 |
and satifsy clippy
|
I'll leave this to @dylwil3 to review. What's important is that we gate these changes behind preview because it is a "significant" increase of a stable rule's scope. |
|
I'm curious how this PR handles the case with |
|
@maxmynter could you add some test cases along the lines of what @ddeepwell mentioned? and also gate the change behind preview when you get a chance? I'm gonna try to check out some of the ecosystem reports to see if we're getting any false positives before diving into the implementation Thank you for your help with this! |
|
Thanks for flagging @ddeepwell, Edit: This is not the case. I had misremembered rule F841 which does not apply at the file root; deletion in the example that prompted this comment was wanted. The proposed changes handle (and test) the cases when one code is unused and the other one is used. |
|
@dylwil3 ping :) No worries if you don't have the bandwidth at the moment; i just wanted to let you know that I'm unsure about the next steps here. |
|
Thanks for the ping! Hoping to get to this tomorrow morning - sorry for the delay! |
dylwil3
left a comment
There was a problem hiding this comment.
This looks good, thank you! The gating behind preview looks correct. I'm not sure why it's not reflected in the ecosystem check... I can't reproduce it locally. I'll try to investigate it.
In the meantime, I've just left one more issue to address about preserving the prefix used by the user for file-level directives.
dylwil3
left a comment
There was a problem hiding this comment.
Looks good! One small change and then looks like there are some merge conflicts to resolve as well
|
Merge conflicts were with test cases that i added in #17138. They're resolved now. |
* main: (42 commits) [playground] New default program (#17277) [red-knot] Add `--python-platform` CLI option (#17284) [red-knot] Allow ellipsis default params in stub functions (#17243) [red-knot] Fix stale syntax errors in playground (#17280) Update Rust crate clap to v4.5.35 (#17273) Fix RUF100 to detect unused file-level noqa directives with specific codes (#17042) (#17061) [ci] Fix pattern for code changes (#17275) [`airflow`] Update oudated `AIR301`, `AIR302` rules (#17123) [docs] fix formatting of "See Style Guide" link (#17272) [red-knot] Support stub packages (#17204) ruff_annotate_snippets: address unused code warnings [red-knot] Add a couple more tests for `*` imports (#17270) [red-knot] Add 'Format document' to playground (#17217) Update actions/setup-node action to v4.3.0 (#17259) Update actions/upload-artifact action to v4.6.2 (#17261) Update actions/download-artifact action to v4.2.1 (#17258) Update actions/setup-python action to v5.5.0 (#17260) Update actions/cache action to v4.2.3 (#17256) Update Swatinem/rust-cache action to v2.7.8 (#17255) Update actions/checkout action to v4.2.2 (#17257) ...
* main: (222 commits) [playground] New default program (#17277) [red-knot] Add `--python-platform` CLI option (#17284) [red-knot] Allow ellipsis default params in stub functions (#17243) [red-knot] Fix stale syntax errors in playground (#17280) Update Rust crate clap to v4.5.35 (#17273) Fix RUF100 to detect unused file-level noqa directives with specific codes (#17042) (#17061) [ci] Fix pattern for code changes (#17275) [`airflow`] Update oudated `AIR301`, `AIR302` rules (#17123) [docs] fix formatting of "See Style Guide" link (#17272) [red-knot] Support stub packages (#17204) ruff_annotate_snippets: address unused code warnings [red-knot] Add a couple more tests for `*` imports (#17270) [red-knot] Add 'Format document' to playground (#17217) Update actions/setup-node action to v4.3.0 (#17259) Update actions/upload-artifact action to v4.6.2 (#17261) Update actions/download-artifact action to v4.2.1 (#17258) Update actions/setup-python action to v5.5.0 (#17260) Update actions/cache action to v4.2.3 (#17256) Update Swatinem/rust-cache action to v2.7.8 (#17255) Update actions/checkout action to v4.2.2 (#17257) ...
Closes #17042
Summary
This PR fixes the issue outlined in #17042 where RUF100 (unused-noqa) fails to detect unused file-level noqa directives (
# ruff: noqaor# ruff: noqa: {code}).The issue stems from two underlying causes:
For blanket file-level directives (
# ruff: noqa), there's a circular dependency: the directive exempts all rules including RUF100 itself, which prevents checking for usage. This isn't changed by this PR. I would argue it is intendend behavior - a blanket# ruff: noqadirective should exempt all rules including RUF100 itself.For code-specific file-level directives (e.g.
# ruff: noqa: F841), the handling was missing in thecheck_noqafunction. This is added in this PR.Notes
For file-level directives, the
matchesarray is pre-populated with the specified codes during parsing, unlike line-level directives which only populate theirmatchesarray when actually suppressing diagnostics. This difference requires the somewhat clunky handling of both cases. I would appreciate guidance on a cleaner design :)A more fundamental solution would be to change how file-level directives initialize the
matchesarray inFileNoqaDirectives::extract(), but that requires more substantial changes as it breaks existing functionality. I suspect discussions in Make noqa parsing consistent and more robust #16483 are relevant for this.Test Plan