Skip to content

[apex] ApexCRUDViolation: False positive with Lists of Objects with getSObjectType().getDescribe() #3877

@aaronmorris

Description

@aaronmorris

Affects PMD Version:
6.42.0+
7.0.0

Rule: ApexCRUDViolation

Description:
I'm getting the "Validate CRUD permission before SOQL/DML operation" error when updating a list of a custom object, but not on the individual.

Code Sample demonstrating the issue:

public static CustomObject__c updateCustomObject(CustomObject__c customObject) {
    if (CustomObject__c.getSObjectType().getDescribe().isUpdateable() == false) {
        throw new DmlException('No permissions to update.');
    }

    // This is fine with no PMD Error
    update resultObject;
    return resultObject;
}

public static List<CustomObject__c> updateCustomObjects(List<CustomObject__c> customObjects) {
    if (CustomObject__c.getSObjectType().getDescribe().isUpdateable() == false) {
        throw new DmlException('No permissions to update.');
    }

    // Getting PMD Error: Validate CRUD permission before SOQL/DML operation
    update resultObjects;
    return resultObjects;
}

Expected outcome:
PMD reports a violation at line ..., but that's wrong. That's a false positive.
Since I've checked the object is Updateable, shouldn't that apply to individuals and lists

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]
ApexPMD Extension in VSCode - chuckjonas.apex-pmd (https://marketplace.visualstudio.com/items?itemName=chuckjonas.apex-pmd)

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematicgood first issueA great starting point for new contributors

    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