rewrite-static-analysis icon indicating copy to clipboard operation
rewrite-static-analysis copied to clipboard

Add missing nullable annotation on method declaration return type

Open timtebeek opened this issue 1 year ago • 1 comments

What problem are you trying to solve?

Nullability annotations on method return types help tools and devs to better understand and reason about the code. Folks might forget to add these annotations though. A recipe could look through the body of a method and add the missing nullability annotations on the return type.

What precondition(s) should be checked before applying this recipe?

Possibly look at package level defaults, to determine if annotations should be added at all. Be mindful not to add method level nullable annotation when a the return null is in a nested lambda or similar. Possibly also include methods known to return null. JSpecify available on the classpath, or added as part of adoption.

Describe the situation before applying the recipe

class A {
    public String foo(String bar) {
        return null;
    }
}

Describe the situation after applying the recipe

import org.jspecify.annotations.Nullable;
class A {
    @Nullable public String foo(String bar) {
        return null;
    }
}

Have you considered any alternatives or workarounds?

There are other tools in this space; could help to look at their specifics.

Any additional context

  • https://github.com/jspecify/jspecify/issues/553
  • https://github.com/google/error-prone/blob/5ada179028452868623a1aa8f11eee2996886454/core/src/main/java/com/google/errorprone/bugpatterns/nullness/ReturnMissingNullable.java#L77-L80

timtebeek avatar Aug 15 '24 08:08 timtebeek

Consider doing this only for non-private methods since private can usually have their correctness derived via FB/Null checker.

blipper avatar Sep 13 '24 11:09 blipper