S2637 triggers on Java primitives

Version used: * Community Edition Version 9.0.1 (build 46107)

We are getting hits on S2637 “@NonNull” values should not be set to null in code like this:

package simple;

import org.eclipse.jdt.annotation.NonNullByDefault;

@NonNullByDefault
class Example {
  boolean falseByDefault;
  Boolean nullByDefault;

  int zeroByDefault;
  Integer nullByDefault2;

  public Example() {
    nullByDefault = false;
    nullByDefault2 = 200;
  }
}

SonarQube reports that falseByDefault is not initialized in the constructor. But the Java specification says that falseByDefault is indeed… false by default. So how should it be able to be null? I don’t get it.

Now, some might say that this is “bad code”, but if that is the case this should be a code smell. It is as far as I understand it not a @NonNull violation.

Not sure what the data point is worth, but Eclipse Java NonNull detection does not complain.

/Andreas

Hello @Andreas_Magnusson

I agree with you, this case should not be reported by this rule. Ticket created to improve the situation: SONARJAVA-4025.

Thanks for taking the time to report this problem with a clear reproducer.

Best,
Quentin

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.