Skip to content

[java] LiteralsFirstInComparisonsRule not applied on constant #5590

@Pankraz76

Description

@Pankraz76

Affects PMD Version: pmd:3.26.0

Rule: LiteralsFirstInComparisonsRule

Please provide the rule name and a link to the rule documentation:

https://pmd.github.io/pmd/pmd_rules_java_bestpractices.html#literalsfirstincomparisons

Description:

It is not found on the constant.
Is this indented correctly?
After briefly reviewing the code, I don't see any filtering, so this might be accidental.
A reference is always a potential NPE, in my opinion.
Therefore, the raw string literal should always come first.

Code Sample demonstrating the issue:

    static class Foo {
        private static final String FOO = null;

        public static void bar(String _null) {
            String _null2 = null;
            FOO.equals("RAW"); // not reported -> false-negative?
            _null.equals("RAW"); // is reported
            _null2.equals("RAW"); // is reported
        }
    }
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-negativePMD doesn't flag a problematic piece of code

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions