ISSUE DESCRIPTION Schrödinger's comment: it is both commented-out code and a comment…
- User
ISSUE DESCRIPTION
Schrödinger's comment: it is both commented-out code and a comment that is not code (ERA001 vs RUF100)
Summary
First, I'm impressed by the recent reduction of ERA001 false positives. I suspect that ERA001 is an exceptionally difficult rule to implement.
This is the only time I've seen this situation.
# ) # ) # noqa: ERA001Found commented-out code (ERA001) [Ln 1, Col 4] Unused
noqadirective (unused:ERA001) (RUF100) [Ln 2, Col 10]preview = false builtins = [] target-version = "py310" line-length = 88 indent-width = 4 [lint] allowed-confusables = [] dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" extend-select = [] extend-fixable = [] external = [] ignore = [] select = [ "F", "E4", "E7", "E9", "ERA", "RUF" ] [format] indent-style = "space" quote-style = "double"Version
ruff 0.15.14 (9ad2da301 2026-05-21)
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
Assistant
Let me understand the issue. When you have code like:
# ) # ) # noqa: ERA001ERA001 flags line 1 as commented-out code (the
)looks like code), but does NOT flag line 2 (because it has a# noqa: ERA001directive). Then RUF100 flags line 2'snoqaas unused because... wait, actually ERA001 didn't flag line 2, so RUF100 thinks the noqa is unused. But the user added the noqa to suppress ERA001 on that line.So the issue is: ERA001 flags line 1 (
# )) but not line 2 (# ) # noqa: ERA001). The user put noqa on line 2 to suppress ERA001, but ERA001 doesn't even flag line 2 (maybe because the noqa comment makes it not look like code, or because of the way the comment is parsed). Then RUF100 says the noqa is unused.Let me look at the ERA001 rule implementation.
Assistant
Let me understand the issue by reproducing it. Let me create a test file and run ruff.
Assistant
I've reproduced the issue. Let me understand the tokenization of line 2.
Assistant
Let me check the comment tokenization more carefully. Let me write a small test inside the ruff crate.
Assistant
There's a
find_trailing_pragma_offsetfunction. Let me look at it.Assistant
Now I understand the issue. Let me verify my understanding by examining how
comment_contains_codehandles each line.Assistant
Confirmed. The brackets-only positive case
^[()\[\]{}\s]+$matches line 1 ()) but not line 2 () # noqa: ERA001) because of the trailing noqa. Andparse_module(") # noqa: ERA001")fails (unmatched paren). So line 2 isn't flagged, making the noqa "unused".The fix: strip the trailing pragma (e.g.
# noqa: ERA001) from the comment before code detection, so the code part)is evaluated. Then ERA001 flags it, the noqa suppresses the diagnostic, and RUF100 sees the noqa as used.Let me implement the fix in
comment_contains_code.Assistant
Now let me check the imports in detection.rs:
Assistant
Good,
find_trailing_pragma_offsetis re-exported fromruff_python_trivia. Now let me implement the fix:Assistant
Now let me modify
comment_contains_codeto strip the trailing pragma:Assistant
Now let me build and test:
Assistant
The fix works. Line 1 is flagged, line 2 is suppressed by the noqa, and RUF100 no longer reports the noqa as unused. Let me test edge cases.
Assistant
Let me verify line 6 (
# x = foo(— the multiline-assignment positive case) is actually detected now, confirming the noqa is "used":Assistant
The multiline-assignment case is now also handled:
# x = foo( # noqa: ERA001is suppressed (no ERA001, no RUF100). Let me run the existing ERA001 test suite to make sure nothing breaks:Assistant
All tests pass. Let me also check the RUF100 noqa tests and the broader resource-based tests for ERA:
Assistant
Let me look at the test that uses this fixture and its expected output:
Assistant
All tests pass with no snapshot changes. Let me test more edge cases including leading whitespace and blanket noqa:
Assistant
Let me run the ruff_python_trivia tests and a broader check to be thorough:
Assistant
Let me check how
find_trailing_pragma_offsetis used elsewhere, to understand its handling of strings:Assistant
So
find_trailing_pragma_offsetis used by the overlong rule and the formatter. Let me check how it's used in overlong — does it find the noqa directive in a comment? Let me look.Assistant
So
find_trailing_pragma_offsetis used in overlong.rs (E501) to strip the trailing pragma. This means ruff's E501 already uses this same function and thus already treats# x = "# noqa"as having a trailing pragma at the# noqainside the string. So this behavior (naive#scanning) is already established and accepted by ruff.Let me check whether the noqa mechanism itself uses this. Actually, the noqa extraction (for suppressions) is a different code path. Let me check how ruff extracts noqa directives from comments — does it use
find_trailing_pragma_offset?Assistant
So the noqa lexer also naively scans
#characters within the comment. Let me verify ruff's behavior with# x = "# noqa"to ensure my use offind_trailing_pragma_offsetis consistent:Assistant
The noqa lexer also naively scans
#characters, so ruff already treats# noqainside a string as a (invalid) directive. However, I want to avoid a potential false-negative regression: with the simple "strip everything first" approach, a comment like# x = "# noqa"(a string literal containing# noqa) would no longer be flagged, becauseparse_module("x = \"")fails. Currently it IS flagged becauseparse_module('x = "# noqa"')succeeds.Let me use a more targeted approach: only strip the trailing pragma for the positive cases (which rely on line endings), while keeping the full content for
parse_module(so string literals containing# noqaare still detected). Let me revise:Assistant
Now let me rewrite the function with the targeted approach (Option B):
Assistant
Let me also handle the line-continuation case (
ends_with('\\')), which has the same inconsistency when a trailing pragma is present:Assistant
All three cases work correctly. Let me verify the continuation file content precisely and run the full test suite:
Assistant
The file has two backslashes. Let me create a proper single-backslash continuation test:
Assistant
The single-backslash continuation case is now properly suppressed. Let me run the full linter test suite and clippy:
Assistant
All tests pass and clippy is clean. Let me run a comprehensive set of edge cases to verify robustness:
Assistant
All behaviors are correct and consistent:
- Lines 1, 2, 4, 5 (actual commented-out code) → ERA001 fires, noqa suppresses it, no RUF100 (noqa is used). ✓
- Lines 6, 7, 8 (real comments, not code) → ERA001 doesn't fire, noqa is genuinely unused → RUF100 reports it. ✓ (this is correct — the noqa really is unused there)
- Line 3 (code, no noqa) → flagged. ✓
The "Schrödinger's comment" inconsistency is resolved: a noqa on a line is "used" iff ERA001 would actually fire on that line. Let me clean up temp files and review the final diff:
Assistant
The diff looks clean. Let me fix a small clarity issue in the comment — the
\\in a Rust line comment is literally two backslashes, but a line continuation is a single backslash:Assistant
Good, the comment now shows a single backslash. Let me rebuild and re-run the tests to be safe.
Assistant
Let me do a final verification with the exact issue scenario (tab characters) and also confirm the formatter tests still pass since they share the pragma helper:
Assistant
The issue is resolved. Let me verify no stray files are left in the repo and do a final review of the complete change:
Assistant
The change is clean and minimal — only the source file is modified, no test files touched, no stray temp files. Let me run one final broad test pass to be thorough, including the formatter (which shares the pragma helper) and the linter integration tests:
Assistant
All tests pass. Let me provide a summary of the work and the checklist.
Summary
The Issue
With code like:
# ) # ) # noqa: ERA001
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.