A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/718 below:

[java] BrokenNullCheck false positive with parameter/field confusion · Issue #718 · pmd/pmd · GitHub

Rule: BrokenNullCheck

Description:

The BrokenNullCheck of the basic rule set does not work as expected.
If I separate the condition of the if into separate statements the test succeeds.

The code sample below. The function test1 and test2 handle the same check. In test1 boolean variables are created in test2 there are no local variables created but we use the same statement in braces.

Code Sample demonstrating the issue:

public class PmdTest {

    public static boolean test1(final boolean hasFoo, final boolean hasBar, final Object foo, final Object bar) {
        final boolean hasFooObject = foo != null;
        final boolean hasBarObject = bar != null;
        if (hasFoo == hasFooObject && hasBar == hasBarObject) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean test2(final boolean hasFoo, final boolean hasBar, final Object foo, final Object bar) {
        if (hasFoo == (foo != null) && hasBar == (bar != null)) {
            return true;
        } else {
            return false;
        }
    }
}

Running PMD through:

CLI

The error has been previously detected by a project using Maven build system. I created a small example and use the command line, so it could be tested easily.

Command line:

pmd-bin-5.8.1/bin/run.sh pmd -d PmdTest.java -f xml -l java -R rulesets/java/basic.xml

Output:

<?xml version="1.0" encoding="UTF-8"?>
<pmd version="5.8.1" timestamp="2017-11-07T14:09:02.275">
<file name="/home/rathgeb/tmp/pmd/PmdTest.java">
<violation beginline="14" endline="18" begincolumn="9" endcolumn="9" rule="BrokenNullCheck" ruleset="Basic" class="PmdTest" method="test2" externalInfoUrl="https://pmd.github.io/pmd-5.8.1/pmd-java/rules/java/basic.html#BrokenNullCheck" priority="2">
Method call on object which may be null
</violation>
</file>
</pmd>

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4