-
Notifications
You must be signed in to change notification settings - Fork 745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add more Optional methods to ReturnValueIgnored #2282
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this!
We'll probably need to put this behind a flag initially to avoid making an atomic breaking change to an existing error. If you don't mind doing that there's an example in 86f8e23, otherwise I can take care of it when I import the PR.
instanceMethod().onExactClass("java.util.Optional").named("isPresent"), | ||
instanceMethod().onExactClass("java.util.Optional").named("isEmpty")); | ||
instanceMethod().onExactClass("java.util.Optional").named("map"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI we have a separate check just for this one, with a fix that migrates to ifPresent
: https://github.com/google/error-prone/blob/master/core/src/main/java/com/google/errorprone/bugpatterns/OptionalMapUnusedValue.java. We were planning to add map
here after that cleanup was done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want me to remove map
from here for now?
core/src/main/java/com/google/errorprone/bugpatterns/ReturnValueIgnored.java
Outdated
Show resolved
Hide resolved
instanceMethod().onExactClass("java.util.Optional").named("or"), | ||
instanceMethod().onExactClass("java.util.Optional").named("orElse"), | ||
instanceMethod().onExactClass("java.util.Optional").named("orElseGet"), | ||
instanceMethod().onExactClass("java.util.Optional").named("orElseThrow")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling orElseThrow
to assert an Optional
is present and then discarding the return result appears to be more common than the other examples here. I'm not really defending that as a pattern, just saying that making this one an error maybe not be lower priority for us. I'm curious if you've run into examples of that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Occasionally, but rarely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a reference, SonarLint requires that return values are used for all Optional
methods that return non-void values.
https://github.com/SonarSource/sonar-java/blob/d59d157c852ac924f3029ddb8c0aa366501f9fda/java-checks/src/main/java/org/sonar/java/checks/IgnoredReturnValueCheck.java#L68
Maybe we should do the same here?
What do you think about matching all non-void instance methods like we do for |
That SGTM, maybe as a follow-up once the flag can be removed? |
The return values of almost all
Optional
instance methods, excludingifPresent
andifPresentOrElse
, should have their return values used.For cases where you may don't care about the return value, it's better to use
isPresent
andisEmpty
. For example, instead of:Do:
As a motivating example, I had some code that was intended to look like:
But I forgot the
return
, so the code actually looked like:Detecting the unused
Optional
value here would have been useful.