A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/2669 below:

[apex] UnusedLocalVariable false positive in dynamic SOQL · Issue #2669 · pmd/pmd · GitHub

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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4