Skip to content

fix(add-noqa): preserve trailing content after noqa directives#26437

Open
sergioperezcheco wants to merge 1 commit into
astral-sh:mainfrom
sergioperezcheco:fix/add-noqa-preserve-trailing-content
Open

fix(add-noqa): preserve trailing content after noqa directives#26437
sergioperezcheco wants to merge 1 commit into
astral-sh:mainfrom
sergioperezcheco:fix/add-noqa-preserve-trailing-content

Conversation

@sergioperezcheco

Copy link
Copy Markdown

Summary

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.

Reproduction

import math  # noqa: RUF100 with a trailing reason
import sys  # noqa: RUF100 # fmt:skip

Before (broken):

import math  # noqa: F401, RUF100
import sys  # noqa: F401, RUF100

Both the trailing reason and # fmt:skip are deleted.

After (fixed):

import math  # noqa: F401, RUF100 with a trailing reason
import sys  # noqa: F401, RUF100 # fmt:skip

Trailing content is preserved.

Root Cause

In generate_noqa_edit (crates/ruff_linter/src/noqa.rs), the Directive::Codes branch computed edit_range as TextRange::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

  1. Limit the edit range for Directive::Codes to only cover up to existing_codes.end() — the end of the noqa directive itself — leaving trailing content untouched.
  2. Add an emit_line_ending: bool flag to NoqaEdit so the line ending is only emitted when the edit actually covers the end of the line (the None / blank-line case).

Test Plan

Added add_noqa_existing_noqa_with_trailing_content test verifying both scenarios (trailing reason and # fmt:skip). All 14 existing add_noqa tests continue to pass.

test lint::add_noqa ... ok
test lint::add_noqa_existing_noqa ... ok
test lint::add_noqa_existing_noqa_with_trailing_content ... ok   ← new
... (14 passed, 0 failed)

Closes #26287

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
@astral-sh-bot astral-sh-bot Bot requested a review from ntBre June 28, 2026 04:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--add-noqa deletes trailing content

1 participant