Skip to content
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

[apex] UnusedLocalVariable false positive in dynamic SOQL #2669

Closed
hudec117 opened this issue Jul 27, 2020 · 2 comments · Fixed by #4110
Closed

[apex] UnusedLocalVariable false positive in dynamic SOQL #2669

hudec117 opened this issue Jul 27, 2020 · 2 comments · Fixed by #4110
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@hudec117
Copy link

hudec117 commented Jul 27, 2020

Affects PMD Version:

6.26.0 and 6.29.0

Rule:

UnusedLocalVariable

Description:

Local variables marked as unused despite references in dynamic SOQL queries (strings).

See also Dynamic SOQL

Code Sample demonstrating the issue:

public class MyClass implements Database.Batchable<sObject> {
    public Database.QueryLocator start(Database.BatchableContext bc) {
        Date todayDate = System.today();
        String recordTypeName = 'MyRecordType';

        String query = 'SELECT Id FROM Account WHERE RecordType.DeveloperName = :recordTypeName';

        if (lastRunOn == null) {
            query += ' AND createdDate < :todayDate';
        }

        return Database.getQueryLocator(query);
    }
}
{
  "formatVersion": 0,
  "pmdVersion": "6.26.0",
  "timestamp": "2020-07-27T13:16:06.532+01:00",
  "files": [
    {
      "filename": "MyClass.cls",
      "violations": [
        {
          "beginline": 3,
          "begincolumn": 14,
          "endline": 3,
          "endcolumn": 22,
          "description": "Variable \u0027todayDate\u0027 defined but not used",
          "rule": "UnusedLocalVariable",
          "ruleset": "Best Practices",
          "priority": 1,
          "externalInfoUrl": "${pmd.website.baseurl}/pmd_rules_apex_bestpractices.html#unusedlocalvariable"
        },
        {
          "beginline": 4,
          "begincolumn": 16,
          "endline": 4,
          "endcolumn": 29,
          "description": "Variable \u0027recordTypeName\u0027 defined but not used",
          "rule": "UnusedLocalVariable",
          "ruleset": "Best Practices",
          "priority": 1,
          "externalInfoUrl": "${pmd.website.baseurl}/pmd_rules_apex_bestpractices.html#unusedlocalvariable"
        }
      ]
    }
  ],
  "suppressedViolations": [],
  "processingErrors": [],
  "configurationErrors": []
}

Expected outcome:

This is a false positive since the local variables are referenced inside the query strings. I expect no violation reported.

Running PMD through:

Ant

@hudec117 hudec117 added the a:bug PMD crashes or fails to analyse a file. label Jul 27, 2020
@savithraj
Copy link

I am also facing same issue.

@adangel adangel added a:false-positive PMD flags a piece of code that is not problematic and removed a:bug PMD crashes or fails to analyse a file. labels Aug 23, 2020
@adangel adangel added this to the 6.54.0 milestone Jan 23, 2023
adangel added a commit to adangel/pmd that referenced this issue Jan 26, 2023
@tprouvot
Copy link
Contributor

tprouvot commented Feb 6, 2023

@adangel thanks a lot for this one !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants