Skip to content

SONARJAVA-4426 S5778 allow calling factory methods to create empty collections#5633

Merged
tomasz-tylenda-sonarsource merged 7 commits into
masterfrom
tt/SONARJAVA-4426-empty-col-rt-ex
May 27, 2026
Merged

SONARJAVA-4426 S5778 allow calling factory methods to create empty collections#5633
tomasz-tylenda-sonarsource merged 7 commits into
masterfrom
tt/SONARJAVA-4426-empty-col-rt-ex

Conversation

@tomasz-tylenda-sonarsource
Copy link
Copy Markdown
Contributor

@tomasz-tylenda-sonarsource tomasz-tylenda-sonarsource commented May 27, 2026

Expand the list of methods that we can safely ignore.


Summary by Gitar

  • Rule update:
    • Extended OneExpectedRuntimeExceptionCheck to ignore Collections.singleton, Collections.singletonList, and Collections.singletonMap method calls.
  • Test update:
    • Added test cases in OneExpectedRuntimeExceptionCheckSample to verify the exclusion of Collections.singleton* methods.
    • Updated its/autoscan/src/test/resources/autoscan/diffs/diff_S5961.json to reflect new false negative count for rule S5961.

This will update automatically on new commits.

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

hashicorp-vault-sonar-prod Bot commented May 27, 2026

SONARJAVA-4426

Copy link
Copy Markdown
Contributor

@rombirli rombirli left a comment

Choose a reason for hiding this comment

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

It seems quite difficult to maintain an exhaustive list of all commonly used functions that never throw an exception. It might make more sense to rethink the underlying implementation of this rule in the future. That said, I'm approving this PR anyway, as it should still eliminate a solid number of false positives.

"ruleKey": "S5778",
"hasTruePositives": false,
"falseNegatives": 32,
"falseNegatives": 41,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure to understand correctly what this value is, but is it expected that we have more FN than before? Even if TPs are missclassified as FNs we shouldn't have more issues raised than before by just expanding the whitelist

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For some reason, this check does not work without semantics, so nothing is reported in autoscan. The new false negatives are the additional test cases that I added.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah I understand, thanks for this explanation.

COLLECTIONS_EMPTY,
COLLECTION_OF,
COLLECTION_CTOR,
EMPTY
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, I've seen your comment under the jira ticket, but there are many otheres non-raising functions, here are a few commonly used that could make sense to add.

  • Arrays.asList(...)
  • Collections.singleton* : Collections.singleton(o) / Collections.singletonList(o) / Collections.singletonMap(k, v)...
  • *.of: Stream.of(t) / Optional.ofNullable(v)...
  • boxing functions :Byte.valueOf(b), Boolean.valueOf(b), Integer.valueOf(i)
  • ...
    I'm not requesting any change, just proposing very commonly used functions you can add if you think it makes sense

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added Collections.singleton*. Arrays.asList can throw NPE. While the other examples can lead to FPs, I don't think we should be adding a large collection of cases unless we have some evidence that they happen in practice.

I think we can validate this PR on Peachee and follow up if we see more cases worth excluding.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sounds good!

@sonarqube-next
Copy link
Copy Markdown

@tomasz-tylenda-sonarsource tomasz-tylenda-sonarsource merged commit e9c8b4c into master May 27, 2026
16 of 17 checks passed
@tomasz-tylenda-sonarsource tomasz-tylenda-sonarsource deleted the tt/SONARJAVA-4426-empty-col-rt-ex branch May 27, 2026 14:31
@gitar-bot
Copy link
Copy Markdown

gitar-bot Bot commented May 27, 2026

Code Review ✅ Approved

Expands the list of ignored factory methods in OneExpectedRuntimeExceptionCheck to include Collections.singleton variants. No issues found.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing less information.

Comment with these commands to change:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

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