-
Notifications
You must be signed in to change notification settings - Fork 30
Description
I'm seeing a lot of C# 8 Nullable Reference Type issues.
Issue 1
These issues remind me to turn on nullable context, which is already enabled project wide in the csproj.
Issue 2
See Example 1 for a constructor issue, declaring a property as nullable in an API indicates you intend for the consumer to pass nulls. In this case we don't (and throw exceptions).
Issue 3
See example 2, There's no way to declare a var as nullable and the target of the foreach is non-null.
Issue 4
See example 3, There's no way to declare a var as nullable.
Code snippets are from OAT which triggers many of the same types of issues: https://github.com/microsoft/OAT
For context, I do have nullable reference types enabled project wide.
Example 1
This code triggers "Enable Nullable Context and declare constructor parameters as nullable. I explicitly don't want nulls passed in, but am already handling nulls. Setting these parameters to be nullable would indicate to consumers that they may expect reasonable behavior if they pass in nulls. This is explicitly an API you may not pass nulls to.
public Violation(string description, Rule rule, Clause[] clauses)
{
this.description = description ?? throw new ArgumentNullException(nameof(description), $"{nameof(description)} may not be null");
this.rule = rule ?? throw new ArgumentNullException(nameof(rule), $"{nameof(rule)} may not be null");
this.clauses = clauses ?? throw new ArgumentNullException(nameof(clauses), $"{nameof(clauses)} may not be null");
}Example 2
This is a false positive. The target is both not null and there's no way to declare a "var" as nullable.
Example 3
This is another false positive. There's no way to declare a var as nullable. It's fine that this is may be null here.
public bool Applies(Rule rule, object? state1 = null, object? state2 = null)
{
if (rule != null)
{
var sample = state1 is null ? state2 : state1;