Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 24 additions & 29 deletions nullaway/src/main/java/com/uber/nullaway/NullAway.java
Original file line number Diff line number Diff line change
Expand Up @@ -2082,6 +2082,8 @@ private Description handleInvocation(
: Nullness.NONNULL);
}
}

// perform generics checks for calls to annotated methods in JSpecify mode
if (config.isJSpecifyMode()) {
genericsChecks.compareGenericTypeParameterNullabilityForCall(methodSymbol, tree, state);
if (!methodSymbol.getTypeParameters().isEmpty()) {
Expand All @@ -2090,7 +2092,9 @@ private Description handleInvocation(
}
}

// Allow handlers to override the list of non-null argument positions
// Allow handlers to override the list of non-null argument positions. For a varargs parameter,
// this array holds the nullness of individual varargs arguments; the case of a varargs array is
// handled separately
@Nullable Nullness[] finalArgumentPositionNullness =
handler.onOverrideMethodInvocationParametersNullability(
state.context, methodSymbol, isMethodAnnotated, argumentPositionNullness);
Expand All @@ -2106,38 +2110,29 @@ private Description handleInvocation(
}
boolean argIsNonNull =
Objects.equals(Nullness.NONNULL, finalArgumentPositionNullness[argPos]);
boolean mayActualBeNull = false;
if (varArgsPassedAsArray) {
// This is the case where an array is explicitly passed in the position of the
// varargs parameter
// Only check for a nullable varargs array if the method is annotated, or a restrictive
// @NonNull annotation is present on the varargs array itself. In legacy mode, a
// declaration @NonNull annotation also implies the array is @NonNull. Also check for
// JetBrains @NotNull declaration annotation (due to
// https://github.com/uber/NullAway/issues/720)
VarSymbol formalParamSymbol = formalParams.get(formalParams.size() - 1);
boolean restrictiveNonNullVarargsArray =
config.acknowledgeRestrictiveAnnotations()
&& Nullness.varargsArrayIsNonNull(formalParamSymbol, config);
boolean checkForNullableVarargsArray =
isMethodAnnotated
|| (config.isLegacyAnnotationLocation() && argIsNonNull)
|| restrictiveNonNullVarargsArray
|| NullabilityUtil.hasJetBrainsNotNullDeclarationAnnotation(formalParamSymbol);
if (checkForNullableVarargsArray) {
// If varargs array itself is not @Nullable, cannot pass @Nullable array
if (restrictiveNonNullVarargsArray
|| !Nullness.varargsArrayIsNullable(formalParams.get(argPos), config)) {
mayActualBeNull = mayBeNullExpr(state, actual);
}
}
} else {
if (!argIsNonNull) {
// argument can be @Nullable, so nothing to check
return;
}
mayActualBeNull = mayBeNullExpr(state, actual);
VarSymbol varargsFormalParam = formalParams.get(formalParams.size() - 1);
// when is the varargs array itself @NonNull?
// 1. For @NullMarked methods, as long as there is no @Nullable annotation
Nullness varargsArrayNullness =
(isMethodAnnotated && !Nullness.varargsArrayIsNullable(varargsFormalParam, config))
? Nullness.NONNULL
: null;
Comment thread
msridhar marked this conversation as resolved.
Resolved
// 2. For @NullUnmarked methods, there should be an explicit @NonNull annotation. This
// is handled by the RestrictiveAnnotationHandler
// 3. Library model indicating it is @NonNull (handled in LibraryModelsHandler)
varargsArrayNullness =
handler.onOverrideMethodInvocationVarargsArrayNullability(
state.context, methodSymbol, isMethodAnnotated, varargsArrayNullness);
Comment on lines +2119 to +2128

@coderabbitai coderabbitai Bot Mar 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial

Make varargsArrayNullness explicitly nullable for readability.

At Line [2123], the local can be null; annotating it improves intent clarity and avoids future confusion.

♻️ Suggested refactor
-            Nullness varargsArrayNullness =
+            `@Nullable` Nullness varargsArrayNullness =
                 (isMethodAnnotated && !Nullness.varargsArrayIsNullable(varargsFormalParam, config))
                     ? Nullness.NONNULL
                     : null;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@nullaway/src/main/java/com/uber/nullaway/NullAway.java` around lines 2123 -
2132, The local variable varargsArrayNullness is assigned null in some branches
but not explicitly declared nullable; update its declaration to make nullability
explicit (e.g., annotate or declare as `@Nullable/nullable` type) to improve
readability and intent. Locate the declaration and initialization around
Nullness.varargsArrayIsNullable(varargsFormalParam, config) and ensure the
variable holding that result (varargsArrayNullness) is explicitly nullable
before it is passed into
handler.onOverrideMethodInvocationVarargsArrayNullability(state.context,
methodSymbol, isMethodAnnotated, varargsArrayNullness).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NullAway infers the nullability of local variables; they should not be annotated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@msridhar, thanks for the clarification! That makes sense — since NullAway itself infers the nullability of local variables, annotating them is unnecessary. I'll retract the suggestion.


✏️ Learnings added
Learnt from: msridhar
Repo: uber/NullAway PR: 1485
File: nullaway/src/main/java/com/uber/nullaway/NullAway.java:2119-2128
Timestamp: 2026-03-20T15:34:10.657Z
Learning: In NullAway's own codebase (uber/NullAway), local variables should NOT be annotated with `Nullable` or `NonNull`. NullAway infers the nullability of local variables automatically, so adding such annotations to locals is incorrect style for this codebase.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.


🧠 Learnings used
Learnt from: msridhar
Repo: uber/NullAway PR: 1464
File: nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java:610-621
Timestamp: 2026-02-15T00:29:58.053Z
Learning: In NullAway's codebase, the `InvocationArguments.forEach` method has special-case handling for varargs and other edge cases, so it should not be replaced with a simple indexed loop. Using `forEach` with an `AtomicReference` or similar pattern is appropriate when you need to find a specific argument and its corresponding formal parameter type.

Learnt from: msridhar
Repo: uber/NullAway PR: 1248
File: nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java:847-857
Timestamp: 2025-08-28T04:54:20.953Z
Learning: In NullAway's GenericsChecks.java, NewClassTree support for explicit type argument substitution requires more extensive changes beyond just modifying the conditional in compareGenericTypeParameterNullabilityForCall. The maintainers prefer to handle NewClassTree support in a separate follow-up rather than expanding the scope of PRs focused on specific issues like super constructor calls.

Learnt from: msridhar
Repo: uber/NullAway PR: 1316
File: jdk-javac-plugin/src/main/java/com/uber/nullaway/javacplugin/NullnessAnnotationSerializer.java:261-293
Timestamp: 2025-10-29T23:56:18.236Z
Learning: In NullAway's jdk-javac-plugin NullnessAnnotationSerializer, type variable bounds with annotations (e.g., `T extends Nullable Object`) are checked at their declaration sites by the typeParamHasAnnotation method for both class-level and method-level type parameters. The hasJSpecifyAnnotationDeep method is designed to check type uses (return types, parameters, etc.) and does not need a TYPEVAR case because type variable declaration bounds are already handled separately.

Learnt from: msridhar
Repo: uber/NullAway PR: 1421
File: nullaway/src/main/java/com/uber/nullaway/NullAway.java:339-340
Timestamp: 2025-12-31T23:59:20.009Z
Learning: In Error Prone's ASTHelpers class, the getSymbol(MethodInvocationTree) overload is guaranteed to return a non-null Symbol.MethodSymbol. This differs from other overloads of getSymbol() that may return null. Therefore, no null check is required after calling ASTHelpers.getSymbol() with a MethodInvocationTree parameter.

Learnt from: msridhar
Repo: uber/NullAway PR: 1245
File: guava-recent-unit-tests/src/test/java/com/uber/nullaway/guava/NullAwayGuavaParametricNullnessTests.java:101-102
Timestamp: 2025-08-14T18:50:06.159Z
Learning: In NullAway JSpecify tests, when JDK version requirements exist due to bytecode annotation reading capabilities, prefer failing tests over skipping them on unsupported versions to ensure CI catches regressions and enforces proper JDK version usage for developers.

Learnt from: msridhar
Repo: uber/NullAway PR: 1259
File: jdk-recent-unit-tests/src/test/java/com/uber/nullaway/jdk17/SwitchTests.java:318-321
Timestamp: 2025-08-29T18:41:43.584Z
Learning: Classes annotated with NullMarked are analyzed by NullAway even if they are not in packages specified by the AnnotatedPackages configuration. The NullMarked annotation guarantees NullAway analysis.

argIsNonNull = Objects.equals(varargsArrayNullness, Nullness.NONNULL);
}
if (!argIsNonNull) {
// argument can be @Nullable, so nothing to check
return;
}
boolean mayActualBeNull = mayBeNullExpr(state, actual);
if (mayActualBeNull) {
String message =
"passing @Nullable parameter '"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ private static boolean checkArrayElementAnnotations(
* Does the given symbol have a JetBrains @NotNull declaration annotation? Useful for workarounds
* in light of https://github.com/uber/NullAway/issues/720
*/
public static boolean hasJetBrainsNotNullDeclarationAnnotation(Symbol.VarSymbol varSymbol) {
public static boolean hasJetBrainsNotNullDeclarationAnnotation(Symbol varSymbol) {
// We explicitly ignore type-use annotations here, looking for @NotNull used as a
// declaration annotation, which is why this logic is simpler than e.g.
// NullabilityUtil.getAllAnnotationsForParameter.
Expand Down
6 changes: 5 additions & 1 deletion nullaway/src/main/java/com/uber/nullaway/Nullness.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,11 @@ public static boolean varargsArrayIsNullable(Symbol paramSymbol, Config config)
public static boolean varargsArrayIsNonNull(Symbol paramSymbol, Config config) {
return hasNonNullTypeUseAnnotation(paramSymbol, config)
|| (config.isLegacyAnnotationLocation()
&& hasNonNullDeclarationAnnotation(paramSymbol, config));
&& hasNonNullDeclarationAnnotation(paramSymbol, config))
// For Kotlin-generated class files, we treat the presence of a JetBrains @NotNull
// declaration annotation on a varargs parameter as indicating that the varargs array itself
// is non-null. See https://github.com/uber/NullAway/issues/720
|| NullabilityUtil.hasJetBrainsNotNullDeclarationAnnotation(paramSymbol);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

/** Checks if the symbol has a {@code @Nullable} declaration annotation */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,20 @@ public boolean onOverrideFieldNullability(Symbol field) {
return argumentPositionNullness;
}

@Override
public @Nullable Nullness onOverrideMethodInvocationVarargsArrayNullability(
Context context,
Symbol.MethodSymbol methodSymbol,
boolean isAnnotated,
@Nullable Nullness varargsArrayNullness) {
for (Handler h : handlers) {
varargsArrayNullness =
h.onOverrideMethodInvocationVarargsArrayNullability(
context, methodSymbol, isAnnotated, varargsArrayNullness);
}
return varargsArrayNullness;
}

@Override
public boolean onOverrideMayBeNullExpr(
NullAway analysis,
Expand Down
31 changes: 30 additions & 1 deletion nullaway/src/main/java/com/uber/nullaway/handlers/Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ default boolean onOverrideFieldNullability(Symbol field) {
* @param argumentPositionNullness Nullness info for each argument position as computed by
* upstream handlers and/or the base analysis. Some entries may be {@code null}, indicating
* upstream handlers and the base analysis consider the parameter to be nullness-unknown,
* usually since the parameter is from unannotated code.
* usually since the parameter is from unannotated code. For a varargs parameter, the nullness
* for when individual parameters are passed in the varargs position will be stored in this
* array. See {@link #onOverrideMethodInvocationVarargsArrayNullability} for overriding the
* nullness of the varargs array itself.
* @return The updated nullness info for each argument position, as computed by the current
* handler.
*/
Expand All @@ -222,6 +225,32 @@ default boolean onOverrideFieldNullability(Symbol field) {
return argumentPositionNullness;
}

/**
* Called to potentially override the nullability of the array itself when a varargs method is
* invoked by passing an array in the varargs position.
*
* <p>This hook is intentionally separate from {@link
* #onOverrideMethodInvocationParametersNullability(Context, Symbol.MethodSymbol, boolean,
* Nullness[])}. A single parameter-slot nullness value is not expressive enough for varargs,
* where the array and its elements can have different nullability.
*
* @param context The current context.
* @param methodSymbol The method symbol for the method in question.
* @param isAnnotated A boolean flag indicating whether the called method is annotated
* @param varargsArrayNullness current value for the varargs array nullability, or {@code null} if
* upstream handlers and the base analysis consider the parameter to be nullness-unknown
* @return Updated explicit nullability for the varargs array, or {@code varargsArrayNullness} if
* the current handler does not provide an override.
*/
default @Nullable Nullness onOverrideMethodInvocationVarargsArrayNullability(
Context context,
Symbol.MethodSymbol methodSymbol,
boolean isAnnotated,
@Nullable Nullness varargsArrayNullness) {
// NoOp
return varargsArrayNullness;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

/**
* Called when the Dataflow analysis generates the initial NullnessStore for a method or lambda.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static com.uber.nullaway.LibraryModels.MethodRef.methodRef;
import static com.uber.nullaway.Nullness.NONNULL;
import static com.uber.nullaway.Nullness.NULLABLE;
import static com.uber.nullaway.librarymodel.NestedAnnotationInfo.TypePathEntry.Kind.ARRAY_ELEMENT;
import static com.uber.nullaway.librarymodel.NestedAnnotationInfo.TypePathEntry.Kind.TYPE_ARGUMENT;

import com.google.common.base.Preconditions;
Expand Down Expand Up @@ -134,11 +135,22 @@ public NullnessHint onDataflowVisitFieldAccess(
optimizedLibraryModels.nonNullParameters(methodSymbol);
// For sanity check: $ nonNullParamsFromModel \cap nullableParamsFromModel $ should be empty
Set<Integer> allPositions = new HashSet<>();
boolean varArgsMethod = methodSymbol.isVarArgs();
for (Integer nullParam : nullableParamsFromModel) {
if (varArgsMethod && nullParam == methodSymbol.getParameters().size() - 1) {
// For varargs, top-level library models describe the array itself. We handle that through
// the dedicated varargs-array hook so that element nullability can be modeled separately.
continue;
}
allPositions.add(nullParam);
argumentPositionNullness[nullParam] = NULLABLE;
}
for (Integer nonNullParam : nonNullParamsFromModel) {
if (varArgsMethod && nonNullParam == methodSymbol.getParameters().size() - 1) {
// See comment above: top-level nullability for the varargs parameter applies to the array,
// not to individually passed varargs elements.
continue;
}
if (!allPositions.add(nonNullParam)) {
// position was already marked as nullable
throw new IllegalStateException(
Expand All @@ -148,9 +160,61 @@ public NullnessHint onDataflowVisitFieldAccess(
}
argumentPositionNullness[nonNullParam] = NONNULL;
}
if (varArgsMethod) {
ImmutableSetMultimap<Integer, NestedAnnotationInfo> nestedAnnotations =
optimizedLibraryModels.nestedAnnotationsForMethods(methodSymbol);
int varargsIndex = methodSymbol.getParameters().size() - 1;
Nullness varargsContentsNullness = null;
for (NestedAnnotationInfo nestedAnnotation : nestedAnnotations.get(varargsIndex)) {
// check if it's for the array element
if (nestedAnnotation.typePath().size() == 1
&& nestedAnnotation.typePath().get(0).kind() == ARRAY_ELEMENT) {
// this is a nested annotation for the varargs array element, which applies when
// individual parameters are passed
if (varargsContentsNullness == null) {
varargsContentsNullness =
nestedAnnotation.annotation() == Annotation.NULLABLE ? NULLABLE : NONNULL;
argumentPositionNullness[varargsIndex] = varargsContentsNullness;
} else {
throw new IllegalStateException(
"varargs contents nullness set multiple times: "
+ nestedAnnotations.get(varargsIndex));
}
}
Comment thread
msridhar marked this conversation as resolved.
Resolved
}
}
return argumentPositionNullness;
}

@Override
public @Nullable Nullness onOverrideMethodInvocationVarargsArrayNullability(
Context context,
Symbol.MethodSymbol methodSymbol,
boolean isAnnotated,
@Nullable Nullness varargsArrayNullness) {
Nullness modeledVarargsArrayNullness = null;
if (methodSymbol.isVarArgs()) {
int varargsIndex = methodSymbol.getParameters().size() - 1;
OptimizedLibraryModels optimizedLibraryModels = getOptLibraryModels(context);
boolean nullableFromModel =
optimizedLibraryModels.explicitlyNullableParameters(methodSymbol).contains(varargsIndex);
boolean nonNullFromModel =
optimizedLibraryModels.nonNullParameters(methodSymbol).contains(varargsIndex);
if (nullableFromModel && nonNullFromModel) {
throw new IllegalStateException(
String.format(
"Library models give conflicting varargs array nullability for method %s",
methodSymbol.getQualifiedName()));
Comment thread
msridhar marked this conversation as resolved.
Resolved
}
if (nullableFromModel) {
modeledVarargsArrayNullness = NULLABLE;
} else if (nonNullFromModel) {
modeledVarargsArrayNullness = NONNULL;
}
}
return modeledVarargsArrayNullness != null ? modeledVarargsArrayNullness : varargsArrayNullness;
}

@Override
public Nullness onOverrideMethodReturnNullability(
Symbol.MethodSymbol methodSymbol,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,22 @@ private CodeAnnotationInfo getCodeAnnotationInfo(Context context) {
return argumentPositionNullness;
}

@Override
public @Nullable Nullness onOverrideMethodInvocationVarargsArrayNullability(
Context context,
Symbol.MethodSymbol methodSymbol,
boolean isAnnotated,
@Nullable Nullness varargsArrayNullness) {
if (methodSymbol.isVarArgs()) {
Symbol.VarSymbol varargsParamSymbol =
methodSymbol.getParameters().get(methodSymbol.getParameters().size() - 1);
if (Nullness.varargsArrayIsNonNull(varargsParamSymbol, config)) {
return Nullness.NONNULL;
}
}
return varargsArrayNullness;
}

@Override
public Nullness onOverrideMethodReturnNullability(
Symbol.MethodSymbol methodSymbol,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.uber.lib.unannotated;

/* @NullMarked */
public class NullMarkedVarargsWithModel {

public static void nullableContents(/* @Nullable */ String... args) {}

public static void nullableArray(String /* @Nullable */... args) {}

public static void bothNullable(/* @Nullable */ String /* @Nullable */... args) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.uber.lib.unannotated;

public class NullUnmarkedVarargsWithModel {

public static void nonNullContents(/* @NonNull */ String... args) {}

public static void nonNullArray(String /* @NonNull */... args) {}

public static void bothNonNull(/* @NonNull */ String /* @NonNull */... args) {}
}
Loading