GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [apex] New Rule: Unused local variables by gwilymatgearset · Pull Request #2395 · pmd/pmd

Conversation

@gwilymatgearset

@gwilymatgearset gwilymatgearset commented Apr 1, 2020 •
edited by adangel
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

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
All reactions
  • 🎉 1 reaction
gwilymatgearset added 5 commits March 30, 2020 10:54
@gwilymatgearset
Basic implementation of detecting apex unused local variables
d95763c
@gwilymatgearset
Add rule to bestpractices.xml
29cef7f
@gwilymatgearset
Add tests and fix up issues found while writing them
80c26bf
@gwilymatgearset
Fixup checkstyle issues
705fa25
@gwilymatgearset
Add another test for variable usage in blocks
abbe46c
@gwilymatgearset gwilymatgearset assigned rsoesemann Apr 1, 2020
@ghost

ghost commented Apr 1, 2020

Copy link
Copy Markdown
1 Message
📖 No java rules are changed!

Generated by 🚫 Danger

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gwilymatgearset
Include variable name in violation message
41f6a59
@adangel adangel changed the title [apex] Variables that are only ever assigned but never otherwise used [apex] New Rule: Unused local variables Apr 3, 2020
@adangel adangel added the a:new-rule Proposal to add a new built-in rule label Apr 3, 2020
@adangel

adangel commented Apr 3, 2020

Copy link
Copy Markdown
Member

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...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@adangel adangel added this to the 6.23.0 milestone Apr 3, 2020
@gwilymatgearset
Make UnusedLocalVariableRule a rule chain rule
7b06d30
@gwilymatgearset
gwilymatgearset marked this pull request as ready for review April 3, 2020 14:28
@gwilymatgearset

gwilymatgearset commented Apr 3, 2020

Copy link
Copy Markdown
Collaborator Author

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 😄.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rsoesemann

rsoesemann commented Apr 3, 2020

Copy link
Copy Markdown
Member

@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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@adangel adangel self-assigned this Apr 4, 2020
adangel
adangel approved these changes Apr 4, 2020
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

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.

All reactions
adangel added a commit that referenced this pull request Apr 4, 2020
@adangel
[doc] Update release notes, refs #2395
cdbb457
adangel added a commit that referenced this pull request Apr 4, 2020
@adangel
Merge branch 'pr-2395'
a86e172
[apex] New Rule: Unused local variables #2395
@adangel
adangel merged commit 7b06d30 into pmd:master Apr 4, 2020
@gwilymatgearset
gwilymatgearset deleted the apex-unused-local-variable branch April 6, 2020 07:54
@rsoesemann rsoesemann mentioned this pull request May 6, 2020
[apex] Unused Local Variable fails on blocks #2468
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@adangel adangel adangel approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@adangel adangel

@rsoesemann rsoesemann

Labels

a:new-rule Proposal to add a new built-in rule

Projects

None yet

Milestone

6.23.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@gwilymatgearset @adangel @rsoesemann
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.