Skip to content

Support converting more ?? expressions to parameter null checks #58699

@RikkiGibson

Description

@RikkiGibson

We are punting on, but would like to eventually support, more scenarios for the parameter null checking analyzer/code fix such as:

// before
public C(string s)
    => _s = s ?? throw new ArgumentNullException();

// after
public C(string s!!)
    => _s = s;
// before
public C(string s) : base(s ?? throw new ArgumentNullException()) { }

// after
public C(string s!!) : base(s) { }
// before
public C(string s)
{
    ArgumentNullException.ThrowIfNull(s, nameof(s));
}

// after
public C(string s!!)
{
}
// before
public C(string s) : base(s == null ? 0 : s.Length)
{
    if (s == null) throw new ArgumentNullException();
}

// after
public C(string s!!) : base(s.Length)
{
}
// before
public C(string s) : base(s?.Length)
{
    if (s == null) throw new ArgumentNullException();
}

// after
public C(string s!!) : base(s.Length)
{
}

Migrated from #58182 (comment)

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions