Repository navigation
Remove onOverrideMethodInvocationVarargsArrayNullability handler method - #1518
Conversation
WalkthroughIntroduces a new public final class MethodParameterNullness that stores mutable nullness metadata for a method’s formal parameters and, when applicable, its varargs array. Replaces previous per-parameter Possibly related PRs
Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In
`@nullaway/src/main/java/com/uber/nullaway/handlers/RestrictiveAnnotationHandler.java`:
- Around line 135-140: When handling varargs, the code currently only sets
array-level nullness via Nullness.varargsArrayIsNonNull(...), which misses cases
where the parameter is explicitly annotated `@Nullable`; update the
RestrictiveAnnotationHandler logic so that when isVarargsParam is true and
Nullness.paramHasNullableAnnotation(methodSymbol, i, config) is true you also
call argumentNullness.setVarargsArrayNullness(Nullness.NULLABLE) (in addition to
existing per-parameter handling), ensuring varargs-array nullness is preserved
for explicitly nullable varargs parameters; use the existing symbols
isVarargsParam, varargsParamSymbol, paramHasNullableAnnotation,
varargsArrayIsNonNull, and setVarargsArrayNullness to locate and implement the
fix.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 5ffa8aa7-0504-46a9-817f-83c350687612
📒 Files selected for processing (8)
nullaway/src/main/java/com/uber/nullaway/MethodParameterNullness.javanullaway/src/main/java/com/uber/nullaway/NullAway.javanullaway/src/main/java/com/uber/nullaway/dataflow/CoreNullnessStoreInitializer.javanullaway/src/main/java/com/uber/nullaway/handlers/CompositeHandler.javanullaway/src/main/java/com/uber/nullaway/handlers/Handler.javanullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.javanullaway/src/main/java/com/uber/nullaway/handlers/RestrictiveAnnotationHandler.javanullaway/src/test/java/com/uber/nullaway/jspecify/JSpecifyVarargsTests.java
| } else if (Nullness.paramHasNullableAnnotation(methodSymbol, i, config)) { | ||
| argumentPositionNullness[i] = Nullness.NULLABLE; | ||
| argumentNullness.setParameterNullness(i, Nullness.NULLABLE); | ||
| } | ||
| } | ||
| 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; | ||
| if (isVarargsParam && Nullness.varargsArrayIsNonNull(varargsParamSymbol, config)) { | ||
| argumentNullness.setVarargsArrayNullness(Nullness.NONNULL); | ||
| } |
There was a problem hiding this comment.
Preserve nullable varargs-array nullness.
After removing the dedicated varargs hook, this path only writes array-level nullness through Nullness.varargsArrayIsNonNull(...), so a restrictively annotated @Nullable T... parameter never updates argumentNullness.setVarargsArrayNullness(...). That changes behavior for explicit-array varargs calls and related override checks when the default varargs-array nullness is non-null.
Proposed fix
} else if (Nullness.paramHasNullableAnnotation(methodSymbol, i, config)) {
argumentNullness.setParameterNullness(i, Nullness.NULLABLE);
+ if (isVarargsParam) {
+ argumentNullness.setVarargsArrayNullness(Nullness.NULLABLE);
+ }
}
if (isVarargsParam && Nullness.varargsArrayIsNonNull(varargsParamSymbol, config)) {
argumentNullness.setVarargsArrayNullness(Nullness.NONNULL);
}🤖 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/handlers/RestrictiveAnnotationHandler.java`
around lines 135 - 140, When handling varargs, the code currently only sets
array-level nullness via Nullness.varargsArrayIsNonNull(...), which misses cases
where the parameter is explicitly annotated `@Nullable`; update the
RestrictiveAnnotationHandler logic so that when isVarargsParam is true and
Nullness.paramHasNullableAnnotation(methodSymbol, i, config) is true you also
call argumentNullness.setVarargsArrayNullness(Nullness.NULLABLE) (in addition to
existing per-parameter handling), ensuring varargs-array nullness is preserved
for explicitly nullable varargs parameters; use the existing symbols
isVarargsParam, varargsParamSymbol, paramHasNullableAnnotation,
varargsArrayIsNonNull, and setVarargsArrayNullness to locate and implement the
fix.
There was a problem hiding this comment.
The restrictive annotation handler only handles @NonNull annotations in null-unmarked (unannotated) code. So I think this is fine.
There was a problem hiding this comment.
@msridhar, that's a good clarification — since RestrictiveAnnotationHandler only acts on null-unmarked code and its primary purpose is to enforce @NonNull restrictive annotations (overriding the default nullable assumption), the varargs-array nullable case isn't something this handler needs to address. My concern was unfounded. Thanks for the explanation!
✏️ Learnings added
Learnt from: msridhar
Repo: uber/NullAway PR: 1518
File: nullaway/src/main/java/com/uber/nullaway/handlers/RestrictiveAnnotationHandler.java:135-141
Timestamp: 2026-04-11T17:13:55.316Z
Learning: In NullAway's `RestrictiveAnnotationHandler` (`nullaway/src/main/java/com/uber/nullaway/handlers/RestrictiveAnnotationHandler.java`), the handler only applies to null-unmarked (unannotated) code (`isAnnotated == false`) and its primary purpose is to enforce `NonNull` restrictive annotations. The `Nullable` branch exists for completeness but the handler does NOT need to propagate `Nullable` to varargs-array nullness (`setVarargsArrayNullness`), since that is not within the scope of restrictive annotation handling for unannotated code.
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: CR
Repo: uber/NullAway PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-04-08T19:11:30.754Z
Learning: Applies to **/*.java : Do not add top-level `Nullable` annotations on local variables in Java code. NullAway infers the nullability of local variables and ignores these explicit annotations.
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 this repo (uber/NullAway), don’t annotate local variables with `Nullable` or `NonNull`. NullAway infers nullability for local variables automatically, so adding these annotations to locals is incorrect style for this codebase.
Learnt from: msridhar
Repo: uber/NullAway PR: 1507
File: nullaway/src/main/java/com/uber/nullaway/generics/GenericTypePrettyPrintingVisitor.java:66-79
Timestamp: 2026-04-01T15:40:51.141Z
Learning: In NullAway's `GenericTypePrettyPrintingVisitor` (nullaway/src/main/java/com/uber/nullaway/generics/GenericTypePrettyPrintingVisitor.java), the `appendNullableAnnotationIfPresent` helper intentionally prints ONLY `Nullable` type-use annotations and silently suppresses all other type-use annotations (including `NonNull`). This is by design: non-null is the default in NullAway's model and other type-use qualifiers are not relevant to NullAway diagnostics, so only `Nullable` is meaningful to display in error messages.
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: 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: 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.
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: 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: haewiful
Repo: uber/NullAway PR: 1243
File: jdk-annotations/astubx-generator-cli/src/main/java/com/uber/nullaway/jdkannotations/AstubxGeneratorCLI.java:97-106
Timestamp: 2025-09-24T23:14:37.777Z
Learning: In the jdk-javac-plugin NullnessAnnotationSerializer, the plugin only creates one JSON file per compilation, so processing only the first JSON file is intentional behavior rather than a bug.
|
Main Branch: With This PR: |
There was a problem hiding this comment.
♻️ Duplicate comments (1)
nullaway/src/main/java/com/uber/nullaway/handlers/RestrictiveAnnotationHandler.java (1)
135-141:⚠️ Potential issue | 🟠 MajorPreserve nullable varargs-array nullness for
@Nullable T....At Line 135-137,
@Nullableon the varargs parameter updates only per-parameter nullness. But array-level nullness is only set in the non-null path at Line 138-140. This can drop nullable varargs-array semantics and change behavior for explicit-array varargs calls / override checks.Proposed fix
} else if (Nullness.paramHasNullableAnnotation(methodSymbol, i, config)) { argumentNullness.setParameterNullness(i, Nullness.NULLABLE); + if (varargsParamSymbol != null) { + argumentNullness.setVarargsArrayNullness(Nullness.NULLABLE); + } } if (varargsParamSymbol != null && Nullness.varargsArrayIsNonNull(varargsParamSymbol, config)) { argumentNullness.setVarargsArrayNullness(Nullness.NONNULL); }🤖 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/handlers/RestrictiveAnnotationHandler.java` around lines 135 - 141, The code in RestrictiveAnnotationHandler sets per-parameter nullness for a varargs parameter when Nullness.paramHasNullableAnnotation(...) is true but fails to also set the varargs array nullness, so add logic in that branch to preserve array-level NULLABLE semantics for varargs: inside the block where you call argumentNullness.setParameterNullness(i, Nullness.NULLABLE) (the branch using Nullness.paramHasNullableAnnotation), detect if the current parameter is the varargs parameter (varargsParamSymbol != null and i is the varargs index) and call argumentNullness.setVarargsArrayNullness(Nullness.NULLABLE); leave the existing non-null path that sets NONNULL unchanged so you don't overwrite explicit non-null handling.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In
`@nullaway/src/main/java/com/uber/nullaway/handlers/RestrictiveAnnotationHandler.java`:
- Around line 135-141: The code in RestrictiveAnnotationHandler sets
per-parameter nullness for a varargs parameter when
Nullness.paramHasNullableAnnotation(...) is true but fails to also set the
varargs array nullness, so add logic in that branch to preserve array-level
NULLABLE semantics for varargs: inside the block where you call
argumentNullness.setParameterNullness(i, Nullness.NULLABLE) (the branch using
Nullness.paramHasNullableAnnotation), detect if the current parameter is the
varargs parameter (varargsParamSymbol != null and i is the varargs index) and
call argumentNullness.setVarargsArrayNullness(Nullness.NULLABLE); leave the
existing non-null path that sets NONNULL unchanged so you don't overwrite
explicit non-null handling.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 243ffd15-536a-4dd7-98bd-5e031a171e72
📒 Files selected for processing (1)
nullaway/src/main/java/com/uber/nullaway/handlers/RestrictiveAnnotationHandler.java
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #1518 +/- ##
============================================
+ Coverage 88.46% 88.48% +0.01%
- Complexity 2840 2841 +1
============================================
Files 102 103 +1
Lines 9478 9488 +10
Branches 1901 1902 +1
============================================
+ Hits 8385 8395 +10
- Misses 530 531 +1
+ Partials 563 562 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
lazaroclapp
left a comment
There was a problem hiding this comment.
LGTM!
Comments below are just random related questions
| } | ||
| void test() { | ||
| NullableVarargsElementsConsumer consumer = | ||
| inputs -> { |
There was a problem hiding this comment.
So, the semantics here is that inputs is the full array? Can't be called as i1, i2, i3 -> { ... }, then? (I assume so, just double checking)
There was a problem hiding this comment.
Actually, it looks like for a varargs functional-interface method, you can only assign a lambda that takes a single argument, which must be an array; this example doesn't compile https://godbolt.org/z/YsYP1G6KT
| /** | ||
| * Mutable nullness information for a method's formal parameters and varargs array. For a varargs | ||
| * method, the nullability of individual arguments is represented in the last parameter position, | ||
| * while the nullability of the varargs array itself is represented separately. |
There was a problem hiding this comment.
Do we plan to allow using this class in the future to override deep nullability? E.g. allow stuff like overriding argument 1 of foo(List<Bar> bars) to be List<@Nullable Bar> or List<? extends @Nullable Bar> or whatever? Obviously outside of the scope of this PR, but I am curious...
We currently have a separate model from LibraryModels to set these kinds of annotations for third-party libraries / JDK method, right? (using astub(x)?)
There was a problem hiding this comment.
Right now this is handled by the onOverrideMethodType handler method, see, e.g., here for library models:
It would be great to unify the two eventually, but it will also be painful given that we still need to handle declaration annotations and other legacy corner cases. Plus, all the original APIs didn't handle nested annotations, and that support has been tacked on for JSpecify mode, in a way that allows for JSpecify mode to be turned off. I think maybe when JSpecify mode is the default, or even better when there is only JSpecify mode for NullAway, we could think about cleaning things up into one handler method.
Fixes #1501
Instead, we return varargs array nullability through the previous
onOverrideMethodInvocationParametersNullabilityhandler method. We introduce a newMethodParameterNullnesstype to encapsulate regular parameter nullability and nullability of the varargs array;MethodParameterNullnessis mutable for efficiency (like the arrays we used before).This PR should not change visible behavior. Still, I wasn't sure about some corner cases involving lambdas and method overriding, so there are some new tests. I confirmed those new tests passed even without these changes.
Summary by CodeRabbit
Refactor
Tests