Support static @Initializer methods assigning receiver parameter fields#1626
Support static @Initializer methods assigning receiver parameter fields#1626Shankar-v27 wants to merge 2 commits into
Conversation
WalkthroughChangesThis change extends NullAway's field initialization checking to recognize fields initialized via static initializer methods annotated with Suggested labels: Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 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: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@nullaway/src/main/java/com/uber/nullaway/dataflow/NullnessStore.java`:
- Around line 292-315: The body of getNonNullParameterFields is duplicating the
same access-path scanning logic used by getReceiverFields,
getNonNullReceiverFields, and getNonNullStaticFields. Extract the shared
iteration/filtering into a private helper in NullnessStore that takes the
nullness value and a root-matching predicate (or equivalent root selector), then
have getNonNullParameterFields delegate to it with the parameter equality check.
Keep the FIELD-only element filtering and the existing result type unchanged.
In `@nullaway/src/main/java/com/uber/nullaway/NullAway.java`:
- Around line 2342-2352: The static initializer handling in NullAway.java only
analyzes the first parameter of each method, so later-parameter field
initialization paths are missed. Update the loop in the static initializer
processing to consider all parameters from each MethodTree returned by
entities.staticInitializerMethods(), or else clearly enforce and document a
first-parameter-only contract; use getNullnessAnalysis,
getNonnullFieldsOfParameterAtExit, and initInSomeInitializerBuilder as the key
locations to adjust.
🪄 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: 159d512d-fd90-4791-b621-e57b5f3fe1c4
📒 Files selected for processing (4)
nullaway/src/main/java/com/uber/nullaway/NullAway.javanullaway/src/main/java/com/uber/nullaway/dataflow/AccessPathNullnessAnalysis.javanullaway/src/main/java/com/uber/nullaway/dataflow/NullnessStore.javanullaway/src/test/resources/com/uber/nullaway/testdata/CheckFieldInitNegativeCases.java
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
nullaway/src/main/java/com/uber/nullaway/dataflow/NullnessStore.java (1)
292-302: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick winMake
getFieldsWithRootMatchingaccept a nullable rootAccessPath.getRoot()is@Nullable, andNullnessStoreis@NullMarked, sorootMatches.test(ap.getRoot())should type the predicate asPredicate<@nullableElement>(or equivalent) to match the values it receives.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@nullaway/src/main/java/com/uber/nullaway/dataflow/NullnessStore.java` around lines 292 - 302, The root-matching helper in NullnessStore is currently typed too narrowly for nullable roots. Update getFieldsWithRootMatching and its rootMatches predicate usage so the predicate accepts a nullable Element (for example Predicate<`@Nullable` Element>), since AccessPath.getRoot() can return null under `@NullMarked`; adjust the method signature and any related local declarations in NullnessStore to match the actual values passed into rootMatches.test(...).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@nullaway/src/main/java/com/uber/nullaway/dataflow/NullnessStore.java`:
- Around line 292-302: The root-matching helper in NullnessStore is currently
typed too narrowly for nullable roots. Update getFieldsWithRootMatching and its
rootMatches predicate usage so the predicate accepts a nullable Element (for
example Predicate<`@Nullable` Element>), since AccessPath.getRoot() can return
null under `@NullMarked`; adjust the method signature and any related local
declarations in NullnessStore to match the actual values passed into
rootMatches.test(...).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 9411cc5e-da5f-4211-93f7-3f2a41b2ae07
📒 Files selected for processing (2)
nullaway/src/main/java/com/uber/nullaway/NullAway.javanullaway/src/main/java/com/uber/nullaway/dataflow/NullnessStore.java
Thank you for contributing to NullAway!
Please note that once you click "Create Pull Request" you will be asked to sign our Uber Contributor License Agreement via CLA assistant.
Before pressing the "Create Pull Request" button, please provide the following:
Description
Adds support for static methods annotated with
@Initializerthat initialize fields through a receiver parameter.Currently, NullAway recognizes initialization performed through instance initializer methods, but static helper methods such as:
are not considered when checking field initialization.
This results in false positive
@NonNull field not initializedwarnings for cases such as enum constants that defer field assignment through shared static initialization logic.This PR updates the initialization analysis to:
collect fields initialized through parameter access paths
support static
@Initializerhelper methodsreuse the existing AccessPath-based nullness tracking
preserve the existing instance initializer behavior
Fixes #1625
Added regression coverage with
EnumWithStaticInitializer, verifying that enum fields assigned through a static@Initializerhelper are recognized as initialized.Testing
Ran successfully:
Both completed with
BUILD SUCCESSFUL.Summary by CodeRabbit