GeezerVison is showing github.com — [apex] New Rule: Unused local variables by gwilymatgearset · Pull Request #2395 · pmd/pmd
Conversation
gwilymatgearset
commented
Apr 1, 2020
•
edited by adangel
Loading
Uh oh!
There was an error while loading. Please reload this page.
Before submitting a PR, please check that:
- The PR is submitted against
master. The PMD team will merge back to support branches as needed.
-
./mvnw clean verify passes. This will build and test PMD, execute PMD and checkstyle rules. Check this for more info
PR Description:
Variables that are only ever assigned but never otherwise used.
This was discussed in #2322
Adds a basic implementation of detecting variables that are assigned but not used. I believe it does not get any false positives, just false negatives in the case of variable shadowing. I'm not sure if that is acceptable so I've left this as a draft for now until I get an opinion on that.
A way to fix shadowing issue would be to make sure the parser's variable resolver runs so we can work out which variables are the same. This information would probably be helpful for other rules, so it may be worth doing as a separate PR prior to this going in?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
rsoesemann reacted with hooray emoji
|
1 Message
|
| 📖 |
No java rules are changed! |
Generated by 🚫 Danger
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
adangel
changed the title
[apex] Variables that are only ever assigned but never otherwise used
[apex] New Rule: Unused local variables
Apr 3, 2020
Adds a basic implementation of detecting variables that are assigned but not used. I believe it does not get any false positives, just false negatives in the case of variable shadowing. I'm not sure if that is acceptable so I've left this as a draft for now until I get an opinion on that.
I'd say, we should start simple. And once that is done (we looks like that is already the case), we can further try to improve the rule.
A way to fix shadowing issue would be to make sure the parser's variable resolver runs so we can work out which variables are the same. This information would probably be helpful for other rules, so it may be worth doing as a separate PR prior to this going in?
That's an interesting idea. This definitively needs to be a separate PR. I would be ok, if this PR is merged first, then the apex parser/compiler is improved, and after that this rule is improved in one another PR...
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
That's an interesting idea. This definitively needs to be a separate PR. I would be ok, if this PR is merged first, then the apex parser/compiler is improved, and after that this rule is improved in one another PR...
Cool. Was just checking that you were happy with that order 😄.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@gwilymatgearset Thanks again for contributing a much needed rule idea. I also like this rule even if many cases are covered. Starting small is always better.
My impression is that you should add some test that show unused rules in multiple cases. For safety if we make the code more complicated later. You should really check if unused variables are found deteced in different circumstances. Besides that I am happy with that.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Looks good, thanks!
I'll add one test case for shadowing a field. If a local var has the same name as a field and the field is used, the unused local var is still detected by this rule (so, no false-negative).
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
adangel
adangel approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|