When I am scanning code with sonar lint the following code shows the bug as "The return value of "orElseThrow" must be used"
itemList.stream()
.filter(item -> orderItemId.equals(item.getId()))
.findAny()
.orElseThrow(() -> new BadRequestException("12345","Item Not Found"));
This is just for a validation purpose no need to return anything from this statement. need to validate whether the item exists or not.
FYI: Eclipse showing a quick fix as squid:S2201
Anybody have any idea how to resolve this bug?
I'm assuming this is a warning (not using the value returned by orElseThrow()
shouldn't be an error).
If you wish to eliminate that warning, use isPresent()
instead:
if (!itemList.stream().filter(i->orderItemId.equals(i.getId())).findAny().isPresent()) {
throw new BadRequestException("12345","Item Not Found");
}
or just avoid using Optional
s, and use anyMatch()
instead:
if (!itemList.stream().anyMatch(i->orderItemId.equals(i.getId()))) {
throw new BadRequestException("12345","Item Not Found");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With