error-prone icon indicating copy to clipboard operation
error-prone copied to clipboard

`AddNullMarkedToPackageInfo` should also report absent packages

Open JarvisCraft opened this issue 5 months ago • 2 comments

Problem

There is an experimental AddNullMarkedToPackageInfo bug pattern which reports package-info.java files which do not have @NullMarked applied to them.

It does help detect the issue of forgetting to add @NullMarked to the package, but in our experience a more common problem is forgetting to create package-info itself and thus marking it with @NullMarked which is currently not caught by the checker.

Solution

The suggestion is to also handle this scenario, probably also allowing the package to be explicitly @NullUnmarked so that there still is an escape hatch for unannotated packages.

Pseudo-code

Currently:

if (thePackage.exists()) {
    if (!thePackage.isAnnotated(NullMarked.class)) {
        reportError("No @NullMarked on packages %s", thePackage);
    }
}

Suggested:

if (thePackage.exists()) {
    if (!thePackage.isAnnotated(NullMarked.class)
        && !thePackage.isAnnotated(NullUnmarked.class)) {
        reportError("No @NullMarked or @NullUnmarked on packages %s", thePackage);
    }
} else {
    reportError("No package info %s", thePackage);
}

JarvisCraft avatar Oct 06 '25 10:10 JarvisCraft

FYI: NullAway have implemented their own equivalent with the described semantic aka RequireExplicitNullMarking

JarvisCraft avatar Dec 11 '25 03:12 JarvisCraft

@JarvisCraft nice, so leaving AddNullMarkedToPackageInfo the way it works would allow us to expect annotations in case package-info exists and NullAway's RequireExpliciteNullMarking allows us to expect everything to be null marked. NullAway does not care whether it's module, package or class annotations. Personally, I no longer want the behavior of AddNullMarkedToPackageInfo to be changed.

kressi avatar Dec 11 '25 05:12 kressi