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] UnusedLocalVariable false positive in dynamic SOQL · Issue #2669 · pmd/pmd

[apex] UnusedLocalVariable false positive in dynamic SOQL #2669

New issue
Copy link
New issue
Copy link
Closed
#4110
Closed
[apex] UnusedLocalVariable false positive in dynamic SOQL#2669
#4110
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematic
Milestone
6.54.0

Description

@ghost
ghost
opened on 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

Activity

  1. ghost added
    a:bugPMD crashes or fails to analyse a file.
    on Jul 27, 2020
  2. savithraj commented on Aug 7, 2020

    @savithraj
    savithraj
    on Aug 7, 2020
    More actions

    I am also facing same issue.

  3. adangel
    added
    a:false-positivePMD flags a piece of code that is not problematic
    and removed
    a:bugPMD crashes or fails to analyse a file.
    on Aug 23, 2020
  4. adangel
    added a commit that references this issue on Jan 23, 2023

    [apex] Enhance UnusedLocalVariable to consider SOQL binding vars

    This issue will close once commit a03a55b is merged into the 'master' branch.
    a03a55b
  5. adangel
    added this to the 6.54.0 milestone on Jan 23, 2023
  6. adangel
    closed this as completedin a03a55bon Jan 26, 2023
  7. adangel
    added a commit that references this issue on Jan 26, 2023

    [doc] Update release notes (pmd#2669, pmd#4110)

    aa24ae7
  8. tprouvot commented on Feb 6, 2023

    @tprouvot
    tprouvot
    on Feb 6, 2023
    Contributor
    More actions

    @adangel thanks a lot for this one !

    Reacted by Andreas Dangel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

      Milestone

      • 6.54.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.