Skip to content

Commit 9c1008c

Browse files
Suppress boxed Boolean warnings on @nonnull parameters.
1 parent 708fe30 commit 9c1008c

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

java-checks-test-sources/default/src/main/java/checks/BoxedBooleanExpressionsCheckSample.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package checks;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
35
import java.util.Optional;
46
import javax.annotation.CheckForNull;
57
import javax.annotation.Nonnull;
@@ -725,4 +727,32 @@ void testBoxedValue(Boolean value) {
725727
}
726728
}
727729

730+
public void lambdaParameterAnnotations() {
731+
List<Boolean> xs = new ArrayList<>();
732+
xs.add(true);
733+
734+
xs.forEach(b -> {
735+
if (b) { // Noncompliant
736+
foo();
737+
} else {
738+
bar();
739+
}
740+
});
741+
742+
xs.forEach((Boolean b) -> {
743+
if (b) { // Noncompliant
744+
foo();
745+
} else {
746+
bar();
747+
}
748+
});
749+
750+
xs.forEach((@NonNull Boolean b) -> {
751+
if (b) {
752+
foo();
753+
} else {
754+
bar();
755+
}
756+
});
757+
}
728758
}

java-checks/src/main/java/org/sonar/java/checks/BoxedBooleanExpressionsCheck.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ private static Optional<IfStatementTree> getParentConditionalBranch(Tree tree) {
217217

218218
@CheckForNull
219219
private static ExpressionTree findBoxedBoolean(ExpressionTree tree) {
220-
if (tree.symbolType().is(BOOLEAN) && !isValidMethodInvocation(tree)) {
220+
if (tree.symbolType().is(BOOLEAN) && !isValidMethodInvocation(tree) && !isNonnullBoxedBoolean(tree)) {
221221
return tree;
222222
}
223223
if (tree.is(Kind.LOGICAL_COMPLEMENT)) {
@@ -245,7 +245,7 @@ private static boolean isNullCheck(ExpressionTree tree) {
245245
private static boolean isValidMethodInvocation(ExpressionTree tree) {
246246
if (tree.is(Kind.METHOD_INVOCATION)) {
247247
MethodInvocationTree mit = (MethodInvocationTree) tree;
248-
return isOptionalInvocation(mit) || isAnnotatedNonnull(mit);
248+
return isOptionalInvocation(mit) || isAnnotatedNonnull(mit.methodSymbol());
249249
}
250250
return false;
251251
}
@@ -254,8 +254,16 @@ private static boolean isOptionalInvocation(MethodInvocationTree mit) {
254254
return OPTIONAL_OR_ELSE.matches(mit) && !mit.arguments().get(0).is(Kind.NULL_LITERAL);
255255
}
256256

257-
private static boolean isAnnotatedNonnull(MethodInvocationTree mit) {
258-
return mit.methodSymbol().metadata()
257+
private static boolean isNonnullBoxedBoolean(ExpressionTree tree) {
258+
if(!tree.is(Kind.IDENTIFIER)) {
259+
return false;
260+
}
261+
IdentifierTree identifierTree = (IdentifierTree)tree;
262+
return isAnnotatedNonnull(identifierTree.symbol());
263+
}
264+
265+
private static boolean isAnnotatedNonnull(Symbol symbol) {
266+
return symbol.metadata()
259267
.annotations()
260268
.stream()
261269
.map(SymbolMetadata.AnnotationInstance::symbol)

0 commit comments

Comments
 (0)