Skip to content

[java] Update rule MisplacedNullCheck#3545

Merged
oowekyala merged 3 commits into
pmd:pmd/7.0.xfrom
adangel:pmd7-update-MisplacedNullCheck
Oct 25, 2021
Merged

[java] Update rule MisplacedNullCheck#3545
oowekyala merged 3 commits into
pmd:pmd/7.0.xfrom
adangel:pmd7-update-MisplacedNullCheck

Conversation

@adangel

@adangel adangel commented Oct 8, 2021

Copy link
Copy Markdown
Member

Part of #2701

@adangel adangel added this to the 7.0.0 milestone Oct 8, 2021
@ghost

ghost commented Oct 8, 2021

Copy link
Copy Markdown
2 Messages
📖 Compared to pmd/7.0.x:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report
📖 Compared to master:
This changeset changes 31211 violations,
introduces 21309 new violations, 1 new errors and 0 new configuration errors,
removes 135826 violations, 8 errors and 3 configuration errors.
Full report
Compared to pmd/7.0.x:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
[Full report](<html>

<title>504 Gateway Time-out</title>

504 Gateway Time-out


nginx

</html>/diff1/index.html)

Compared to master:
This changeset changes 31211 violations,
introduces 21309 new violations, 1 new errors and 0 new configuration errors,
removes 135826 violations, 8 errors and 3 configuration errors.
[Full report](<html>

<title>504 Gateway Time-out</title>

504 Gateway Time-out


nginx

</html>/diff2/index.html)

Compared to pmd/7.0.x:
This changeset changes 0 violations,
introduces 398 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report
Compared to master:
This changeset changes 31211 violations,
introduces 21331 new violations, 1 new errors and 0 new configuration errors,
removes 136054 violations, 8 errors and 3 configuration errors.
Full report

Generated by 🚫 Danger

@adangel

adangel commented Oct 15, 2021

Copy link
Copy Markdown
Member Author

There are a couple of new false positives:


https://github.com/openjdk/jdk/blob/jdk-11%2B28/src/java.base/share/classes/com/sun/java/util/jar/pack/ConstantPool.java#L1163

                Entry e1 = indexKey[probe];
                if (e1 == e || e1 == null)
                    return probe;

Message: "The null check here is misplaced; if the variable 'e1' is null there will be a NullPointerException"

There will be no NPE - this code is fine.


https://github.com/openjdk/jdk/tree/jdk-11+28/src/java.base/share/classes/com/sun/java/util/jar/pack/PopulationCoding.java#L432

int UDef = (unfavoredCoding == dflt || unfavoredCoding == null)?1:0;

Message: "The null check here is misplaced; if the variable 'unfavoredCoding' is null there will be a NullPointerException"

That's wrong - there will be no NPE.


https://github.com/openjdk/jdk/tree/jdk-11+28/src/java.base/share/classes/java/io/ObjectOutputStream.java#L1146

if (rep != obj && rep != null) {

Message: The null check here is misplaced; if the variable 'rep' is null there will be a NullPointerException


https://github.com/openjdk/jdk/tree/jdk-11+28/src/java.base/share/classes/java/lang/Class.java#L1522

if (enclosingClass == this || enclosingClass == null)

Message: The null check here is misplaced; if the variable 'enclosingClass' is null there will be a NullPointerException


https://github.com/openjdk/jdk/tree/jdk-11+28/src/java.base/share/classes/java/util/concurrent/locks/AbstractQueuedLongSynchronizer.java#L224

for (Node p = tail; p != node && p != null; p = p.prev)

Message: "The null check here is misplaced; if the variable 'p' is null there will be a NullPointerException"


https://github.com/openjdk/jdk/tree/jdk-11+28/src/java.base/share/classes/java/util/TimeZone.java#L560

if (defaultZone != this &&
    defaultZone != null && id.equals(defaultZone.getID())) {

Message: "The null check here is misplaced; if the variable 'defaultZone' is null there will be a NullPointerException"


https://github.com/openjdk/jdk/tree/jdk-11+28/src/java.base/share/classes/sun/invoke/util/VerifyAccess.java#L369

if (loader1 == loader2 || loader1 == null
        || (loader2 == null && !loader1MustBeParent)) {

Message: "The null check here is misplaced; if the variable 'loader1' is null there will be a NullPointerException"


https://github.com/openjdk/jdk/tree/jdk-11+28/src/java.base/share/classes/sun/security/util/CurveDB.java#L87

if ((params instanceof NamedCurve) || (params == null)) {

Message: "The null check here is misplaced; if the variable 'params' is null there will be a NullPointerException"


https://github.com/spring-projects/spring-framework/tree/v5.0.6.RELEASE/spring-core/src/main/java/org/springframework/util/ReflectionUtils.java#L99

while (Object.class != searchType && searchType != null) {

Message: "The null check here is misplaced; if the variable 'searchType' is null there will be a NullPointerException"


/@Image
satisfies starts-with($var, concat(PrimaryExpression/PrimaryPrefix/Name/@Image, '.'))]
/PrimaryExpression/PrimaryPrefix/Name
//InfixExpression[@Operator = '&&']

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 think this rule might have been easier to write in Java. There are already utilities for null checks in JavaRuleUtil. But well, this works, so let's keep it.

@oowekyala oowekyala self-assigned this Oct 25, 2021
@oowekyala oowekyala merged commit 38ea6c3 into pmd:pmd/7.0.x Oct 25, 2021
@adangel adangel deleted the pmd7-update-MisplacedNullCheck branch October 28, 2021 13:22
@adangel adangel mentioned this pull request Jan 23, 2023
55 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants