marinasundstrom / CheckedExceptions

Enforce better exception handling in C#/.NET by ensuring exceptions are explicitly handled.
MIT License
7 stars 1 forks source link

Ignore ``ArgumentNullException`` for properties and method arguments in nullable context #31

Closed marinasundstrom closed 1 week ago

marinasundstrom commented 1 week ago

Perhaps it would be possible to remove theArgumentNullException warnings when method arguments and properties are non-nullable in a nullable context.

Since those warnings would be annoying and even redundant for those targets in a nullable context. You already get indication by nullable. If ArgumentNullException is thrown then it is your own fault, as a developer.

Perhaps warn when trying to override with !.

marinasundstrom commented 1 week ago

One case:

This might be flagged as a potential for NullReferenceException since Task is not annotated as nullable:

    public Task Foo2
    {
        get
        {
            return null!;
        }
    }

Solution 1: In nullable context

    public Task? Foo2
    {
        get
        {
            return null!;
        }
    }

Solution 2: In non-nullable context

    public Task Foo2
    {
        [Throws(typeof(NullReferenceException))]
        get
        {
            return null!;
        }
    }
marinasundstrom commented 1 week ago

I have come up with a solution that doesn't cover this, which emits a warning:

    /// <summary>
    /// 
    /// </summary>
    /// <value></value>
    /// <exception cref="ArgumentNullException">
    /// The value provided that is set is null.
    /// </exception>
    public string? Value2
    {
        get;
        set;
    }

    public void Foo6()
    {
        Value2 = null;
    }

Which would be fine for now, since it overrides nullability. Although incorrect, you shouldn't declare that exception over a nullable parameter or property.