-
Notifications
You must be signed in to change notification settings - Fork 742
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more Optional methods to ReturnValueIgnored
The return values of almost all `Optional` instance methods, excluding `ifPresent` and `ifPresentOrElse`, should have their return values used. For cases where you may don't care about the return value, it's better to use `isPresent` and `isEmpty`. For example, instead of: ```java optional.orElseThrow(() -> ...); ``` Do: ```java if (optional.isEmpty()) { throw ...; } ``` --- As a motivating example, I had some code that was intended to look like: ```java if (condition) { return getOptional().orElseThrow(...); } return ...; ``` But I forgot the `return`, so the code actually looked like: ```java if (condition) { getOptional().orElseThrow(...); } return ...; ``` Detecting the unused `Optional` value here would have been useful. Fixes #2282 COPYBARA_INTEGRATE_REVIEW=#2282 from pkoenig10:optional 6fec586 PiperOrigin-RevId: 369581827
- Loading branch information
Showing
2 changed files
with
87 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters