pmd / pmd

An extensible multilanguage static code analyzer.
https://pmd.github.io
Other
4.84k stars 1.5k forks source link

[java] NullAssignment - false positive inside if statement #1050

Open adangel opened 6 years ago

adangel commented 6 years ago

Affects PMD Version: 6.3.0+, 7.0.0

Rule: NullAssignment

Description: If there is a variable declaration without initializer, then the first assignment with null is considered already as a NullAssignment. This should be ignored, since it is the first assignment, which should be allowed by the rule.

Code Sample demonstrating the issue:

String key;
if (a) {
    key = "a";
} else if (b) {
    key = "b";
} else {
    key = null;
}

See #629 for the original issue.

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]

boris-petrov commented 6 years ago

I think I've found another false-positive for this check (PMD version 6.6.0):

return new Pair<>(found ? a : null, found ? b : null);

I think this also could be considered a bug in PMD, what do you think?