Skip to content

Should [DisallowNull] apply to value types or nullable value types? #36009

@jcouv

Description

@jcouv

A few options:

  1. no warning (as today)
  2. warn for passing a possibly null value to [DisallowNull]int? parameter
  3. disallow [DisallowNull] on that parameter. If we do that, then we should probably also disallow on string?.
        [Fact]
        public void DisallowNull_04()
        {
            var source =
@"using System.Runtime.CompilerServices;
class Program
{
    static void F1([DisallowNull]int i) { }
    static void F2([DisallowNull]int? i) { }
    static void M1(int i1)
    {
        F1(i1);
        F2(i1);
    }
    static void M2(int? i2)
    {
        F2(i2); // I expected a warning here (jcouv)
        if (i2 == null) return;
        F2(i2);
    }
}";
            var comp = CreateCompilation(new[] { DisallowNullAttributeDefinition, source }, options: WithNonNullTypesTrue());
            comp.VerifyDiagnostics();
        }

Branch disallow-nullable

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