Skip to content

[java] UnconditionalIfStatement: False negative when true/false is not literal but local variable #5882

@csrma

Description

@csrma

Affects PMD Version: 7.15.0

Rule: UnconditionalIfStatement

Description: Currently the rule can only detect literal if (false)/if (true). However, in real code it is more likely that the if condition checks a variable or method return value that is always true/false instead of having literal true/false inside if.

Update: Removed "method return value", as the method might do some real work which makes the condition not unconditional anymore. We don't know whether the method would return always the literal true/false or whether it takes more information into consideration.

-> The rule should only find the following example, where the local variable is effectively final and initialized with a boolean literal.

Code Sample demonstrating the issue:

class UnconditionalIfStatementBug {
    public static void main(String[] args) {
        boolean condition = true;
        if (condition) {        // should raise a flag.
            System.out.println("This will always print.");
        }
    }
}

Expected outcome:

PMD should report a violation at line 4, but doesn't. This is a false-negative.

Running PMD through: [CLI]

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