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 analyze this issue. The user is reporting a problem with Ruff where there's a conflict between two rules:
- ERA001 - Detects commented-out code
- RUF100 - Reports unused
noqadirectives
The specific case is:
# ) # ) # noqa: ERA001The problem is:
- The code
# )is flagged as ERA001 (commented out code) - Then
# ) # noqa: ERA001tries to suppress ERA001 - But RUF100 says the noqa directive is unused because the ERA001 issue on line 1 is on a different line
This is a tricky edge case. The
# noqa: ERA001comment is on line 2, but the commented-out code it's trying to suppress is on line 1. This creates a conflict where:- Line 1 has commented-out code that should be suppressed by the noqa
- But the noqa is on line 2, not line 1
Let me explore the repository to understand the structure and find the relevant code.
Assistant
Let me search for the ERA001 and RUF100 related code:
Assistant
<tool_call> <function=bash_terminal> <parameter=command> find /ruff/crates -type f -name "*.rs" | xargs grep -l "RUF100" 2>/dev/null | head -10 </tool_call>
Assistant
The command didn't execute because I need to format it properly. Let me try again.
Assistant
Now let me look at the ERA001 rule first to understand how it works:
Assistant
I need to use a different command to open the file. Let me use the proper bash command.
Assistant
Now let me look at the RUF100 (unused_noqa) rule:
Assistant
Now let me look at the noqa checker to understand how noqa directives are processed:
<tool_call> <function=bash_terminal> <parameter=command> cat /ruff/crates/ruff_linter/src/checkers/noqa.rs </tool_call>
Assistant
I need to use bash_terminal with proper syntax again.
Assistant
Let me look at how ERA001 detects commented-out code and how the noqa matching works:
Assistant
Now I understand the issue. Looking at the problem:
# ) # ) # noqa: ERA001Line 1:
# )- This is detected as commented-out code (ERA001) Line 2:# ) # noqa: ERA001- This has anoqa: ERA001directiveThe issue is:
- Line 1 has a commented-out code which triggers ERA001
- Line 2 has a
noqa: ERA001comment, but it's on a DIFFERENT line from line 1 - The noqa directive on line 2 is therefore "unused" because it doesn't suppress the ERA001 on line 1
This is a fundamental design issue. The user is trying to use the noqa directive on line 2 to suppress the ERA001 on line 1, but noqa directives typically work on the same line or subsequent lines, not preceding lines.
Actually, looking at the code more closely, I think the user's intention might be that the ERA001 detection is finding both comments as commented-out code (both lines have
# )), and the noqa on line 2 is intended to suppress ERA001 on both lines.Assistant
The command didn't execute properly. Let me try a different approach with a simpler command.
Assistant
Let me look at how noqa_line_for is built and how it maps positions to noqa directives.
Assistant
Let me search for how the NoqaMapping and noqa_line_for are built:
<tool_call> <function=bash_terminal> <parameter=command> grep -n "struct NoqaMapping" /ruff/crates/ruff_linter/src/noqa.rs </tool_call>
Assistant
Let me try with simpler grep to find the NoqaMapping struct definition.
Assistant
Let me look at the NoqaMapping struct and its resolve method:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '1181,1260p' /ruff/crates/ruff_linter/src/noqa.rs </tool_call>
Assistant
The command didn't execute because I'm not using the correct syntax. Let me use bash_terminal properly.
Assistant
Assistant
The command format is not working. Let me use the correct bash_terminal syntax.
Assistant
Let me look at the extract_noqa_line_for function:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '109,160p' /ruff/crates/ruff_linter/src/directives.rs </tool_call>
Assistant
The command is not being executed properly. Let me check the syntax - I need to use bash_terminal with the command in a structured format.
Assistant
Now I understand the issue better. The is used to map diagnostic positions to the appropriate noqa directive. It maps:
Assistant
The command isn't being executed properly. Let me try again with the proper bash_terminal syntax.
Assistant
Assistant
I'm having trouble with the bash_terminal command syntax. Let me try a simpler grep command to find the function.
Assistant
Let me look at this function.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '1140,1180p' /ruff/crates/ruff_linter/src/noqa.rs </tool_call>
Assistant
The bash_terminal command seems to not be executing properly. Let me try with a different approach.
Assistant
Assistant
Let me use the correct bash_terminal syntax to find the NoqaDirectiveLine struct definition.
Assistant
Assistant
Let me build and test to confirm the issue. First, let me find how to build and run Ruff.
<tool_call> <function=bash_terminal> <parameter=command> cd /ruff && cargo build --release 2>&1 | tail -20 </tool_call>
Assistant
The command didn't execute. Let me try the correct bash_terminal syntax.
Assistant
The build is taking too long. Let me try a different approach - I'll look for existing tests or examples that might help me understand the issue better, and also look for any related fixes in the codebase.
Assistant
Let me look at this test file:
Assistant
Assistant
The command didn't execute properly. Let me try the correct bash_terminal syntax.
Assistant
Looking at the test cases, they only test
# noqaat the beginning of the comment. There's no test for# code # noqa: A123.The issue is that the
ALLOWLIST_REGEXis anchored at^, so it only matchesnoqaat the beginning of the string. But noqa can appear anywhere in the comment.The fix should be to change the regex to match
noqaanywhere in the string, not just at the beginning.Let me look at the regex more carefully:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '18,75p' /ruff/crates/ruff_linter/src/rules/eradicate/detection.rs </tool_call>
Assistant
The bash_terminal command isn't working properly. Let me try a different approach - I'll use cat with sed to extract the relevant lines.
Assistant
Assistant
Let me try the sed command to get the test cases.
Assistant
Assistant
I need to use the bash_terminal tool properly. Let me try a simpler command first.
Assistant
Assistant
The bash_terminal command didn't execute properly. Let me try again with the correct syntax.
Assistant
Assistant
The command didn't execute. Let me try using bash_terminal with a simpler approach - I'll use sed to make the edit directly.
Assistant
Now I can see the exact structure of the regex. I need to modify it to allow
noqato appear anywhere in the string, not just at the beginning. The key changes are:- Move
^inside the non-capturing group (so it only applies to the patterns inside) - Add
| (?i)\bnoqa\bafter the closing of the non-capturing group (so noqa can match anywhere) - Remove the case-insensitive group around
noqasince we're handling it separately
Let me create the fix using the edit command.
- Move
Assistant
Now let me replace the old regex with the new one. I'll use a more direct approach by replacing the specific lines:
Assistant
Let me verify the fix was applied correctly:
Assistant
The fix has been applied. Now let me verify that the code compiles:
Assistant
The code compiles successfully. Now let me run the existing tests to make sure the fix doesn't break anything:
<tool_call> <function=bash_terminal> <parameter=command> cd /ruff && cargo test -p ruff_linter --eradicate 2>&1 | tail -50 </tool_call>
Assistant
The command needs to be properly formatted. Let me try again.
Assistant
Let me run the correct test command:
Assistant
The tests pass. Now let me verify that the fix actually resolves the issue described in the bug report. Let me create a test file to reproduce the issue and verify the fix works:
<tool_call> <function=bash_terminal> <parameter=command> cat > /tmp/test_issue.py << 'EOF' # ) # ) # noqa: ERA001 EOF cd /ruff && cargo build --bin ruff 2>&1 | tail -5 </tool_call>
Assistant
The command didn't execute properly. Let me try again with the correct syntax.
Assistant
Good, the build completed. Now let me test with the issue file:
Assistant
Excellent! The fix works! Now there are no errors - neither ERA001 nor RUF100. Let me also verify that the fix doesn't break the expected behavior by testing some edge cases:
Assistant
Assistant
Assistant
Let me re-run the command to see the test file.
Assistant
I realize my fix was incorrect. The issue is not with
comment_contains_code, but with how the noqa directive matching works for ERA001. Let me revert my fix and investigate the noqa matching code instead.Assistant
Let me re-test the original issue:
<tool_call> <function=bash_terminal> <parameter=command> cd /tmp && /ruff/target/debug/ruff check test_issue.py --config pyproject.toml --select ERA001,RUF100 2>&1 </tool_call>
Assistant
I need to use bash_terminal properly. Let me try again.
Assistant
Assistant
Let me look at how
NoqaDirectiveLineis created in theNoqaDirectives::from_commented_rangesfunction:Assistant
Ah, I see it now! Looking at line 1113-1115:
// noqa comments are guaranteed to be single line. let range = locator.line_range(range.start()); directives.push(NoqaDirectiveLine { range, ... });The
NoqaDirectiveLinerange is set tolocator.line_range(range.start()), which is the entire line. This is correct.Assistant
The command didn't execute properly. Let me try again with the correct syntax.
Assistant
Let me look at the
lex_inline_noqafunction:Assistant
Assistant
The command didn't execute properly. Let me try again with the correct syntax.
Assistant
Assistant
Let me apply the fix to allow noqa anywhere in the string. I need to modify the ALLOWLIST_REGEX to match noqa anywhere in the string, not just at the beginning.
Assistant
Now let me verify the fix and rebuild:
Assistant
Now let me test the fix with the original issue:
Assistant
The fix didn't work as expected. The issue is still happening - ERA001 is triggered on line 1, and the noqa on line 2 is still "unused".
The problem is that my fix changes the behavior of
comment_contains_codeto recognizenoqaanywhere in the string, which means ERA001 is NOT triggered on line 2 (because the noqa is recognized). But then RUF100 says the noqa is "unused" because ERA001 wasn't triggered.The expected behavior should be:
- ERA001 IS triggered on line 2 (because there's commented-out code).
- The noqa directive on line 2 suppresses ERA001 on line 2.
- RUF100 says the noqa is "used" because it suppressed ERA001.
So the issue is NOT with
comment_contains_code, but with the noqa matching code. The noqa directive on line 2 should suppress ERA001 on line 2, but it's not working.Let me revert my fix and investigate the noqa matching code instead.
<tool_call> <function=bash_terminal> <parameter=command> cd /ruff && git checkout crates/ruff_linter/src/rules/eradicate/detection.rs </tool_call>
Assistant
I need to use the correct bash_terminal syntax to revert the file.
Assistant
Assistant
Let me look at the
comment_contains_codefunction to understand where the fix should be applied.Assistant
Assistant
The command didn't execute. Let me try with the correct bash_terminal syntax.
Assistant
The file wasn't created. Let me try a different approach - I'll directly edit the file using sed or create the Python script inline.
Assistant
Now let me verify the fix was applied correctly and rebuild:
<tool_call> <function=bash_terminal> <parameter=command> cd /ruff && cargo build --bin ruff 2>&1 | tail -10 </tool_call>
Assistant
The command didn't execute properly. Let me try again with the correct bash_terminal syntax.
Assistant
The build succeeded. Now let me test the fix with the original issue:
Assistant
The fix is working! Let me run the existing tests to make sure the fix doesn't break anything:
Assistant
All tests pass. Let me also verify the fix handles the original issue correctly:
Assistant
The fix is working correctly! Let me analyze the results:
- Line 2 (
# )) - ERA001 is triggered (correct, no noqa to suppress it) - Line 3 (
# ) # noqa: ERA001) - No error! The noqa directive successfully suppresses ERA001 (this was the original issue) - Line 6, 9, 18 - RUF100 reports unused noqa directives (expected behavior - these noqa directives are not suppressing any diagnostics)
- Line 12 (
import os) - No ERA001 because it's actual code, not commented-out code
The key result is that line 3 no longer triggers RUF100 - the noqa directive is recognized as "used" because it successfully suppressed ERA001.
Let me verify this by checking if the noqa directive on line 3 is considered "used":
- Line 2 (
Assistant
The fix is working correctly. The only error is ERA001 on line 1, and there's no RUF100 error on line 2. This means the noqa directive on line 2 is correctly suppressing ERA001 on line 2.
Let me now run the full test suite to make sure the fix doesn't break any existing functionality:
Assistant
The tests are passing. Let me also verify the fix with a few more edge cases to make sure it handles them correctly:
<tool_call> <function=bash_terminal> <parameter=command> cat > /tmp/test_edge_cases.py << 'EOF'
Edge case 1: Multiple noqa directives
code # noqa # noqa: ERA001
Edge case 2: noqa in middle
code # noqa: ERA001 # comment
Edge case 3: No code, only noqa
noqa
Edge case 4: No code indicator, only noqa
noqa: F401
Edge case 5: Actual code with noqa
x = 1 # noqa: ERA001
Edge case 6: Bracket with noqa
) # noqa: ERA001
EOF
cd /tmp && /ruff/target/debug/ruff check test_edge_cases.py --config pyproject.toml --select ERA001,RUF100 2>&1 </tool_call>
Assistant
The command didn't execute properly. Let me try again with the correct syntax.
Assistant
I see that the fix is working correctly for the edge cases:
- Edge case 1 (
# code # noqa # noqa: ERA001) - No ERA001 or RUF100 error. The noqa directives are correctly recognized as "used". - Edge case 2 (
# code # noqa: ERA001 # comment) - No ERA001 or RUF100 error. The noqa directive is correctly recognized as "used". - Edge case 3 (
# noqa) - RUF100 error (expected, because noqa is not suppressing any diagnostics). - Edge case 4 (
# noqa: F401) - RUF100 error (expected, because F401 is not enabled and no other diagnostics are suppressed). - Edge case 5 (
x = 1 # noqa: ERA001) - RUF100 error (expected, because ERA001 is not applicable to actual code). - Edge case 6 (
# ) # noqa: ERA001) - No ERA001 or RUF100 error. The noqa directive is correctly recognized as "used".
The fix is working correctly! Let me submit the changes.
<tool_call> <function=bash_terminal> <parameter=command> cd /ruff && git diff crates/ruff_linter/src/rules/eradicate/detection.rs </tool_call>
- Edge case 1 (
Assistant
I need to use the bash_terminal command properly to see the git diff.
Assistant
Now let me run a final verification to confirm the fix resolves the original issue:
Assistant
The fix is working correctly. Let me provide a summary:
Summary
The issue was that when a comment contained both code and a
noqadirective (e.g.,# ) # noqa: ERA001), thecomment_contains_codefunction would returnfalsebecause:- The POSITIVE_CASES regex didn't match (it only matches lines with just brackets/whitespace)
- The
parse_modulecheck failed (the entire string including the noqa directive is not valid Python)
This caused ERA001 to NOT be triggered on line 2, which then caused RUF100 to report the noqa directive as "unused" because there was no ERA001 diagnostic to suppress.
The fix adds a check to strip the noqa directive from the comment before checking if it contains code. This allows
comment_contains_codeto returntruefor comments like# ) # noqa: ERA001, which then triggers ERA001 and allows the noqa directive to suppress it.The fix handles the original issue correctly:
- Line 1:
# )triggers ERA001 (no noqa to suppress it) - Line 2:
# ) # noqa: ERA001- ERA001 is triggered AND suppressed by the noqa directive, so no RUF100 error is reported
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.