Skip to content

SONARJAVA-6198 S1451 should accept an empty headerFormat as the absence of any line#5576

Open
NoemieBenard wants to merge 3 commits intomasterfrom
sonarjava-6198
Open

SONARJAVA-6198 S1451 should accept an empty headerFormat as the absence of any line#5576
NoemieBenard wants to merge 3 commits intomasterfrom
sonarjava-6198

Conversation

@NoemieBenard
Copy link
Copy Markdown
Contributor

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

@hashicorp-vault-sonar-prod
Copy link
Copy Markdown
Contributor

hashicorp-vault-sonar-prod bot commented Apr 16, 2026

SONARJAVA-6198

@sonar-review-alpha
Copy link
Copy Markdown
Contributor

sonar-review-alpha bot commented Apr 16, 2026

Summary

What changed: Modified FileHeaderCheck to recognize an empty headerFormat configuration as "no header required" rather than crashing or splitting an empty string. When headerFormat is empty, the check now sets expectedLines to an empty array, allowing any file (with or without blank lines at the start) to pass.

Why: This handles an edge case where users configure the rule with an empty value to disable header validation. Previously, the code didn't explicitly handle this case, leading to undefined behavior. The fix makes the intent clear: empty format = no header needed.

What reviewers should know

Where to start: Review the logic change in FileHeaderCheck.java (the empty check and split logic).

Key points for reviewers:

  • The fix is defensive: checks if headerFormat.isEmpty() before calling split, avoiding potential issues with splitting an empty string
  • Two new test files validate the behavior: Class4 (with leading blank line) and Class5 (without), both should pass with empty headerFormat
  • Minor cleanup: also fixed whitespace in if(if ( on the same line (lines 81-82)
  • The getHeaderFormat() method still needs protection when called with empty format (charAt(0) call), but that's only called when headerFormat is explicitly set to a non-empty regex pattern, so it's safe

  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

sonar-review-alpha[bot]

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor

@sonar-review-alpha sonar-review-alpha bot left a comment

Choose a reason for hiding this comment

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

LGTM! ✅

🗣️ Give feedback

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.

1 participant