SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

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

Open yunusdim opened this issue 3 months ago • 3 comments

Hi, an observation about the baseline filter (Source/SwiftLintCore/Models/Baseline.swift), not a bug report. It comes from a formal result on separation criteria I deposited recently.

A baseline entry is matched first by exact identity (rule, file, line, character, reason, and the source line's text), and any exact match is removed from both sides before anything else happens. What's left is grouped by rule, and within a rule, grouped again by a key that's just the source line's text plus the reason string, with no location in it. For each key, if the current count of violations sharing that key is greater than the baseline's count for that key, all of them get reported; if it's less than or equal, none do.

That second part means two violations of the same rule with the exact same text and the exact same reason, at different locations, are indistinguishable once they're grouped, because the key drops the location. I reimplemented the algorithm from source (a small script, not the real binary) and ran this: a baseline with two identical TODO violations at line 10 and line 20 (same text, same reason). Then I fix line 10 for real, and a genuinely new TODO with the identical text and reason shows up at line 30, unrelated to either original. The current violations, after removing exact matches, are just line 20 (unchanged) and line 30 (new). Grouped by key they're both in the same bucket as the one leftover baseline entry (line 10's, now unmatched), count 1 vs count 1, so nothing gets reported. The new line-30 violation disappears.

This needs duplicate text and duplicate reason to collide, so it won't happen for rules whose reason includes specifics (a value, a name), but it will for rules with a fixed, generic reason and short repeated lines. todo is the obvious one, and I'd guess mark, orphaned_doc_comment, and some of the pattern-matching rules on very short or common expressions have the same shape.

Would it make sense to include location, or at least something coarser like file, when deciding whether two same-key violations are the same reported instance, instead of relying on key count alone once the exact-identity pass has already run? I know that's basically re-adding what the initial exact-match pass already checks, but the count-only fallback for the collision case is what loses it.

Reference, if useful: Closure Trivialization in Typed Separation Systems, https://doi.org/10.5281/zenodo.21908527, section 10.

yunusdim avatar Aug 14 '26 22:08 yunusdim

I reproduced this on current main with a focused regression test: keeping one duplicate violation and replacing the other with a same-text/same-reason violation leaves the filter result empty.\n\nThere is a policy trade-off in the fix: simply retaining location in the fallback key catches the replacement, but breaks the existing intentional behavior that ignores uniformly shifted violations. A context-aware baseline entry could distinguish more cases, but would need a baseline-format and compatibility decision.\n\nI have the reproducer ready. Could a maintainer confirm the intended precedence before I propose an implementation?

LizunovSergey avatar Aug 15 '26 08:08 LizunovSergey

An update after #6872, which I am closing. The case in this issue (duplicates at {10, 20}; line 10 fixed and a new duplicate added at 30, so {10, 20} → {20, 30}) is the same filter input as a uniform 10-line shift of {10, 20}. With rule, reason and line text as the key, the two cannot be told apart, so any fix that reports this case also reports a plain shift. This is the limit #5475 documents ("as long as the number of violations of that rule ... did not increase").

Closing it properly needs more in the baseline entry, such as a hash of neighbouring lines, plus a fallback for existing baselines. That is a format decision for maintainers, so I am leaving it here rather than opening another PR.

LizunovSergey avatar Nov 02 '26 20:11 LizunovSergey

Thanks for taking it this far and for writing down why the PR closes. That is the precise statement of the limit: with rule, reason and line text as the key, a replacement and a uniform shift are the same filter input, so the key cannot separate them no matter how the counting is done. The only way out is to put identity into the entry itself, and a hash of neighbouring lines is exactly that. Agreed it is a format and compatibility decision for maintainers, so I will leave it here as well.

yunusdim avatar Nov 03 '26 03:11 yunusdim