[analysis_server] Infer nullable type in 'Add type annotation' when assignment isn't unconditional - #64046
[analysis_server] Infer nullable type in 'Add type annotation' when assignment isn't unconditional#64046hkarmoush wants to merge 2 commits into
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. |
Reorder the new test methods alphabetically to match the repo's
enforced member-sort convention; this was the cause of the CQ
presubmit failure ("Unsorted file").
|
https://dart-review.googlesource.com/c/sdk/+/536161 has been updated with the latest commits from this pull request. |
1 similar comment
|
https://dart-review.googlesource.com/c/sdk/+/536161 has been updated with the latest commits from this pull request. |
|
Gerrit CL has build or test failures as well as new comments, please review them in Gerrit. You will need to address both the comments and the failures before requesting another review. If a reviewer requested changes, push new commits to this PR and it will be automatically copied to Gerrit. After that you can mark reviewer comments as resolved in Gerrit and request another round of reviews. Note: when you add comments in Gerrit they only become visible after you send them by clicking Reply and Send. |
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