Repository navigation
Conversation
…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
left a comment
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
| 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) { |
There was a problem hiding this comment.
| 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 && conditions #6513</description> | ||
| <expected-problems>2</expected-problems> |
There was a problem hiding this comment.
| <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) { |
There was a problem hiding this comment.
This should also be a violation, shouldn't it? obj != null can be dropped
| } | ||
|
|
||
| // With parentheses | ||
| if ((s != null && s2 != null) && s instanceof String) { |
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
| 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)) { |
There was a problem hiding this comment.
| 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> |
There was a problem hiding this comment.
| <expected-problems>2</expected-problems> | |
| <expected-problems>2</expected-problems> | |
| <expected-linenumbers>7,12</expected-linenumbers> |
| // 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 | ||
| } | ||
| } |
There was a problem hiding this comment.
Yes, we should report that s !== null is unnecessary. Either remove this example or adjust it, e.g.:
| // 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); | |
| } | |
| } |
|
Closing this because of lack of response. Feel free to reopen. |
|
Thanks for the report. I can confirm this is a valid false negative. The current implementation of x != null && x instanceof StringWhen another condition appears in the same x != null && otherCondition && x instanceof Stringthe rule no longer sees the null-check as the direct sibling of the The expected behavior makes sense here: as long as the earlier null-check refers to the same stable expression used as the env != null && env.getContext().getContextObject() instanceof StringI'll cover this with regression tests for the reported |
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
Testing