Repository navigation
Fix library modeling for varargs arrays #1485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5792769
5a8f9c2
88f26ca
d95e1f5
19bf783
b24d206
0b78285
afef484
c3bcc5e
d64e304
5de13e9
50bd83d
93656bf
fe977bd
bc6834e
de4c236
2cd4d8b
0a9c5f8
58a5258
7b50df9
c712781
44274dd
ea00b4a
03eaeb3
decf45c
1748a32
1ded3ef
944d32a
d3c1bad
9094085
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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()) { | ||
|
|
@@ -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); | ||
|
|
@@ -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; | ||
| // 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Make At Line [2123], the local can be ♻️ Suggested refactor- Nullness varargsArrayNullness =
+ `@Nullable` Nullness varargsArrayNullness =
(isMethodAnnotated && !Nullness.varargsArrayIsNullable(varargsFormalParam, config))
? Nullness.NONNULL
: null;🤖 Prompt for AI Agents
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NullAway infers the nullability of local variables; they should not be annotated
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
✏️ Learnings added
🧠 Learnings used |
||
| 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 '" | ||
|
|
||
| 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) {} | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.