openrewrite / rewrite-migrate-java

OpenRewrite recipes for migrating to newer versions of Java.
Apache License 2.0
111 stars 75 forks source link

`org.openrewrite.java.migrate.util.MigrateCollectionsSingletonList` should not convert known null #571

Closed blipper closed 1 month ago

blipper commented 2 months ago

What is the smallest, simplest way to reproduce the problem?

import java.util.Collections;

class A {
    void foo( {
     Collections.singletonList(null);
    }
}

What did you expect to see?

import java.util.Collections;

class A {
    void foo( {
     Collections.singletonList(null);
    }
}

What did you see instead?


class A {
    void foo( {
     List.of(null);
    }
}

Really this should be any provably null or if conservative only provably non-null.

This is a problem because singleList allows null element. https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/List.html#of(E) https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Collections.html#singletonList(T)

Are you interested in contributing a fix to OpenRewrite?

blipper commented 2 months ago

The same problem applies to Map as well.