Skip to content

[java] Fix #6513: SimplifyConditional: Fix false negative when null check and instanceof are separated by other && conditions - #6558

Closed
mmnhgo wants to merge 2 commits into
pmd:mainfrom
mmnhgo:issue-6513
Closed

mmnhgo wants to merge 2 commits into
pmd:mainfrom
mmnhgo:issue-6513

Conversation

@mmnhgo

@mmnhgo mmnhgo commented Mar 30, 2026

Copy link
Copy Markdown

Fixes #6513

Problem

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.

Solution

This PR fixes the issue by recursively checking all operands in && and || chains for null checks on the same variable as the instanceof subject.

Changes

  1. Modified \SimplifyConditionalRule.java\ to add a helper method \containsNullCheckInChain\ that recursively traverses && and || expressions
  2. Added comprehensive test cases to cover the fix

Testing

  • Added new test cases in \SimplifyConditional.xml\ covering:
    • Multiple intervening conditions in && chains
    • Multiple intervening conditions in || chains
    • Edge cases to ensure no false positives

…tanceof are separated by other && conditions

Fixes pmd#6513

The SimplifyConditional rule only detected the pattern x != null && x instanceof Type when the null check was directly adjacent to the instanceof expression. When additional conditions were inserted in the && chain, the rule failed to trigger even though the null check before instanceof remains completely redundant.

This commit fixes the issue by recursively checking all operands in && and || chains for null checks on the same variable as the instanceof subject.
@adangel adangel changed the title [java] SimplifyConditional: Fix false negative when null check and instanceof are separated by other && conditions [java] Fix #6513: SimplifyConditional: Fix false negative when null check and instanceof are separated by other && conditions May 7, 2026
@adangel adangel added the needs:user-input Maintainers are waiting for feedback from author label May 7, 2026

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

However, it doesn't seem to be finished, as there are test failures.
Please fix these issues.

}

// Intervening condition (should also be detected now)
if (s != null && s2 != null && s instanceof String) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (s != null && s2 != null && s instanceof String) {
if (s != null && s2 != null && s instanceof String) { // <-- violation: line 12

String s2 = "a";

// Direct adjacent case (should be detected)
if (s != null && s instanceof String) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (s != null && s instanceof String) {
if (s != null && s instanceof String) { // <-- violation: line 7


<test-code>
<description>[java] SimplifyConditional: False negative when null check and instanceof are separated by other &amp;&amp; conditions #6513</description>
<expected-problems>2</expected-problems>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<expected-problems>2</expected-problems>
<expected-problems>2</expected-problems>
<expected-linenumbers>7,12</expected-linenumbers>


// Multiple intervening conditions
Object obj = new Object();
if (obj != null && s2 != null && "test" != null && obj instanceof String) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be a violation, shouldn't it? obj != null can be dropped

}

// With parentheses
if ((s != null && s2 != null) && s instanceof String) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be a violation, shouldn't it? s != null can be dropped. However, we could decide, that we won't consider parentheses - as we have https://docs.pmd-code.org/latest/pmd_rules_java_codestyle.html#uselessparentheses - and once the useless parenthesis are fixed, we would detect s != null again.

String s2 = "a";

// Direct case (should be detected)
if (s == null || !(s instanceof String)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having trouble understanding the logic of this (this is the same test case as in line 49).
Can this be actually simplified by dropping s == null?
If s is not a instance of String, then it is either null or something different...
ah, ok, I get it. Right - the extra check for s == null is not necessary.

String s2 = "a";

// Direct case (should be detected)
if (s == null || !(s instanceof String)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (s == null || !(s instanceof String)) {
if (s == null || !(s instanceof String)) { // <-- violation: line 7

}

// Intervening condition with || (should also be detected now)
if (s == null || s2 == null || !(s instanceof String)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (s == null || s2 == null || !(s instanceof String)) {
if (s == null || s2 == null || !(s instanceof String)) { // <-- violation: line 12


<test-code>
<description>[java] SimplifyConditional: False negative with || chain #6513 variant</description>
<expected-problems>2</expected-problems>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<expected-problems>2</expected-problems>
<expected-problems>2</expected-problems>
<expected-linenumbers>7,12</expected-linenumbers>

Comment on lines +226 to +231
// Complex case: null check for s2 in chain, instanceof for s - should NOT report
if (s != null && s2 != null && s instanceof String && obj != null) {
// Actually this should report for "s != null && s instanceof String"
// But the test expects 0 because we're testing the negative case
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we should report that s !== null is unnecessary. Either remove this example or adjust it, e.g.:

Suggested change
// Complex case: null check for s2 in chain, instanceof for s - should NOT report
if (s != null && s2 != null && s instanceof String && obj != null) {
// Actually this should report for "s != null && s instanceof String"
// But the test expects 0 because we're testing the negative case
}
}
// Complex case: null check for s2 in chain, instanceof for s - should NOT report
if (obj != null && s2 != null && s instanceof String) {
System.out.println(s);
}
}

@adangel

adangel commented May 14, 2026

Copy link
Copy Markdown
Member

Closing this because of lack of response. Feel free to reopen.

@adangel adangel closed this May 14, 2026
@mmnhgo

mmnhgo commented May 28, 2026

Copy link
Copy Markdown
Author

Thanks for the report. I can confirm this is a valid false negative.

The current implementation of SimplifyConditional only detects the null-check when it is directly adjacent to the instanceof expression, eg:

x != null && x instanceof String

When another condition appears in the same && chain:

x != null && otherCondition && x instanceof String

the rule no longer sees the null-check as the direct sibling of the instanceof expression, so it misses the redundant check.

The expected behavior makes sense here: as long as the earlier null-check refers to the same stable expression used as the instanceof subject, the check is redundant and should be reported. The fix should inspect the surrounding short-circuit boolean chain instead of only the adjacent operand, while preserving the existing false-positive protections for different expressions such as:

env != null && env.getContext().getContextObject() instanceof String

I'll cover this with regression tests for the reported && case, parenthesized variants, the corresponding || !(instanceof) form, and negative cases where the null-check targets a different expression.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs:user-input Maintainers are waiting for feedback from author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants