Skip to content

[apex] AvoidNonRestrictiveQueries when LIMIT is followed by bind expression #5270

@thesunlover

Description

@thesunlover

Affects PMD Version:
7.6.0

Rule:

https://docs.pmd-code.org/pmd-doc-7.5.0/pmd_rules_apex_performance.html#avoidnonrestrictivequeries

Description:

When the limit is presented as the value of a constant defined in the class it is detected as a violation.

Code Sample demonstrating the issue:

public with sharing class DemoController {
    public static final Integer LIMIT_ACCOUNTS = 2;
    @AuraEnabled
    public static List<Account> getTwoAccounts() {
        List<Account> result = [
            SELECT Id,
                Name
            FROM Account
            WITH SECURITY_ENFORCED
            LIMIT: LIMIT_ACCOUNTS
        ];
        return result;
    }
}

Expected outcome:

PMD reports a violation at line 5, but that's wrong. That's a false positive.

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]
sf scanner:run --projectdir "./force-app/" --format="csv" --outfile="CodeAnalyzerGeneral.csv" --target "./**/*.cls"

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions