fix(add-noqa): preserve trailing content after noqa directives#26437
Open
sergioperezcheco wants to merge 1 commit into
Open
fix(add-noqa): preserve trailing content after noqa directives#26437sergioperezcheco wants to merge 1 commit into
sergioperezcheco wants to merge 1 commit into
Conversation
When running --add-noqa on a line with an existing noqa directive that
has trailing content (e.g. a trailing reason or a # fmt:skip comment),
the edit was extending to the end of the line and overwriting that
trailing content.
For example:
import math # noqa: RUF100 with a trailing reason
import sys # noqa: RUF100 # fmt:skip
Was incorrectly transformed to:
import math # noqa: F401, RUF100
import sys # noqa: F401, RUF100
Fix: when there is an existing Directive::Codes, limit the edit range
to only cover the noqa directive itself (from the trimmed code to
existing_codes.end()), rather than extending to the full line end.
Add an emit_line_ending flag to NoqaEdit so the line ending is only
emitted when the edit actually covers the end of the line.
Closes astral-sh#26287
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When running
--add-noqaon a line with an existingnoqadirective that has trailing content (e.g. a trailing reason or a# fmt:skipcomment), the edit was extending to the end of the line and overwriting that trailing content.Reproduction
Before (broken):
Both the trailing reason and
# fmt:skipare deleted.After (fixed):
Trailing content is preserved.
Root Cause
In
generate_noqa_edit(crates/ruff_linter/src/noqa.rs), theDirective::Codesbranch computededit_rangeasTextRange::new(TextSize::of(trimmed_line), line_range.len()), which extends from the end of the trimmed code to the end of the full line. This range includes any trailing content after the noqa directive.Fix
Directive::Codesto only cover up toexisting_codes.end()— the end of the noqa directive itself — leaving trailing content untouched.emit_line_ending: boolflag toNoqaEditso the line ending is only emitted when the edit actually covers the end of the line (theNone/ blank-line case).Test Plan
Added
add_noqa_existing_noqa_with_trailing_contenttest verifying both scenarios (trailing reason and# fmt:skip). All 14 existingadd_noqatests continue to pass.Closes #26287