-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
[java] New rule: UnusedAssignment #2618
Merged
Merged
Conversation
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
Generated by 🚫 Danger |
oowekyala
added
the
is:WIP
For PRs that are not fully ready, or issues that are actively being tackled
label
Jun 28, 2020
Probably this is what causes DFAAR to change behavior. The current behavior is wrong (two variables are not equal just from their name, that doesn't account for whether they're declared independently or shadow a declaration). But whatever, this will be scrapped in 7.0
oowekyala
removed
the
is:WIP
For PRs that are not fully ready, or issues that are actively being tackled
label
Jun 28, 2020
adangel
reviewed
Jul 16, 2020
...ava/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedAssignmentRule.java
Show resolved
Hide resolved
adangel
reviewed
Jul 16, 2020
adangel
approved these changes
Jul 16, 2020
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.
Awesome! 🎉
I'll create a issue to deprecate the DFA rule for 6.27.0.
adangel
added a commit
that referenced
this pull request
Jul 16, 2020
[java] New rule: UnusedAssignment #2618
2 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Describe the PR
Add the rule UnusedAssignment. This reports the same thing as DataflowAnomalyAnalysisRule, but doesn't use the current data flow framework. Improvements over DFAAR:
It handles assignments in field initializers, including static initializers
this(...)
constructor call, because our overload resolution doesn't resolve them afaik. This can be solved easily in the 7.0 framework.It handles control flow caused by shortcut boolean expressions
It supports properly all features of Java 5+ (lambdas, foreach, try-with-resources, etc)
The code is manageable and can be updated for new language features. The current DFA framework is impossible to navigate and to change.
It never hangs or fail with obscure messages ([core] DFA hits a hard limit of 6 iterations without results and logs #873)
It's nearly 4 times faster, if you factor in that DFAAR needs a preprocessing step of the tree that builds "DataflowNodes"
Messages are readable (ref [java][doc] DataflowAnomalyAnalysis rule needs to be more descriptive and explanatory #2129). Compare:
to
It's very precise, in principle it makes exactly the same assumptions as the ones a java compiler takes to determine definite assigned-ness. This means it reports less things than DFAAR, but every violation is actually a violation. We can let the regression tester be the judge of that though
The rule supports
@SuppressWarnings("unused")
This rule is meant to replace DFAAR. That way, we remove the last dependency we have on our horrible data flow codebase, and can remove it in PMD 7. I think it would be more interesting to integrate a state-of-the-art data flow solver into pmd in the future, than try to reinvent the wheel (I don't plan to work on this for 7.0).
By necessity, the rule subsumes UnusedLocalVariable and UnusedFormalParameter. There's a switch to disable violations that would overlap with those rules (they're filtered out by default). But maybe this should actually replace those rules?
Related issues
Ready?
Move to bestpractices.xml maybe? It's not really "error prone"
Allow ignoring variables whose name starts with
ignore
. This is useful for exceptions, loop variables, or most importantly resources (which even if they're not explicitly used may serve a purpose)Added unit tests for fixed bug/feature
Passing all unit tests
Complete build
./mvnw clean verify
passes (checked automatically by travis)Added (in-code) documentation (if needed)