Skip to content

fix: drop the backtracking trailing-whitespace regex - #144

Merged
svaningelgem merged 2 commits into
masterfrom
fix/sonar-findings
Aug 6, 2026
Merged

fix: drop the backtracking trailing-whitespace regex#144
svaningelgem merged 2 commits into
masterfrom
fix/sonar-findings

Conversation

@svaningelgem

@svaningelgem svaningelgem commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

Clears the two Sonar findings on master that are worth code changes. 263 tests green.

Fixed — the two MAJOR regex findings

YamlFileInterface:315 and YamlWriter:274 both did line.replaceAll("\\s*$", "") to trim a comment line. Sonar is right that it's super-linear. The stdlib has done this since Java 11 and the project targets 17:

line.replaceAll("\\s*$", "")  ->  line.stripTrailing()

No regex engine, clearer intent, and stripTrailing() uses Character.isWhitespace so it also catches Unicode whitespace that the ASCII-only \s class misses.

Fixed — the MINOR "unused collection", by making the test real

YamlWriterEdgeCaseTests.testNormalizeCollectionWithNonComparableSet built a Set, then asserted on new ArrayList<>(nonComparableSet)it never called save(), so it covered the JDK's ArrayList constructor rather than normalizeCollection. That's what Sonar's "unused collection" was pointing at.

Rather than delete it, it now does what its name says: saves a Set<Address> (records aren't Comparable) and asserts the insertion order survives, since a set that can't be sorted must be written as it came in.

Verified it actually guards the branch — dropping the instanceof Comparable check from normalizeCollection makes it fail with a ClassCastException rather than pass quietly.

Not changed — two I'd suppress instead

TypeConverter:79, cognitive complexity 21. This is the flat dispatch table deliberately left alone in #143: fourteen sequential if (...) return ...; guards with zero nesting, whose order is the meaning. It was WONTFIX'd before, and only reappeared because #141 shifted its declaration line and Sonar lost the issue↔suppression match. Worth re-marking WONTFIX rather than splitting.

PluginLoggerRoutingTest:63, unused parameter args. VarargsStyleLogger.warning(String, Object...) exists precisely so discoverPluginSink can find that signature by reflection — the unused parameter is the thing under test. Removing it deletes the test's reason to exist. Worth marking false-positive.

Also

Corrected the CoverageBoostTests.testNonComparableSetInYaml comment, which said "UUIDs are Comparable, so let's use a List instead to verify the path" while using a Set<UUID>. It covers the sorting side; the comment now says so and points at the test that covers the other side.

replaceAll("\s*$", "") has super-linear runtime on long lines. String.stripTrailing()
(Java 11+) does the same job with no regex engine involved, and handles Unicode
whitespace the ASCII-only \s class misses.

Also deletes testNormalizeCollectionWithNonComparableSet, which built a Set and
asserted on new ArrayList<>(set) without ever calling save() -- it exercised the
JDK, not YamlWriter. normalizeCollection stays at 6/6 branches covered without it.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
.../main/java/org/avarion/yaml/YamlFileInterface.java 100.00% <100.00%> (ø)
src/main/java/org/avarion/yaml/YamlWriter.java 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

The test built a Set and asserted on new ArrayList<>(set) without ever calling
save(), so it covered the JDK rather than normalizeCollection. It now saves a
Set<Address> -- records aren't Comparable -- and asserts insertion order survives,
which fails with a ClassCastException if the Comparable guard is ever dropped.

Also corrects the CoverageBoostTests comment that claimed to use a List when it
uses a Set of Comparable UUIDs.
@sonarqubecloud

sonarqubecloud Bot commented Aug 6, 2026

Copy link
Copy Markdown

@svaningelgem
svaningelgem merged commit cc45d9f into master Aug 6, 2026
3 checks passed
@svaningelgem
svaningelgem deleted the fix/sonar-findings branch August 6, 2026 04:08
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.

2 participants