[analysis_server] Infer nullable type in 'Add type annotation' when assignment isn't unconditional - #64046
Conversation
…ssignment isn't unconditional The `Add type annotation` fix/assist (triggered e.g. by `prefer_typing_uninitialized_variables`) infers a variable's type from assignments found anywhere in the rest of the enclosing block, without regard to whether those assignments are guaranteed to run. For a variable only assigned inside a loop body, a single-branch `if`, or a `try` block, the loop/branch/try might not execute, so the variable could still hold its implicit initial value, `null`, at the point of use. The fix incorrectly inferred a non-nullable type in these cases. This adds a conservative check for whether at least one of the remaining statements is guaranteed to assign the variable (accounting for `if`/`else`, `try`/`finally`, `do`-`while`, labeled statements, and immediately-invoked closures), and makes the inferred type nullable when it isn't. Fixes dart-lang#64015
|
Thank you for your contribution! This project uses Gerrit for code reviews. Your pull request has automatically been converted into a code review at: https://dart-review.googlesource.com/c/sdk/+/536161 Please wait for a developer to review your code review at the above link; you can speed up the review if you sign into Gerrit and manually add a reviewer that has recently worked on the relevant code. See CONTRIBUTING.md to learn how to upload changes to Gerrit directly. Additional commits pushed to this PR will update both the PR and the corresponding Gerrit CL. After the review is complete on the CL, your reviewer will merge the CL (automatically closing this PR). |
|
Gerrit CL has been approved, please wait for a reviewer to merge it. There are also new comments on the CL, please review them and respond if necessary because reviewer might have requested clarifications or possibly some final changes and will not merge the CL until their questions have been answered. |
|
Gerrit CL has been approved, please wait for a reviewer to merge it. There are also new comments on the CL, please review them and respond if necessary because reviewer might have requested clarifications or possibly some final changes and will not merge the CL until their questions have been answered. |
Summary
The
Add type annotationfix/assist (triggered e.g. byprefer_typing_uninitialized_variables) infers an uninitialized variable's type from assignments found anywhere in the rest of the enclosing block, without regard to whether those assignments are guaranteed to run. For a variable only assigned inside a loop body, a single-branchif, or atryblock, that code might not execute, so the variable could still hold its implicit initial value,null, at the point of use — but the fix inferred a non-nullable type anyway.This adds a conservative check for whether at least one of the remaining statements is guaranteed to assign the variable — accounting for
if/else(both branches),try/finally,do-while(body always runs once), labeled statements, and immediately-invoked closures — and makes the inferred type nullable when it isn't.Fixes #64015
Test plan
if, andtry/catchcases, plus counterexamples (if/else,do-while,try/finally) that must remain non-nullablepkg/analysis_server/test/src/services/correction/fix/add_type_annotation_test.dart— 43/43 passpkg/analysis_server/test/src/services/correction/assist/add_type_annotation_test.dart— 75/75 passpkg/analysis_server/test/src/services/correction/test_all.dart(full correction suite) — 6385/6385 pass, no regressionsdart format/dart analyzeclean on changed files