Skip to content

Commit 49be4ca

Browse files
SONARJAVA-5178 Update RSPEC before 8.7 release
1 parent c89b664 commit 49be4ca

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S5411.html

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ <h2>Why is this an issue?</h2>
44
boolean Values</a>) it will throw a <code>NullPointerException</code> if the value is <code>null</code> (as defined in <a
55
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>
66
<p>It is safer to avoid such conversion altogether and handle the <code>null</code> value explicitly.</p>
7-
<p>Note, however, that no issues will be raised for Booleans that have already been null-checked.</p>
7+
<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>
88
<h3>Noncompliant code example</h3>
99
<pre>
1010
Boolean b = getBoolean();
@@ -29,6 +29,30 @@ <h3>Compliant solution</h3>
2929
String test = b ? "test" : "";
3030
}
3131
</pre>
32+
<h3>Exceptions</h3>
33+
<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
34+
instantiation of a generic type parameter and cannot be avoided.</p>
35+
<pre>
36+
List&lt;Boolean&gt; list = new ArrayList&lt;&gt;();
37+
list.add(true);
38+
list.add(false);
39+
list.forEach((@NonNull Boolean value) -&gt; {
40+
// Compliant
41+
if(value) {
42+
System.out.println("yes");
43+
}
44+
});
45+
46+
@NonNull Boolean someMethod() { /* ... */ }
47+
48+
// Compliant
49+
if(someMethod()) { /* ... */ }
50+
51+
@NonNull Boolean boxedNonNull = Boolean.TRUE;
52+
53+
// Compliant
54+
if(boxedNonNull) { /* ... */ }
55+
</pre>
3256
<h2>Resources</h2>
3357
<ul>
3458
<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>

sonarpedia.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"languages": [
44
"JAVA"
55
],
6-
"latest-update": "2024-11-22T09:48:48.563877Z",
6+
"latest-update": "2024-11-29T11:04:25.911576775Z",
77
"options": {
88
"no-language-in-filenames": true,
99
"preserve-filenames": false

0 commit comments

Comments
 (0)