Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullness checker incorrectly marking possible null on compound if/else condition #5016

Closed
jpschewe opened this issue Jan 22, 2022 · 2 comments

Comments

@jpschewe
Copy link

In this code, it's not possible for thisLastEdited or otherLastEdited to be null in the else block. This is a pretty common way to check for nulls in comparisons.

I didn't put this on #4872 because that one is accessing local class state, so another thread may made the class variable null between statements. Here I have stored a local reference to ensure that this can't happen.

package net.mtu.eggplant.checker;

import java.time.LocalTime;

import org.checkerframework.checker.nullness.qual.Nullable;

public class IfElseNullable {

  private int compareLastEdited(final @Nullable LocalTime thisLastEdited, final @Nullable LocalTime otherLastEdited) {
    if (null != thisLastEdited
        && null == otherLastEdited) {
      return -1;
    } else if (null == thisLastEdited
               && null != otherLastEdited) {
      return 1;
    } else if (null == thisLastEdited
               && null == otherLastEdited) {
      return 0;
    } else {
      return thisLastEdited.compareTo(otherLastEdited);
    }
  }

}

These are the outputs of the checker framework.

/home/jpschewe/projects/checker-bugs/src/main/java/net/mtu/eggplant/checker/IfElseNullable.java:20: error: [dereference.of.nullable] dereference of possibly-null reference thisLastEdited
      return thisLastEdited.compareTo(otherLastEdited);
             ^
/home/jpschewe/projects/checker-bugs/src/main/java/net/mtu/eggplant/checker/IfElseNullable.java:20: error: [argument] incompatible argument for parameter other of compareTo.
      return thisLastEdited.compareTo(otherLastEdited);
                                      ^
  found   : @Initialized @Nullable LocalTime
  required: @Initialized @NonNull LocalTime

This output was created using checker framework version 3.21.1.

@MdSahil-oss
Copy link

/assign

@mernst mernst changed the title Nullness checker incorrectly marking possible null on if/else condition Nullness checker incorrectly marking possible null on compound if/else condition Jan 23, 2022
@mernst
Copy link
Member

mernst commented Jan 23, 2022

Thanks for reporting this problem. You are right that the Nullness Checker does not track arbitrary boolean conditions (not even ones as simple as a conjunction). Thus, the code that you wrote is beyond the capabilities of its simple brain.

Here is a variant that the Nullness Checker does verify:

import java.time.LocalTime;
import org.checkerframework.checker.nullness.qual.Nullable;

public class IfElseNullable {

  private int compareLastEdited(
      final @Nullable LocalTime thisLastEdited, final @Nullable LocalTime otherLastEdited) {
    if (null == thisLastEdited) {
      if (null != otherLastEdited) {
        return 1;
      } else {
        // null == otherLastEdited
        return 0;
      }
    } else {
      // null != thisLastEdited
      if (null == otherLastEdited) {
        return -1;
      } else {
        return thisLastEdited.compareTo(otherLastEdited);
      }
    }
  }
}

It differs in that each if condition is a single boolean expression rather than a compound one with &&.

There are pros and cons of the two different ways of writing the code, and I can see why you would prefer your version.

It would be possible to change the Checker Framework to track more complex boolean conditions, which would enable it to handle your version. This would take extra code and would have a run-time cost, so unfortunately it is not high on our priority list (though we would be happy to review such a code contribution).

@mernst mernst closed this as completed Jan 23, 2022
jpschewe added a commit to jpschewe/fll-sw that referenced this issue Jan 23, 2022
According to
typetools/checker-framework#5016 compound
conditions are not handled by checker.
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

No branches or pull requests

3 participants