Skip to content
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

SONARJAVA-5178 Update RSPEC before 8.7 release #4940

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ <h2>Why is this an issue?</h2>
boolean Values</a>) it will throw a <code>NullPointerException</code> if the value is <code>null</code> (as defined in <a
href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.8">Java Language Specification §5.1.8 Unboxing Conversion</a>).</p>
<p>It is safer to avoid such conversion altogether and handle the <code>null</code> value explicitly.</p>
<p>Note, however, that no issues will be raised for Booleans that have already been null-checked.</p>
<p>Note, however, that no issues will be raised for Booleans that have already been null-checked or are marked <code>@NonNull/@NotNull</code>.</p>
<h3>Noncompliant code example</h3>
<pre>
Boolean b = getBoolean();
Expand All @@ -29,6 +29,30 @@ <h3>Compliant solution</h3>
String test = b ? "test" : "";
}
</pre>
<h3>Exceptions</h3>
<p>The issue is not raised if the expression is annotated <code>@NonNull</code> / <code>@NotNull</code>. This is useful if a boxed type is an
instantiation of a generic type parameter and cannot be avoided.</p>
<pre>
List&lt;Boolean&gt; list = new ArrayList&lt;&gt;();
list.add(true);
list.add(false);
list.forEach((@NonNull Boolean value) -&gt; {
// Compliant
if(value) {
System.out.println("yes");
}
});

@NonNull Boolean someMethod() { /* ... */ }

// Compliant
if(someMethod()) { /* ... */ }

@NonNull Boolean boxedNonNull = Boolean.TRUE;

// Compliant
if(boxedNonNull) { /* ... */ }
</pre>
<h2>Resources</h2>
<ul>
<li> <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.8">Java Language Specification §5.1.8 Unboxing Conversion</a>
Expand Down
2 changes: 1 addition & 1 deletion sonarpedia.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"languages": [
"JAVA"
],
"latest-update": "2024-11-22T09:48:48.563877Z",
"latest-update": "2024-11-29T11:04:25.911576775Z",
"options": {
"no-language-in-filenames": true,
"preserve-filenames": false
Expand Down