Description
(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)... }