SONARJAVA-6269 S1451 should correctly handle empty headerFormat#5580
SONARJAVA-6269 S1451 should correctly handle empty headerFormat#5580NoemieBenard wants to merge 12 commits intomasterfrom
Conversation
SummaryThis PR fixes SONARJAVA-6269 where FileHeaderCheck was incorrectly raising issues when headerFormat was empty. The fix refactors the check to:
The result is that S1451 no longer produces false positives when users leave headerFormat unconfigured or set it empty, while maintaining the existing behavior for non-empty patterns (both regex and literal text). What reviewers should knowStart with the main logic: Review the refactored Key changes to understand:
Test coverage: New test files and test cases cover all three scenarios now:
Note: The removal of S1451 from JavaSensorTest (line 432) is intentional — it documents that this check is no longer expected to produce issues in the default configuration after this fix.
|
There was a problem hiding this comment.
ClassDefaultHeader.java?
There was a problem hiding this comment.
Do you really need this file and RegexNoBlankLine.java? Why not reuse ClassBlankLine.java?
| check = new FileHeaderCheck(); | ||
| check.headerFormat = ""; | ||
| CheckVerifier.newVerifier() | ||
| .onFile(mainCodeSourcesPath("checks/FileHeaderCheck/ClassBlankLine.java")) | ||
| .withCheck(check) | ||
| .verifyNoIssues(); |
There was a problem hiding this comment.
ClassBlankLine.java starts with a blank line. Under the old code, "".split(…) produced [""], and matches([""], lines) compared the first line to "" — a blank-first-line file already passed. This test case would have been green before the fix too, so it doesn't verify anything new.
The real non-regex regression test is ClassNoBlankLine.java (first line is package …, which the old code incorrectly flagged). Consider replacing ClassBlankLine.java with a file that would have been mistakenly flagged under the old behavior — or just remove this case, since ClassNoBlankLine.java already covers the fix.
- Mark as noise
| CheckVerifier.newVerifier() | ||
| .onFile(mainCodeSourcesPath("checks/FileHeaderCheck/ClassBlankLine.java")) | ||
| .withCheck(check) | ||
| .verifyNoIssues(); |
There was a problem hiding this comment.
ClassBlankLine.java starts with a blank line (line 1 is ""). Under the old code, "".split("(?:\r)?\n|\r") returned [""], and matches([""], fileLines) compared the first line "" to the expected "" — a match, so no issue was reported. This test was already green before the fix and doesn't verify anything new for the non-regex path.
The real regression test for the non-regex fix is ClassNoBlankLine.java immediately below (first line is "package...", which the old code incorrectly flagged). Either remove the ClassBlankLine.java case here, or replace it with a comment explaining that it's not a regression test but a boundary-condition sanity check.
- Mark as noise
…ssue after the empty header fix
|
There was a problem hiding this comment.
LGTM! ✅
Clean fix for two related bugs triggered by an empty headerFormat. The refactoring is correct and the logic is easy to follow.
Behavior verified:
- Empty array passed to
matches()always returnstrue(0 <= lines.size()holds even for empty files; the for-each never executes), so no issue is ever raised — exactly the desired "skip check" semantics. - The regex path is fully guarded: the empty check fires before
getHeaderFormat()is reached, preventing the pre-existingStringIndexOutOfBoundsExceptionon"".charAt(0). - The
searchPatternlazy-init guard is unchanged and still correct for non-empty regex headers.




Refactor FileHeaderCheck to handle empty headers correctly.