SONARJAVA-6198 S1451 should accept an empty headerFormat as the absence of any line#5576
SONARJAVA-6198 S1451 should accept an empty headerFormat as the absence of any line#5576NoemieBenard wants to merge 6 commits intomasterfrom
Conversation
SummaryThis PR fixes rule S1451 (file header check) to correctly handle an empty The problem: When The fix: In Tests added: Two new test classes demonstrate both cases—files with and without a leading blank line—and confirm no issues are reported when using the default (empty) header format. Cleanup: Removed unrelated code formatting issues in test files. What reviewers should knowWhere to start: Review the core logic change in Key points:
Watch for: Make sure the
|
There was a problem hiding this comment.
I see that the existing samples do not use descriptive names, but I think this should not stop us here. How about a name like ClassBlankLine.java or something similar?
There was a problem hiding this comment.
ClassNoHeader.java ?
| private String getHeaderFormat() { | ||
| String format = headerFormat; | ||
| if(format.charAt(0) != '^') { | ||
| if (format.charAt(0) != '^') { |
There was a problem hiding this comment.
Let's avoid reformatting unchanged code.
| } | ||
| } else { | ||
| expectedLines = headerFormat.split("(?:\r)?\n|\r"); | ||
| if (headerFormat.isEmpty()) { |
There was a problem hiding this comment.
Conditional expression is IMO more readable:
expectedLines = headerFormat.isEmpty())
? new String[] {}
: headerFormat.split("(?:\r)?\n|\r");
``
There was a problem hiding this comment.
I ended up handling the empty case before as it is also necessary to handle it when headerFormat is a regular expression (see #5577)
|
There was a problem hiding this comment.
The non-regex fix is logically sound: matches() short-circuits correctly when expectedLines.length == 0, and the removal of java:S1451 from the contains() assertion in JavaSensorTest correctly reflects the changed behavior. However, there is an asymmetry in the fix that leaves the regex path broken for the same empty-format input.
| private String getHeaderFormat() { | ||
| String format = headerFormat; | ||
| if(format.charAt(0) != '^') { | ||
| if (format.charAt(0) != '^') { |
There was a problem hiding this comment.
When isRegularExpression = true and headerFormat is empty, getHeaderFormat() is still called and will throw StringIndexOutOfBoundsException at format.charAt(0) on an empty string.
The PR fixes the non-regex path for empty headerFormat but leaves the regex path unguarded. A user who enables the regex mode and leaves headerFormat empty (the default) will get a runtime exception instead of the "no header required" behaviour.
Consider guarding the same way as the non-regex path, or at minimum returning "^" (match-everything) when the format is empty:
| if (format.charAt(0) != '^') { | |
| private String getHeaderFormat() { | |
| String format = headerFormat; | |
| if (format.isEmpty() || format.charAt(0) != '^') { | |
| format = "^" + format; | |
| } | |
| return format; | |
| } |
- Mark as noise




Modify S1451 to recognize an empty headerFormat as the absence of any line.