[java] CloseResource - Fix #2764: False-negative when re-assigning variable#2811
Conversation
Generated by 🚫 Danger |
adangel
left a comment
There was a problem hiding this comment.
@andipabst
Now that the regression tester ran, there are some problems, see https://chunk.io/pmd/29b0228064074bb390f40398e6069462/diff/spring-framework/index.html
- There are two NPEs:
java.lang.NullPointerException
at net.sourceforge.pmd.lang.java.rule.errorprone.CloseResourceRule.isAssignmentForVariable(CloseResourceRule.java:733)
at net.sourceforge.pmd.lang.java.rule.errorprone.CloseResourceRule.isReassignedBeforeBeingClosed(CloseResourceRule.java:690)
at net.sourceforge.pmd.lang.java.rule.errorprone.CloseResourceRule.checkForResources(CloseResourceRule.java:172)
at net.sourceforge.pmd.lang.java.rule.errorprone.CloseResourceRule.visit(CloseResourceRule.java:156)
and
java.lang.NullPointerException
at net.sourceforge.pmd.lang.java.rule.errorprone.CloseResourceRule.isAssignmentForVariable(CloseResourceRule.java:735)
at net.sourceforge.pmd.lang.java.rule.errorprone.CloseResourceRule.isReassignedBeforeBeingClosed(CloseResourceRule.java:690)
at net.sourceforge.pmd.lang.java.rule.errorprone.CloseResourceRule.checkForResources(CloseResourceRule.java:172)
at net.sourceforge.pmd.lang.java.rule.errorprone.CloseResourceRule.visit(CloseResourceRule.java:156)
-
This might explain the false negatives in FastByteArrayOutputStreamTests.java
-
We seem to report now the initial assignment of the local var wrongly as a reassignment, e.g. https://chunk.io/pmd/29b0228064074bb390f40398e6069462/diff/spring-framework/index.html#A1
Could you please have another look? Thanks!
|
@adangel Thanks for showing me this. I added a check for null, but I am not sure if this fixes the regression, since I can not reproduce it in a unit test. Can you please tell me how I can run the regression test locally for myself? |
|
@adangel Thank you, I did not notice that the top comment of the pmd-test bot was updated with the new result, my bad! I had missed another null check, but could find it, now the regression test is producing no more errors. I also changed the location of the violation. Now the error gets flagged on the reassignment instead of the initial declaration. |
Describe the PR
This PR fixes a false-negative when re-assigning a variable without closing it before.
Related issues
Ready?
./mvnw clean verifypasses (checked automatically by travis)