mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
![]() <!-- Thank you for contributing to Ruff! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? - Does this pull request include references to any relevant issues? --> Closes #17084 ## Summary This PR adds a new rule (RUF102) to detect and fix invalid rule codes in `noqa` comments. Invalid rule codes in `noqa` directives serve no purpose and may indicate outdated code suppressions. This extends the previous behaviour originating from `crates/ruff_linter/src/noqa.rs` which would only emit a warnigs. With this rule a `--fix` is available. The rule: 1. Analyzes all `noqa` directives to identify invalid rule codes 2. Provides autofix functionality to: - Remove the entire comment if all codes are invalid - Remove only the invalid codes when mixed with valid codes 3. Preserves original comment formatting and whitespace where possible Example cases: - `# noqa: XYZ111` → Remove entire comment (keep empty line) - `# noqa: XYZ222, XYZ333` → Remove entire comment (keep empty line) - `# noqa: F401, INVALID123` → Keep only valid codes (`# noqa: F401`) ## Test Plan - Added tests in `crates/ruff_linter/resources/test/fixtures/ruff/RUF102.py` covering different example cases. <!-- How was it tested? --> ## Notes - This does not handle cases where parsing fails. E.g. `# noqa: NON_EXISTENT, ANOTHER_INVALID` causes a `LexicalError` and the diagnostic is not propagated and we cannot handle the diagnostic. I am also unsure what proper `fix` handling would be and making the user aware we don't understand the codes is probably the best bet. - The rule is added to the Preview rule group as it's a new addition ## Questions - Should we remove the warnings, now that we have a rule? - Is the current fix behavior appropriate for all cases, particularly the handling of whitespace and line deletions? - I'm new to the codebase; let me know if there are rule utilities which could have used but didn't. --------- Co-authored-by: Micha Reiser <micha@reiser.io> |
||
---|---|---|
.. | ||
fixtures | ||
package | ||
project | ||
__init__.py | ||
disallowed_rule_names.txt |