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 FP with binds in SOSL / SOQL #5000

Open
pablogomez2197 opened this issue May 9, 2024 · 0 comments · May be fixed by #5037
Open

[apex] UnusedLocalVariable FP with binds in SOSL / SOQL #5000

pablogomez2197 opened this issue May 9, 2024 · 0 comments · May be fixed by #5037
Assignees
Labels
a:false-positive PMD flags a piece of code that is not problematic

Comments

@pablogomez2197
Copy link

pablogomez2197 commented May 9, 2024

Affects PMD Version:
version 7.1.0 and 7.0.0

Rule:
UnusedLocalVariable

Description:
The rule UnusedLocalVariable doesn't detect the binding of a variable on a String Query. See Salesforce's docs: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_variables.htm

Code Sample demonstrating the issue:

  SObject record = this.getOldRecords()[0];
  **List<SObject> objectsToQuery = this.getOldRecords();**

  SObjectType sObjectType = record.getSObjectType();
  
  String[] relatedObjectData = RELATED_RECORD_DATA_BY_OBJECT.get(sObjectType).split('::');
  
  String relatedObjectName = relatedObjectData[0];
  String relatedFieldName = relatedObjectData[1];

  String query = 'SELECT ' + String.escapeSingleQuotes(relatedFieldName) + ' FROM ' + **String.escapeSingleQuotes(relatedObjectName) + ' WHERE ' + String.escapeSingleQuotes(relatedFieldName) + ' IN :objectsToQuery';**
  
  for(sObject relatedRecord : (Database.query(query))){
    this.withRelatedRecords.add((String)relatedRecord.get(relatedFieldName));
  }

Expected outcome:
The variable is used in the query.
In the line 2 List<SObject> objectsToQuery = this.getOldRecords(); and this variable is used in the line String.escapeSingleQuotes(relatedObjectName) + ' WHERE ' + String.escapeSingleQuotes(relatedFieldName) + ' IN :objectsToQuery';

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]

Thank you in advance!

@pablogomez2197 pablogomez2197 added the a:false-positive PMD flags a piece of code that is not problematic label May 9, 2024
@jsotuyod jsotuyod changed the title UnusedLocalVariable error [apex] UnusedLocalVariable error May 9, 2024
@jsotuyod jsotuyod changed the title [apex] UnusedLocalVariable error [apex] UnusedLocalVariable FP with binds in SOSL / SOQL May 9, 2024
@adangel adangel self-assigned this May 23, 2024
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.

2 participants