Skip to content

Nullable inference in conditional expressions #36096

@RikkiGibson

Description

@RikkiGibson
#nullable enable

public class Program
{
    static void M(bool flag, string? x)
    {
        if (flag ? x?.Length == 2 : x?.Length == 3)
        {
            x.ToString(); // warns today, but perhaps shouldn't warn
        }
    }
}

SharpLab

Note that if flag is changed to the constant true, the warning is not given.

The suggestion here is that if either conditional expression arm has a split state, we join the StateWhenTrue on each arm, and join the StateWhenFalse on each arm, and use the resulting split state as the resulting state of the conditional expression.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions