Skip to content

[java] UnconditionalIfStatement: False negative for negated boolean constant #6435

Closed

Description

@leemeii

Affects PMD Version: 7.20.0

Rule: UnconditionalIfStatement
https://pmd.github.io/pmd/pmd_rules_java_errorprone.html#unconditionalifstatement

Description:
UnconditionalIfStatement fails to detect cases where the condition is an effectively constant boolean expression, such as a UnaryExpression involving a boolean literal (e.g., if (!false)).

In the AST, !false is parsed as a UnaryExpression containing a BooleanLiteral, which does not satisfy the current [BooleanLiteral[1]] predicate. This allows unconditional code blocks to bypass static analysis simply by adding a logical NOT operator.

Code Sample demonstrating the issue:

public class Bug {
    public void method() {
        
        if (!false) { //<-should report (FN)
            int i = 1;
        }
    }
}

Expected outcome:

PMD should report a violation at line 4, but doesn't. This is a false-negative. The rule should be updated to account for simple constant expressions or at least unary-negated literals.

Actual outcome
Line 4 is ignored.

Running PMD through: CLI

Activity

  1. added
    a:false-negative
    on Feb 1, 2026
  2. changed the title [-]UnconditionalIfStatement: False negative when boolean literal is wrapped in UnaryExpression[/-] [+][java] UnconditionalIfStatement: False negative when boolean literal is wrapped in UnaryExpression[/+] on Feb 16, 2026
  3. changed the title [-][java] UnconditionalIfStatement: False negative when boolean literal is wrapped in UnaryExpression[/-] [+][java] UnconditionalIfStatement: False negative for negated boolean expression[/+] on Feb 16, 2026
  4. changed the title [-][java] UnconditionalIfStatement: False negative for negated boolean expression[/-] [+][java] UnconditionalIfStatement: False negative for negated boolean constant[/+] on Feb 16, 2026
  5. adangel commented on Feb 16, 2026

    @adangel
    Member

    Thanks for reporting.

    bypass static analysis simply by adding a logical NOT operator.

    By adding single not, you would change the semantic. But the easier way to bypass this rule is !!true ...

  6. added a commit that references this issue on Aug 11, 2026
    14cb000
  7. added this to the 7.27.0 milestone on Aug 11, 2026
  8. added a commit that references this issue on Aug 11, 2026
    eb9dc7d
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

    a:false-negative

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions