Skip to content

Fix baseline duplicate replacements - #6872

Closed
LizunovSergey wants to merge 2 commits into
realm:mainfrom
LizunovSergey:fix/baseline-duplicate-swap
Closed

LizunovSergey wants to merge 2 commits into
realm:mainfrom
LizunovSergey:fix/baseline-duplicate-swap

Conversation

@LizunovSergey

Copy link
Copy Markdown

Fixes #6871

Summary

  • report a same-count replacement of identical baseline violations when another duplicate still matches exactly
  • preserve the existing behavior for uniformly shifted duplicate violations

Root cause

After exact matches are removed, the fallback comparison used only the number of remaining violations with the same text and reason. A replacement could therefore be hidden by an unmatched baseline duplicate with the same key.

An unchanged duplicate now acts as an anchor: if the remaining counts are equal within that group, the unmatched current violations are reported. A group with no exact match still follows the existing shift-tolerant behavior.

Tests

  • swift test --filter BaselineTests

@LizunovSergey
LizunovSergey marked this pull request as ready for review August 15, 2026 18:06
@SwiftLintBot

SwiftLintBot commented Aug 15, 2026 •

Copy link
Copy Markdown
20 Messages
📖 Building this branch resulted in a binary size of 28461.48 KiB vs 28460.51 KiB when built on main (0% larger).
📖 Linting Aerial with this PR took 0.69 s vs 0.68 s on main (1% slower).
📖 Linting Alamofire with this PR took 0.97 s vs 0.97 s on main (0% slower).
📖 Linting Brave with this PR took 6.34 s vs 6.28 s on main (0% slower).
📖 Linting Brigade with this PR took 18.12 s vs 18.13 s on main (0% faster).
📖 Linting DuckDuckGo with this PR took 29.94 s vs 29.84 s on main (0% slower).
📖 Linting Firefox with this PR took 11.44 s vs 11.43 s on main (0% slower).
📖 Linting Kickstarter with this PR took 7.33 s vs 7.35 s on main (0% faster).
📖 Linting Moya with this PR took 0.37 s vs 0.38 s on main (2% faster).
📖 Linting NetNewsWire with this PR took 2.51 s vs 2.54 s on main (1% faster).
📖 Linting Nimble with this PR took 0.58 s vs 0.56 s on main (3% slower).
📖 Linting PocketCasts with this PR took 7.4 s vs 7.41 s on main (0% faster).
📖 Linting Quick with this PR took 0.36 s vs 0.36 s on main (0% slower).
📖 Linting Realm with this PR took 2.99 s vs 2.97 s on main (0% slower).
📖 Linting Sourcery with this PR took 1.59 s vs 1.67 s on main (4% faster).
📖 Linting Swift with this PR took 4.52 s vs 4.52 s on main (0% slower).
📖 Linting SwiftLintPerformanceTests with this PR took 0.18 s vs 0.18 s on main (0% slower).
📖 Linting VLC with this PR took 1.31 s vs 1.31 s on main (0% slower).
📖 Linting Wire with this PR took 16.3 s vs 16.33 s on main (0% faster).
📖 Linting WordPress with this PR took 10.52 s vs 10.49 s on main (0% slower).

Generated by 🚫 Danger

@yunusdim

Copy link
Copy Markdown

Author of #6871. The anchor approach is the right call: an unchanged duplicate is the evidence that the group did not shift, which is exactly the distinction the old fallback could not make. Keeping the shift-tolerant path where no exact match survives leaves the intentional behaviour intact.

One residual worth stating, not a request to change anything. When every duplicate in a group is replaced in the same run, no anchor survives, the group falls through to the shift-tolerant path, and the swap stays invisible. That is narrower than what #6871 reported — it needs the whole group to turn over at once — but it is the same shape, and it is better known now than discovered later.

Reason and line text remain the only keys on that path, so the residual is bounded by how generic those are. todo, mark and orphaned_doc_comment are where it would show.

Checks are green apart from the ones waiting on maintainer approval.

@LizunovSergey

Copy link
Copy Markdown
Author

Thanks — and the residual is real. Worth stating why it stays open rather than getting folded in here.

The anchor is the only evidence available. Reason and line text are the whole key on that path, so a
group where every duplicate turned over in one run looks byte-for-byte identical to the same group
having shifted: same rule, same texts, same count, nothing unchanged to pin it to. There is no
signal left to separate the two. Treating a same-count, zero-anchor group as new would report every
shifted group as a fresh violation, which is exactly the false positive the shift-tolerant path
exists to avoid — a strictly worse trade than the miss it would close.

So it is bounded the way you describe: it needs the whole group to turn over at once, and it only
bites where the key is generic enough for a group to exist at all — todo, mark,
orphaned_doc_comment. Closing it properly needs a stronger key than reason plus line text, which
is a different change from this one.

@LizunovSergey
LizunovSergey force-pushed the fix/baseline-duplicate-swap branch from 438b3f0 to 945fb2c Compare August 29, 2026 09:28
@yunusdim

Copy link
Copy Markdown

Agreed, and that is the right line to draw. A key stronger than reason plus line text is a different change from this one; folding it in here would report every shifted group as new, which is the false positive the shift-tolerant path exists to avoid. This PR closes #6871 as reported. The whole-group-turnover case is worth its own issue against a stronger key, not a rider on this one.

@Akhrameev

Copy link
Copy Markdown

Thanks for picking this up. I think the anchor heuristic introduces a false positive for a common edit pattern: a shift that only moves some of the duplicates.

Two identical // TODO violations at lines 10 and 20 are baselined. Inserting two lines after line 15 leaves the first at 10 (exact match, so it becomes the anchor) and moves the second to 22. The remaining group is then 1 vs 1 with an anchor present, so line 22 is reported as new, although nothing was added. On main this case stays silent.

A related case: a uniform 10-line shift of {10, 20} produces {20, 30}, which is exactly the input of reportsSameCountReplacementOfDuplicateViolations. So that test expects a pure shift to be reported as a new violation. With only rule, line text and reason as the key, the two cases look the same, so an exact match doesn't seem to show that the group stayed in place.

Repro, using the PR's helpers:

@Test(.temporaryDirectory)
func ignoresShiftBetweenDuplicateViolations() throws {
    let sourceFilePath = URL.cwd.appending(path: "Example.swift", directoryHint: .notDirectory)
    try Self.lines(withDuplicatesAt: [10, 20]).joined(separator: "\n").write(
        to: sourceFilePath, atomically: true, encoding: .utf8
    )
    let baseline = Baseline(violations: Self.duplicateViolations(at: [10, 20], in: sourceFilePath))
    try Self.lines(withDuplicatesAt: [10, 22]).joined(separator: "\n").write(
        to: sourceFilePath, atomically: true, encoding: .utf8
    )
    let currentViolations = Self.duplicateViolations(at: [10, 22], in: sourceFilePath)
    #expect(baseline.filter(currentViolations).isEmpty)
}

This passes on main and fails on this branch.


#expect(baseline.filter(currentViolations).isEmpty)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repro from my comment, as a suggestion. It passes on main and fails on this branch.

Suggested change
@Test(.temporaryDirectory)
func ignoresShiftBetweenDuplicateViolations() throws {
let sourceFilePath = URL.cwd.appending(path: "Example.swift", directoryHint: .notDirectory)
try Self.lines(withDuplicatesAt: [10, 20]).joined(separator: "\n").write(
to: sourceFilePath,
atomically: true,
encoding: .utf8
)
let baseline = Baseline(violations: Self.duplicateViolations(at: [10, 20], in: sourceFilePath))
try Self.lines(withDuplicatesAt: [10, 22]).joined(separator: "\n").write(
to: sourceFilePath,
atomically: true,
encoding: .utf8
)
let currentViolations = Self.duplicateViolations(at: [10, 22], in: sourceFilePath)
#expect(baseline.filter(currentViolations).isEmpty)
}

@LizunovSergey

Copy link
Copy Markdown
Author

@Akhrameev you are right on both points, and thanks for the repro.

A uniform 10-line shift of {10, 20} gives the filter exactly the input of the case in #6871: same rule, same reason, same line texts, lines {20, 30}. So no filter keyed on those fields can report one and ignore the other. The anchor heuristic also flags your partial shift, which main handles correctly. My earlier reply here was wrong to treat an exact match as evidence that the group stayed in place: a shift by the gap between duplicates creates the same match.

Closing this PR. I added a note on #6871.

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.

Baseline: a same-count swap of duplicate-text violations within a rule is invisible

4 participants