Skip to content

Commit

Permalink
Modify S5111 explaining that @nonnull values are ignored.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasz-tylenda-sonarsource committed Nov 25, 2024
1 parent f0b8295 commit 86fa22d
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion rules/S5411/java/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ When boxed type `java.lang.Boolean` is used as an expression to determine the co

It is safer to avoid such conversion altogether and handle the `null` value explicitly.

Note, however, that no issues will be raised for Booleans that have already been null-checked.
Note, however, that no issues will be raised for Booleans that have already been null-checked or are marked `@NonNull/@NotNull`.

=== Noncompliant code example

Expand Down Expand Up @@ -38,6 +38,35 @@ if(b != null){
}
----

=== Exceptions

The issue is not raised if the expression is annotated `@NonNull` / `@NotNull`.
This is useful if a boxed type is an instantiation of a generic type parameter
and cannot be avoided.

[source,java]
----
List<Boolean> list = new ArrayList<>();
list.add(true);
list.add(false);
list.forEach((@NonNull Boolean value) -> {
// Compliant
if(value) {
System.out.println("yes");
}
});
@NonNull Boolean someMethod() { /* ... */ }
// Compliant
if(someMethod()) { /* ... */ }
@NonNull Boolean boxedNonNull = Boolean.TRUE;
// Compliant
if(boxedNonNull) { /* ... */ }
----

== Resources

* https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.8[Java Language Specification §5.1.8 Unboxing Conversion]
Expand Down

0 comments on commit 86fa22d

Please sign in to comment.