Skip to content

Flow analysis feature: Assignment Promotion #1844

Open
@stereotype441

Description

@stereotype441

(Note: this feature request was raised in #1420 under the heading "Feature: Assignment Promotion", but since it's a flow analysis request, and that issue is tagged "field-promotion", I'm splitting the feature request out to its own separate issue).

It would be nice to allow a local variable assignment of the form id assignmentOp expression (potentially parenthesized) to act like the variable itself if used in a test. We currently allow if (x != null) ... to promote x. This change would also allow if ((x = something) != null) ... to promote x.

It only affects the left-most variable of an assignment, so if ((x = y = something) != null) ... will only promote x, not both x and y (although that's also an option if we really want it - treat both x and the assigned expression as being tested)

If you do if ((x += 1) != null) ... that still works, but there aren't that many operators which return a nullable result, so the usefulness is limited.

This is a very small feature, but it allows you to do promotion by:

int? c;
if ((c = this.capacity) != null) { ... use(c)... }

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problemsflow-analysisDiscussions about possible future improvements to flow analysis

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions