FP java:S2259 reports possible NPE for switch case after null case

  • SonarQube Community Build 25.11.0.114957
  • Java Code
  • Rule S2259 Null pointers should not be dereferenced

S2259 reports an issue for the the following Java code:

package org.example;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
public class SwitchWithNullCase {
  public String m(@Nullable Object o) {
    return switch (o) {
      case null -> "";
      default -> o.toString(); // FP: A "NullPointerException" could be thrown; "o" is nullable here.
    };
  }
}

I’ve attached the above source as minimal maven project.

test.zip (1.9 KB)

Cheers
Andreas

Hey there.

While I’m sure you’re already aware, for completeness, I’m linking @ganncamp’s answer on your other thread here. :slight_smile: