'Optional.get() is called without isPresent()' check losing information based on unrelated asserts

Answered

The named inspection triggers on the `.get()` in the second `assert`, but not the first.

@VisibleForTesting
static Stream<FilterDifference> compareFilters(
String hostname,
String filtername,
List<LineAction> currentActions,
List<BDD> currentLineBdds,
List<LineAction> referenceActions,
List<BDD> referenceLineBdds) {
checkArgument(!currentLineBdds.isEmpty());
checkArgument(!referenceLineBdds.isEmpty());
checkArgument(currentActions.size() == currentLineBdds.size() - 1);
checkArgument(referenceActions.size() == referenceLineBdds.size() - 1);
assert currentLineBdds.stream().reduce(BDD::or).get().isOne();
assert referenceLineBdds.stream().reduce(BDD::or).get().isOne(); // warning on .get()

If I comment out the prior assert, the warning is gone.

@VisibleForTesting
static Stream<FilterDifference> compareFilters(
String hostname,
String filtername,
List<LineAction> currentActions,
List<BDD> currentLineBdds,
List<LineAction> referenceActions,
List<BDD> referenceLineBdds) {
checkArgument(!currentLineBdds.isEmpty());
checkArgument(!referenceLineBdds.isEmpty());
checkArgument(currentActions.size() == currentLineBdds.size() - 1);
checkArgument(referenceActions.size() == referenceLineBdds.size() - 1);
// assert currentLineBdds.stream().reduce(BDD::or).get().isOne();
assert referenceLineBdds.stream().reduce(BDD::or).get().isOne();

If I reorder the code so that the (completely independent) checks of the different variables are not interleaved, the warning goes away:


checkArgument(!currentLineBdds.isEmpty());
checkArgument(currentActions.size() == currentLineBdds.size() - 1);
assert currentLineBdds.stream().reduce(BDD::or).get().isOne();
checkArgument(!referenceLineBdds.isEmpty());
checkArgument(referenceActions.size() == referenceLineBdds.size() - 1);
assert referenceLineBdds.stream().reduce(BDD::or).get().isOne();

I was not able to get the same bug to trigger with simpler types. E.g., this code does not warn:

private static void showBug2(
List<Integer> l1, List<String> l3, List<Integer> l2, List<String> l4) {
checkArgument(!l1.isEmpty());
checkArgument(!l2.isEmpty());
checkArgument(l3.size() == l1.size() - 1);
checkArgument(l4.size() == l2.size() - 1);
assert l1.stream().reduce((a, b) -> a).get().equals(0L);
assert l2.stream().reduce((a, b) -> a).get().equals(0L);
}

Seems like a bug in the inspection, but I couldn't figure out what.

0
2 comments

Daniel, 

Thank you for the report. Please follow the issue created for it at YouTrack:

https://youtrack.jetbrains.com/issue/IDEA-220751

1

Please sign in to leave a comment.