-
Notifications
You must be signed in to change notification settings - Fork 737
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
False alarm for StreamResourceLeak with method-chaining #893
Comments
Another false positive is using something like: java.nio.file.Files.walk(somePath).forEach(file -> {}); This gives a |
Actually, the warning is even produced if I inline
The suggested fix in this case is broken:
@boris-petrov In your case one could argue that is preferable to use try-with-resources to close the stream explicitly and not rely on automatic closing. |
@PhilippWendler - I'm not sure how would you use try-with-resources on what I gave as an example - |
@boris-petrov You would need to use a local variable for the stream if you want to close it explicitly. Not sure if its worth it, but it might be a reason why error-prone wants to flag code like this, whereas for the original example, the stream is actually explicitly closed and error-prone still warns about it. |
Terminal operations don't call Stream.of(1, 2, 3)
.onClose(() -> System.out.println("closed"))
.forEach(n -> System.out.println(n)); |
@michaelhixson - thanks for the example, I didn't know this and it doesn't make sense in my book. :) I also don't understand why are these streams supposed to be closed at all? |
Those streams are supposed to be
That is what this check is trying to enforce. Towards fixing this bug: error-prone/core/src/main/java/com/google/errorprone/bugpatterns/AbstractMustBeClosedChecker.java Line 80 in 083720d
|
Fixes #893 See also bazelbuild/bazel#4414 RELNOTES: N/A ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=181687024
What version of Error Prone are you using?
2.2.0
Does this issue reproduce with the latest release?
Yes.
What did you do?
Example program:
What did you expect to see?
No warning, because the method
getPaths
is annotated with@MustBeClosed
.What did you see instead?
A warning for
getPaths
:The warning goes away (as expected) if the
filter
call is removed and the result ofFiles.walk
is returned directly.The text was updated successfully, but these errors were encountered: