Skip to content

[java] SimplifyConditional: False negative when null check and instanceof are separated by other && conditions #6513

Closed

Description

@leemeii

Affects PMD Version: 7.22.0

Rule:SimplifyConditional

Description:
The SimplifyConditional rule only detects the pattern x != null && x instanceof Type when the null check is directly adjacent to the instanceof expression.
When additional conditions are inserted in the && chain, the rule fails to trigger even though the null check before instanceof remains completely redundant (semantically identical after removal).
This is very common in real-world code, where developers frequently insert additional non-null checks or other conditions in the middle of the && chain .

Code Sample demonstrating the issue:

public class a{
    public void foo(){
        String s = "a";
        if( s!=null&&s instanceof String){   // Scenario A: Direct adjacent case (correctly reported - TP)
            System.out.println(s);
        }

        String s2="a";
        if(s!=null && s2!=null && s instanceof String){   // Scenario B: Intervening condition (False Negative)
            System.out.println(s);
        }
    }
}

Expected outcome:

PMD should report a violation at line 10 with the message:
"No need to check for null before an instanceof"
But currently it only reports the first if, completely missing the second one. This is a false-negative.

Running PMD through: CLI

Activity

  1. added
    a:false-negative
    on Mar 20, 2026
  2. added a commit that references this issue on Mar 30, 2026
    5bda90a
  3. added a commit that references this issue on Aug 12, 2026
    1b55586
  4. added 3 commits that reference this issue on Aug 22, 2026
    439ad33
    9ba5e99
    aa95d35
  5. added this to the 7.28.0 milestone on Sep 12, 2026
  6. added a commit that references this issue on Sep 12, 2026
    dd5b3e5
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