mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-13 13:57:22 +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> |
||
---|---|---|
.. | ||
red_knot | ||
red_knot_ide | ||
red_knot_project | ||
red_knot_python_semantic | ||
red_knot_server | ||
red_knot_test | ||
red_knot_vendored | ||
red_knot_wasm | ||
ruff | ||
ruff_annotate_snippets | ||
ruff_benchmark | ||
ruff_cache | ||
ruff_db | ||
ruff_dev | ||
ruff_diagnostics | ||
ruff_formatter | ||
ruff_graph | ||
ruff_index | ||
ruff_linter | ||
ruff_macros | ||
ruff_notebook | ||
ruff_python_ast | ||
ruff_python_ast_integration_tests | ||
ruff_python_codegen | ||
ruff_python_formatter | ||
ruff_python_index | ||
ruff_python_literal | ||
ruff_python_parser | ||
ruff_python_resolver | ||
ruff_python_semantic | ||
ruff_python_stdlib | ||
ruff_python_trivia | ||
ruff_python_trivia_integration_tests | ||
ruff_server | ||
ruff_source_file | ||
ruff_text_size | ||
ruff_wasm | ||
ruff_workspace |