Skip to content

[java] NullAssignment false positive #629

Closed

Description

@boris-petrov

Rule: NullAssignment (java, errorprone)

Type type = condition ? something() : null;

This, according to NullAssignment's documentation, should not give the warning but it does.

PMD version 5.8.1.

P.S. Another false positive is:

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

This also should not give the warning.

P.S. Third example:

Type result = someHash.computeIfAbsent(a, _unused -> test ? truthy : null);

Activity

  1. added
    a:false-positive
    on Sep 19, 2017
  2. jsotuyod commented on Sep 19, 2017

    @jsotuyod
    Member

    @boris-petrov thanks for the reports. I don't think we will be able to cut all of them into 6.0.0, but maybe a couple.

    The second one is probably the hardest one to tackle (requires DFA), but the others should be easy to fix shall anyone desire to collaborate an easy PR.

  3. boris-petrov commented on Dec 16, 2017

    @boris-petrov
    ContributorAuthor

    Thanks for getting out 6.0.0! Unfortunately it didn't fix any of the false-positive I've mentioned in this issue. Let's leave it open for now as a reference. Thank you so much for your hard work!

  4. adangel commented on Dec 16, 2017

    @adangel
    Member

    @boris-petrov Yes, this is expected, since we didn't fix this issue (hence it stayed open - any fixed issue is closed...)

  5. added this to the 6.1.0 milestone on Dec 16, 2017
  6. acanda commented on Feb 6, 2018

    @acanda
    Contributor

    Another most likely related variant of this false positive:

    public DateTime foo(DateTime dateTime) {
        return dateTime.getYear() < 2100 ? dateTime : null;
    }
    
  7. modified the milestones: 6.1.0, 6.2.0 on Feb 25, 2018
  8. modified the milestones: 6.2.0, 6.3.0 on Mar 26, 2018
  9. self-assigned this
    on Apr 10, 2018
  10. adangel commented on Apr 10, 2018

    @adangel
    Member

    I made the following assumptions, since we had some contradictionary statements in the source code:
    a TODO, that tells the same like this issue - and unit tests, that tell the opposite.

    1. Null assignment during initialization of a variable is allowed. This includes also a null assignment via a ternary during initialization.
    2. Null assignment after initialization (so a real assignment) should be flagged by this rule. This includes also null assignment via ternary conditions.

    Result:

    Type type = condition ? something() : null;  // <--- this is not flagged
    
    Type foo;
    foo =  condition ? something() : null;  // <--- this is a violation
    

    The second example (if, elseif, else) is not covered by this issue (or the pr #1033). If we decide to implement this, then we should create a new issue. But: Shouldn't the rule flag null assignments as long as there is a possible execution path? In that sense, I see no difference between the if/elseif/else example and ternary conditions.

    /cc @jsotuyod

  11. jsotuyod commented on Apr 10, 2018

    @jsotuyod
    Member

    @adangel I think both are ok. If the variable was final, both cases would be "the first assignment / variable initialization". I find it very confusing that 2 equivalent ways of writing the same would produce different results... it would favor more complex structure / harder to read simply to avoid the warnings.

    My view is that initializing a var to null (even if not at declaration) should be ok. Resetting a variable to null is not.

  12. added a commit that references this issue on Apr 23, 2018
    bc9b18c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

a:false-positive

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions