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] Feature/unused variable bind false positive with dynamic SOQL by tprouvot · Pull Request #4110 · pmd/pmd

Conversation

@tprouvot

@tprouvot tprouvot commented Sep 2, 2022 •
edited by adangel
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Describe the PR

This PR fix the false positive issue on UnusedLocalVariable rule, when variable is used as bind variable in string soql query.

Related issues

  • Fixes [apex] UnusedLocalVariable false positive in dynamic SOQL #2669
  • Refs [apex] Unused Local Variable fails on String query #3489

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

Sorry, something went wrong.

Uh oh!

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

hassanalamibmx reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@tprouvot

tprouvot commented Sep 2, 2022

Copy link
Copy Markdown
Contributor Author

Hi @adangel ! Would you have some tips on how to solve this issue ? I tried to take a look on ASTBindExpressions but not sure to fully understand how it works.
Thanks !

All reactions

Sorry, something went wrong.

Uh oh!

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

@adangel

adangel commented Sep 6, 2022

Copy link
Copy Markdown
Member

Not sure, if it's that easy. Looking at the sample code with the Rule Designer shows, that the parsed AST doesn't use ASTBindExpression. The dynamic SOQL are plain strings (ASTLiteralExpression).

I'm not familiar with Apex... how can you make dynamic SOQL to be executed? Is Database.getQueryLocator the only way?

It seems, you need to start at the end, where some string is passed to a Apex function (a MethodCallExpression), that expects a SOQL - then you know, that the passed string must be a SOQL. And from there you need to go back and find the assignment/literals that make up the SOQL-string. Then you'd need to parse the SOQL yourself to get the var names out of it.

All reactions

Sorry, something went wrong.

Uh oh!

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

@adangel adangel changed the title Feature/unused variable bind false positive [apex] Feature/unused variable bind false positive with dynamic SOQL Sep 6, 2022
@adangel
adangel marked this pull request as draft September 6, 2022 16:48
@tprouvot

tprouvot commented Sep 8, 2022

Copy link
Copy Markdown
Contributor Author

Not sure, if it's that easy. Looking at the sample code with the Rule Designer shows, that the parsed AST doesn't use ASTBindExpression. The dynamic SOQL are plain strings (ASTLiteralExpression).

I'm not familiar with Apex... how can you make dynamic SOQL to be executed? Is Database.getQueryLocator the only way?

It seems, you need to start at the end, where some string is passed to a Apex function (a MethodCallExpression), that expects a SOQL - then you know, that the passed string must be a SOQL. And from there you need to go back and find the assignment/literals that make up the SOQL-string. Then you'd need to parse the SOQL yourself to get the var names out of it.

Thanks for those guidelines, yes you're right dynamic SOQL are used with Database.getQueryLocator method.
I'll try to implement this and get back to you.

Thanks for your help !

rsoesemann reacted with eyes emoji
All reactions
  • 👀 1 reaction

Sorry, something went wrong.

Uh oh!

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

@adangel

adangel commented Oct 27, 2022

Copy link
Copy Markdown
Member

Hi @tprouvot , how is it going with this? Do you think we could try schedule it for the next release (in a month)? Please let us know if you need any help.
Cheers

rsoesemann reacted with eyes emoji
All reactions
  • 👀 1 reaction

Sorry, something went wrong.

Uh oh!

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

@adangel adangel added this to the 6.52.0 milestone Oct 27, 2022
@tprouvot

tprouvot commented Oct 28, 2022

Copy link
Copy Markdown
Contributor Author

Hi @tprouvot , how is it going with this? Do you think we could try schedule it for the next release (in a month)? Please let us know if you need any help. Cheers

Hi @adangel,
I could not work on it since I have troubles to run the designer Rule (JAVAFX missing), I'll try to find some time to work on it but I'm not sure to be on time for next release.
Do you want me to close the PR and re-open it when it's done ?

All reactions

Sorry, something went wrong.

Uh oh!

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

@adangel

adangel commented Oct 28, 2022

Copy link
Copy Markdown
Member

We can leave this PR in draft until it is ready - just wanted to hear back, whether you are still working on it.

Regarding Designer and JavaFX - did you see #4153 (reply in thread) ? Maybe this helps...

tprouvot reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@adangel

adangel commented Nov 18, 2022

Copy link
Copy Markdown
Member

Hi @tprouvot , any news on this? Can we help?

All reactions

Sorry, something went wrong.

Uh oh!

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

@adangel adangel modified the milestones: 6.52.0, 6.53.0 Nov 22, 2022
@adangel adangel modified the milestones: 6.53.0, 6.54.0 Dec 31, 2022
tprouvot and others added 2 commits January 23, 2023 14:46
@tprouvot @adangel
Add test to check if false positive is fixed
cd88b36
@adangel
[apex] Enhance UnusedLocalVariable to consider SOQL binding vars
a03a55b
Fixes pmd#2669
@adangel
adangel force-pushed the feature/unusedVariableBindFalsePositive branch from 815f665 to a03a55b Compare January 23, 2023 13:49
@adangel
adangel marked this pull request as ready for review January 23, 2023 13:49
@ghost

ghost commented Jan 23, 2023

Copy link
Copy Markdown
1 Message
📖 Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 15 errors and 7 configuration errors.
Full report

Generated by 🚫 Danger

All reactions

Sorry, something went wrong.

Uh oh!

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

@adangel
adangel merged commit a03a55b into pmd:master Jan 26, 2023
adangel added a commit to adangel/pmd that referenced this pull request Jan 26, 2023
@adangel
[doc] Update release notes (pmd#2669, pmd#4110)
aa24ae7
adangel added a commit to adangel/pmd that referenced this pull request Jan 26, 2023
@adangel
Merge pull request pmd#4110 from tprouvot:feature/unusedVariableBindF…
540a737
…alsePositive

[apex] Feature/unused variable bind false positive with dynamic SOQL pmd#4110
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

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

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

6.54.0

Development

Successfully merging this pull request may close these issues.

[apex] UnusedLocalVariable false positive in dynamic SOQL

Uh oh!

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

2 participants

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