Skip to content

[apex] bad support for multiline SOQL in cpd #838

Open

Description

@Bartheleway

PMD Version: 6.0.0

Description: When you write your SOQL on multiple lines and running the cpd command, it will log false positive of duplicate code.

Code Sample demonstrating the issue:

List<Account> accountList = [
    SELECT
        Id,
        Name,
        Site
    FROM
        Account
    WHERE
        Id IN :myIdList
];
List<Account> accountList2 = [
    SELECT
        Id,
        Name,
        Site
    FROM
        Account
    WHERE
        CreatedDate < TODAY
];

Running PMD through: cmd

PMD Result:

Found a 8 line (x tokens) duplication in the following files:
Starting at line 2 of myFile.cls
Starting at line 12 of myFile.cls

    List<Account> accountList2 = [
        SELECT
            Id,
            Name,
            Site
        FROM
            Account
        WHERE

NB: if you run my example as it is, you won't get my result because I minimized the number of selected fields for the example. I think it shouldn't evaluate each line but the SOQL as a whole.
And also my code is totally stupid and only made for the example (if you wonder why).

Activity

  1. jsotuyod commented on Jan 10, 2018

    @jsotuyod
    Member

    Interesting... we are using the ApexLexer from jorje, so they are the ones splitting the SOQL into tokens... we may rebuild it as a single "token" for the whole SOQL on top of that, but we'd have to evaluate if this approach doesn't introduce any unwanted byproducts...

  2. added
    in:cpd
    on Jan 9, 2019
  3. added
    needs:pmd7-revalidation
    on May 3, 2024
  4. adangel commented on Jan 29, 2026

    @adangel
    Member

    Can still be reproduced with 7.21.0.

  5. removed
    needs:pmd7-revalidation
    on Jan 29, 2026
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

    in:cpd

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions