'Optional.get() is called without isPresent()' check losing information based on unrelated asserts
已回答
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.
请先登录再写评论。
This code and the IntelliJ project that go with it are open source: https://github.com/batfish/batfish/blob/3d21180487917d08f5a1321ec909daa362b219a8/projects/question/src/main/java/org/batfish/question/comparefilters/CompareFiltersAnswerer.java#L195-L208
IntelliJ tips for getting started with this project: https://github.com/batfish/batfish/wiki/IntelliJ
Daniel,
Thank you for the report. Please follow the issue created for it at YouTrack:
https://youtrack.jetbrains.com/issue/IDEA-220751