FP java:S4449 reports issue for null parameter for Guava Iterables.getLast

  • SonarQube Community Build v25.10.0.114319
  • Java Code
  • Rule java:S4449 Nullness of parameters should be guaranteed

S4449 reports an issue for the the following Java code:

package org.example;
import com.google.common.collect.Iterables;

import java.util.List;

public class S4449GuavaIterablesGetLast {
  Object test(List<Object> list) {
    return Iterables.getLast(list, null);
  }
}

It does not like the null parameter passed as defaultValue to Iterables#getLast of Guava version 33.5.0-jre.

The signature of the called Guava method is using org.jspecify.annotations.Nullable

  @ParametricNullness
  public static <T extends @Nullable Object> T getLast(
      Iterable<? extends T> iterable, @ParametricNullness T defaultValue) {

Because T is declared as “extends @Nullable Object”, it should be possible to pass null as defaultValue.

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

test.zip (1.9 KB)

Cheers,
Andreas

Hello Andreas!

Thank you very much for the report and especially for the well-prepared reproducer.
Indeed, I can confirm that an FP is being raised here - null should be a valid argument in this context.

I created a ticket to track the problem here: Link

Best regards,

Anton

1 Like