Skip to content

Enable StatementSwitchToExpressionSwitch check and fix all warnings#1413

Merged
msridhar merged 4 commits intomasterfrom
switch-expression-conversion
Dec 28, 2025
Merged

Enable StatementSwitchToExpressionSwitch check and fix all warnings#1413
msridhar merged 4 commits intomasterfrom
switch-expression-conversion

Conversation

@msridhar
Copy link
Copy Markdown
Collaborator

@msridhar msridhar commented Dec 28, 2025

Summary by CodeRabbit

  • Refactor

    • Modernized many switch statements to Java 14+ switch expressions for cleaner, more consistent control flow.
    • Improved type/symbol handling and added local-variable support in location/serialization paths for more robust nullability and serialization behavior.
  • Chores

    • Re-enabled the StatementSwitchToExpressionSwitch errorprone check to enforce the modern switch style.

✏️ Tip: You can customize this high-level summary in your review settings.

@msridhar msridhar marked this pull request as ready for review December 28, 2025 06:24
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Dec 28, 2025

Walkthrough

Refactors many traditional switch statements into Java 14+ switch expressions with arrow syntax across nullaway and jar-infer modules, adds a LOCAL_VARIABLE case in symbol location mapping, replaces a string-based RECORD kind check with ElementKind.RECORD, and re-enables the StatementSwitchToExpressionSwitch Error Prone check in build.gradle. No public/exported signatures were changed.

Possibly related PRs

Suggested reviewers

  • yuxincs
  • lazaroclapp

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main changes: enabling the StatementSwitchToExpressionSwitch errorprone check and converting all switch statements to switch expressions throughout the codebase.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch switch-expression-conversion

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7751c1b and ec373cc.

📒 Files selected for processing (1)
  • nullaway/src/main/java/com/uber/nullaway/fixserialization/XMLUtil.java
🔇 Additional comments (1)
nullaway/src/main/java/com/uber/nullaway/fixserialization/XMLUtil.java (1)

176-186: Remove this comment — the premise is unsupported by the codebase.

The claim that this change replaces Double support with Integer support cannot be verified. No evidence of Double ever being supported exists in the codebase: there are no Double.class usages with these methods, no Double references in XMLUtil.java or the fixserialization package, and no test cases using Double. The current code supports only Integer, Boolean, and String, and this appears to be the only supported set.

Likely an incorrect or invalid review comment.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

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/fixserialization/XMLUtil.java (1)

170-187: Error message doesn’t match supported types in DefaultXMLValueProvider

The switch supports "Integer", "Boolean", and "String, but the exception text says “only Double|Boolean|String accepted,” which is misleading.

Consider aligning the message with the actual cases (or adding Double support if that was intended). For example:

Proposed message-only fix
-                          + ", only Double|Boolean|String accepted.");
+                          + ", only Integer|Boolean|String accepted.");
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a2afed5 and 5b35042.

📒 Files selected for processing (13)
  • build.gradle
  • jar-infer/jar-infer-lib/src/main/java/com/uber/nullaway/jarinfer/DefinitelyDerefedParamsDriver.java
  • nullaway/src/main/java/com/uber/nullaway/ErrorBuilder.java
  • nullaway/src/main/java/com/uber/nullaway/NullAway.java
  • nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java
  • nullaway/src/main/java/com/uber/nullaway/Nullness.java
  • nullaway/src/main/java/com/uber/nullaway/dataflow/AccessPathNullnessPropagation.java
  • nullaway/src/main/java/com/uber/nullaway/fixserialization/Serializer.java
  • nullaway/src/main/java/com/uber/nullaway/fixserialization/XMLUtil.java
  • nullaway/src/main/java/com/uber/nullaway/fixserialization/adapters/SerializationAdapter.java
  • nullaway/src/main/java/com/uber/nullaway/fixserialization/location/SymbolLocation.java
  • nullaway/src/main/java/com/uber/nullaway/generics/ConstraintSolverImpl.java
  • nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java
💤 Files with no reviewable changes (1)
  • build.gradle
🧰 Additional context used
🧠 Learnings (6)
📓 Common learnings
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.
📚 Learning: 2025-08-28T04:54:20.953Z
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.

Applied to files:

  • nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java
  • nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java
  • nullaway/src/main/java/com/uber/nullaway/generics/ConstraintSolverImpl.java
  • nullaway/src/main/java/com/uber/nullaway/Nullness.java
  • nullaway/src/main/java/com/uber/nullaway/dataflow/AccessPathNullnessPropagation.java
  • nullaway/src/main/java/com/uber/nullaway/NullAway.java
📚 Learning: 2025-08-14T18:50:06.159Z
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.

Applied to files:

  • nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java
  • nullaway/src/main/java/com/uber/nullaway/Nullness.java
  • nullaway/src/main/java/com/uber/nullaway/NullAway.java
📚 Learning: 2025-10-29T23:56:18.236Z
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.

Applied to files:

  • nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java
  • nullaway/src/main/java/com/uber/nullaway/generics/ConstraintSolverImpl.java
  • nullaway/src/main/java/com/uber/nullaway/ErrorBuilder.java
  • nullaway/src/main/java/com/uber/nullaway/Nullness.java
📚 Learning: 2025-09-03T19:40:11.118Z
Learnt from: msridhar
Repo: uber/NullAway PR: 1276
File: nullaway/src/main/java/com/uber/nullaway/generics/TypeMetadataBuilder.java:235-241
Timestamp: 2025-09-03T19:40:11.118Z
Learning: In JDK 21+, ListBuffer can be passed directly to TypeMetadata.Annotations constructor without needing conversion to List via toList() method. The APIs were updated to make this compatible.

Applied to files:

  • nullaway/src/main/java/com/uber/nullaway/Nullness.java
📚 Learning: 2025-08-29T18:41:43.584Z
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.

Applied to files:

  • nullaway/src/main/java/com/uber/nullaway/NullAway.java
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
  • GitHub Check: Build and test on ubuntu-latest
  • GitHub Check: Build caffeine with snapshot
  • GitHub Check: Build and test on windows-latest
  • GitHub Check: Build and test on macos-latest
  • GitHub Check: Build spring-framework with snapshot
🔇 Additional comments (17)
nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java (1)

215-222: Switch expression for typeArguments is tight and correct

Using switch (tree.getKind()) with explicit METHOD_INVOCATION / NEW_CLASS branches and a failing default matches the method’s contract and keeps bad call sites visible at runtime. This refactor is behavior‑preserving and clearer than the previous branching.

jar-infer/jar-infer-lib/src/main/java/com/uber/nullaway/jarinfer/DefinitelyDerefedParamsDriver.java (1)

674-680: Wildcard switch expression is clear and equivalent

The new switch expression for sourceLevelWildcardType cleanly preserves the * / + / - handling with an explicit failing default. No behavioral concerns.

nullaway/src/main/java/com/uber/nullaway/fixserialization/Serializer.java (1)

188-196: serializeSymbol switch expression keeps existing behavior

The enhanced switch cleanly captures the previous branching: fields/parameters by simple name, methods/constructors via the adapter, everything else via flatName(). The shared MethodSymbol cast for METHOD and CONSTRUCTOR is appropriate.

nullaway/src/main/java/com/uber/nullaway/generics/ConstraintSolverImpl.java (1)

168-193: Worklist propagation switch is safely refactored

The arrow-style switch on st.nullness preserves the NONNULL/NULLABLE propagation logic and turns the previously implicit “should be unreachable” UNKNOWN path into an explicit failure. Given the worklist is only seeded for non‑UNKNOWN states, this is a sound and slightly safer refactor.

nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java (2)

383-421: targetTypeMatches arrow switch + RECORD support look correct

The enhanced switch keeps the existing behavior for parameters, locals, fields, methods, etc., and explicitly groups CLASS, ENUM, and RECORD as “no top-level type-use annotations,” returning false. The AssertionError default ensures any unexpected ElementKind is caught early. This is a clean, behavior‑preserving modernization with improved RECORD support.


534-540: nullnessToBool switch matches documented semantics

Mapping NULL and NULLABLE to true and BOTTOM/NONNULL to false in a switch expression exactly matches the method’s contract. The default AssertionError is a good guard against future Nullness enum extensions.

nullaway/src/main/java/com/uber/nullaway/dataflow/AccessPathNullnessPropagation.java (1)

739-756: visitFieldAccess NullnessHint handling is preserved and clearer

The switch expression on handler.onDataflowVisitFieldAccess(...) preserves the HINT_NULLABLE / FORCE_NONNULL / UNKNOWN mapping, with UNKNOWN still delegating to mayBeNullFieldFromType. The default case throws, which is appropriate as a safeguard if NullnessHint ever gains new values.

nullaway/src/main/java/com/uber/nullaway/fixserialization/adapters/SerializationAdapter.java (1)

81-95: Version-to-adapter switch expression is straightforward

The new switch expression keeps the same mapping (1→v1 adapter, 3→v3 adapter, 2 and others throwing targeted RuntimeExceptions) and uses LATEST_VERSION in the error text. This is a clean modernization with unchanged behavior.

nullaway/src/main/java/com/uber/nullaway/NullAway.java (4)

349-354: LGTM: Clean switch expression conversion.

The conversion from traditional switch to switch expression is straightforward and preserves the original logic correctly.


656-680: LGTM: Correct switch expression conversion with block bodies.

The conversion properly uses blocks for each case that contains multiple statements, maintaining the original logic.


2457-2503: LGTM: Switch expression conversion for entity collection.

The conversion correctly uses arrow syntax with blocks for each case, and the default case properly throws an exception for unexpected tree kinds.


2577-2688: LGTM: Switch expression conversion with extensive case grouping.

The conversion consolidates a large number of expression kinds that all return false (clearly non-nullable) into a single arrow case (lines 2582-2632). This is a significant readability improvement. The second switch expression (lines 2638-2688) correctly handles specific expression types that require nullness analysis.

nullaway/src/main/java/com/uber/nullaway/Nullness.java (2)

135-142: LGTM: Clean switch expression conversion.

The conversion to a switch expression with explicit returns for each Nullness value is straightforward and correct.


26-26: LGTM: Improved type checking using ElementKind enum.

The change from string-based comparison (symbol.owner.toString().equals("RECORD")) to enum-based comparison (symbol.owner.getKind().equals(ElementKind.RECORD)) is more type-safe and robust. This is a good improvement beyond just the switch expression conversion.

Also applies to: 259-259

nullaway/src/main/java/com/uber/nullaway/ErrorBuilder.java (2)

194-223: LGTM: Switch expression with appropriate case grouping.

The conversion groups message types with identical handling (DEREFERENCE_NULLABLE, RETURN_NULLABLE, etc.) into a single arrow case, improving readability. Individual cases for specific message types are handled correctly.


332-349: LGTM: Switch expression conversion with varied return logic.

The conversion correctly handles different return logic for each case: NULL_LITERAL always returns false, IDENTIFIER has conditional logic for parameters, and the default returns true.

nullaway/src/main/java/com/uber/nullaway/fixserialization/location/SymbolLocation.java (1)

58-78: LGTM: Switch expression conversion with functional enhancement.

Beyond the switch expression conversion, this change adds support for LOCAL_VARIABLE symbols (line 75), which was previously missing. Note that the comment (lines 62-74) indicates that local variables declared inside lambda expressions are still not fully handled and will require future changes to LocalVariableLocation.

This is a functional enhancement, not just a syntax refactor, but it appropriately handles a previously unhandled case.

@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 28, 2025

Codecov Report

❌ Patch coverage is 68.83117% with 24 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.36%. Comparing base (a2afed5) to head (ec373cc).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...away/src/main/java/com/uber/nullaway/NullAway.java 70.58% 4 Missing and 1 partial ⚠️
...va/com/uber/nullaway/fixserialization/XMLUtil.java 50.00% 2 Missing and 1 partial ⚠️
...ixserialization/adapters/SerializationAdapter.java 40.00% 2 Missing and 1 partial ⚠️
...llaway/jarinfer/DefinitelyDerefedParamsDriver.java 60.00% 1 Missing and 1 partial ⚠️
...away/src/main/java/com/uber/nullaway/Nullness.java 60.00% 1 Missing and 1 partial ⚠️
...llaway/dataflow/AccessPathNullnessPropagation.java 75.00% 1 Missing and 1 partial ⚠️
...away/fixserialization/location/SymbolLocation.java 66.66% 1 Missing and 1 partial ⚠️
...ava/com/uber/nullaway/generics/GenericsChecks.java 60.00% 1 Missing and 1 partial ⚠️
.../src/main/java/com/uber/nullaway/ErrorBuilder.java 85.71% 1 Missing ⚠️
...c/main/java/com/uber/nullaway/NullabilityUtil.java 83.33% 1 Missing ⚠️
... and 1 more
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1413      +/-   ##
============================================
+ Coverage     88.29%   88.36%   +0.06%     
+ Complexity     2661     2659       -2     
============================================
  Files            98       98              
  Lines          8853     8835      -18     
  Branches       1767     1767              
============================================
- Hits           7817     7807      -10     
+ Misses          514      508       -6     
+ Partials        522      520       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@msridhar msridhar requested a review from yuxincs December 28, 2025 17:16
@msridhar msridhar merged commit 8cf4101 into master Dec 28, 2025
9 of 11 checks passed
@msridhar msridhar deleted the switch-expression-conversion branch December 28, 2025 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants