-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create rule S7125: Null values should not be used in non-nullable input positions #4410
Draft
github-actions
wants to merge
2
commits into
master
Choose a base branch
from
rule/add-RSPEC-S7125
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"title": "Null values should not be used in non-nullable input positions", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-7125", | ||
"sqKey": "S7125", | ||
"scope": "Main", | ||
"defaultQualityProfiles": ["Sonar way"], | ||
"quickfix": "unknown", | ||
"code": { | ||
"impacts": { | ||
"RELIABILITY": "HIGH" | ||
}, | ||
"attribute": "CONVENTIONAL" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
== Why is this an issue? | ||
|
||
Using `null` in a non-nullable input position (e.g., as the right-hand side of an assignment, a function call argument, or a return statement argument) can lead to a NullPointerException (NPE) at runtime. This occurs because the receiving code typically assumes the value is non-null and omits null checks. | ||
|
||
Formally, non-nullable and nullable versions of a type are distinct, with different domains. | ||
The domain of a non-nullable type is _D_, while the domain of a nullable type is _D ∪ null_, a superset of _D_. | ||
Thus, a non-null value can be used wherever a nullable type is expected, but not vice versa. | ||
The only reason it's allowed by the compiler is that null-safety is not a built-in Java language feature, and it's therefore handled via nullability annotations by external tools bypassing the regular typing system. | ||
|
||
== How to fix it | ||
|
||
Depending on the use-case, there are different strategies to fix this problem | ||
|
||
=== Code examples | ||
|
||
**1. Change the input position type from non-nullable to nullable:** This resolves the issue at the reported location but may propagate it elsewhere. Note: you should avoid declaring everything nullable; only do so where it aligns with your data and state models. Otherwise, consider the other approaches. | ||
|
||
==== Noncompliant code example | ||
|
||
[source,java,diff-id=1,diff-type=noncompliant] | ||
---- | ||
@NonNull String title = null; | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,java,diff-id=1,diff-type=compliant] | ||
---- | ||
String title = null; | ||
---- | ||
|
||
==== Noncompliant code example | ||
|
||
[source,java,diff-id=2,diff-type=noncompliant] | ||
---- | ||
@NullMarked | ||
class Collector { | ||
void collectData(List<Entity> entities) { | ||
// ... | ||
} | ||
} | ||
|
||
void process() { | ||
collector.collectData(null); | ||
} | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,java,diff-id=2,diff-type=compliant] | ||
---- | ||
class Collector { | ||
void collectData(List<Entity> entities) { | ||
// ... | ||
} | ||
} | ||
|
||
void process() { | ||
collector.collectData(null); | ||
} | ||
---- | ||
|
||
**2. Replace `null` with a Guard Element:** This is particularly effective for array and collection types, where `null` can easily be replaced with an empty array or collection instance. | ||
|
||
==== Noncompliant code example | ||
|
||
[source,java,diff-id=3,diff-type=noncompliant] | ||
---- | ||
@NullMarked | ||
class Collector { | ||
void collectData(List<Entity> entities) { | ||
// ... | ||
} | ||
} | ||
|
||
void process() { | ||
collector.collectData(null); | ||
} | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,java,diff-id=3,diff-type=compliant] | ||
---- | ||
@NullMarked | ||
class Collector { | ||
void collectData(List<Entity> entities) { | ||
// ... | ||
} | ||
} | ||
|
||
void process() { | ||
collector.collectData(List.of()); | ||
} | ||
---- | ||
|
||
**3. Throw an Exception:** For unexpected or uninitialized values or unspecified behavior, throw an exception instead of returning `null`. This reports the issue at its origin, not somewhere else in the source code where the unexpected `null` value suddenly becomes a problem. This makes debugging easier. | ||
|
||
==== Noncompliant code example | ||
|
||
[source,java,diff-id=4,diff-type=noncompliant] | ||
---- | ||
@NonNull State getNextState() { | ||
return switch (state) { | ||
case State.PENDING -> State.PROCESSING; | ||
case State.PROCESSING -> State.PENDING; | ||
default -> null; | ||
}; | ||
} | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,java,diff-id=4,diff-type=compliant] | ||
---- | ||
@NonNull State getNextState() { | ||
return switch (state) { | ||
case State.PENDING -> State.PROCESSING; | ||
case State.PROCESSING -> State.PENDING; | ||
default -> throw new IllegalStateException(); | ||
}; | ||
} | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing a "==== Noncompliant code example" entry here.