[eradicate] Fix ERA001/RUF100 conflict when noqa is on commented-out code#25414
Conversation
|
Thanks for looking into this. Could you please take a look at our AI Policy and confirm that you've followed it? I get the strong impression from the PR summary that this was written by an LLM. I'm also a bit surprised by this approach. I see that you found existing tests in ruff/crates/ruff_linter/src/rules/eradicate/detection.rs Lines 67 to 72 in 0158390 Looks like this is the PR that added that for dictionaries: #6822. |
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| ERA001 | 2 | 2 | 0 | 0 | 0 |
Linter (preview)
ℹ️ ecosystem check detected linter changes. (+2 -0 violations, +0 -0 fixes in 1 projects; 55 projects unchanged)
zulip/zulip (+2 -0 violations, +0 -0 fixes)
ruff check --no-cache --exit-zero --no-fix --output-format concise --preview --select ALL
+ zproject/prod_settings_template.py:297:1: ERA001 Found commented-out code + zproject/prod_settings_template.py:298:1: ERA001 Found commented-out code
Changes by rule (1 rules affected)
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| ERA001 | 2 | 2 | 0 | 0 | 0 |
|
Thanks for the feedback, yes I followed the AI policy. I confirm the generated code is generated by an LLM, me guiding it. Changed the implementation to match the existing ERA001 pattern instead of using a dedicated strip_trailing_noqa helper. Reran:
|
|
Thank you for looking at the AI policy. However, this is the section I was more curious about than the code itself:
My impression was that the PR summary, especially this list of overly-specific test commands in the test plan, was written by an LLM, which is against our policy. The fact that you've copied this test plan again into your latest comment very strongly suggests that you're violating our AI policy. The code changes look reasonable here, so I'm okay with keeping the PR open, but please refrain from pasting LLM output into comments. The PR summary should also be updated to reflect the current state of the code rather than the first implementation. |
|
Thanks for clarification, Im new to OSS so it made sense in my head to add the test cases which passed. apologies that it looked LLM generated comment. I confirm that I personally wrote the above comment. I updated the implementation as per your feedback of the regex style and test coverage... |
eradicate] Fix ERA001/RUF100 conflict when noqa is on commented-out code
…ted-out code (astral-sh#25414) ## Summary Fixes astral-sh#25386. For commented-out code with a trailing `# noqa: ERA001`, Ruff could report both `ERA001` on one line and `RUF100` (unused `ERA001`) on the next — which is inconsistent. `ERA001` uses `comment_contains_code()` to decide if a comment is code. That function was analyzing the full comment body, including trailing `# noqa: ERA001`. The allowlist treats `noqa` as non-code, so lines like `# ) # noqa: ERA001` were skipped by `ERA001` while a nearby line without `noqa` still triggered `ERA001`. That made the `noqa` look unused to `RUF100`. The fix strips a trailing inline `# noqa...` before ERA001’s code detection, so the comment is evaluated on its code portion only (e.g. `)`). Suppression and detection then agree: `noqa: ERA001` is used when it suppresses a real `ERA001` match. the repro now shows ERA001 diagnostics without incorrectly flagging the paired noqa: ERA001 as unused. ## Test Plan - Added unit tests in `detection.rs` for `# ) # noqa: ERA001` and `# \t( # noqa: ERA001` - Extended `RUF100_5.py` with regression cases from astral-sh#25386 - Ran `cargo test -p ruff_linter ruf100_5` (snapshot updated) - Ran `cargo test -p ruff_linter comment_contains_code_with_multiline` - Ran `uvx prek run --files` on the three changed files
Summary
Fixes #25386.
For commented-out code with a trailing
# noqa: ERA001, Ruff could report bothERA001on one line andRUF100(unusedERA001) on the next — which is inconsistent.ERA001usescomment_contains_code()to decide if a comment is code. That functionwas analyzing the full comment body, including trailing
# noqa: ERA001. The allowlisttreats
noqaas non-code, so lines like# ) # noqa: ERA001were skipped byERA001while a nearby line without
noqastill triggeredERA001. That made thenoqalook unused to
RUF100.The fix strips a trailing inline
# noqa...before ERA001’s code detection, so thecomment is evaluated on its code portion only (e.g.
)). Suppression and detectionthen agree:
noqa: ERA001is used when it suppresses a realERA001match.the repro now shows ERA001 diagnostics without incorrectly flagging the paired noqa: ERA001 as unused.
Test Plan
detection.rsfor# ) # noqa: ERA001and# \t( # noqa: ERA001RUF100_5.pywith regression cases from Schrödinger's comment: it is both commented-out code and a comment that is not code (ERA001 vs RUF100) #25386cargo test -p ruff_linter ruf100_5(snapshot updated)cargo test -p ruff_linter comment_contains_code_with_multilineuvx prek run --fileson the three changed files