λ1008: Prefer ToNullable over Match
A Match that returns null for None and the value itself for Some should be expressed as ToNullable.
Cause
Match is called on an Option<T> where the none branch is null (or a function returning null)
and the some branch is the identity function, resulting in a T?.
Reason for rule
Converting an Option<T> to a nullable value is a common interop step at the boundary to code that does not use Option.
ToNullable names this conversion. The Match form requires a cast such as (string?)null to make the type inference work,
which is noise that is easy to get wrong.
How to fix violations
Replace the Match call with ToNullable(). A code fix is available.
Examples
Disallowed
static string? Example(Option<string> option)
=> option.Match(none: (string?)null, some: x => x);
static int? Example(Option<int> option)
=> option.Match(none: () => (int?)null, some: Identity);
Allowed
static string? Example(Option<string> option)
=> option.ToNullable();
static int? Example(Option<int> option)
=> option.ToNullable();
// The some branch transforms the value, so this is a legitimate Match.
static string? Example(Option<string> option)
=> option.Match(none: (string?)null, some: x => x + "!");