Skip to content

ITenantIdentificationStrategy.TryIdentifyTenant nullable annotation incomplete #40

@bbarry

Description

@bbarry

Describe the Bug

Nullable annotation on ITenantIdentificationStrategy.TryIdentifyTenant is incorrect. It is missing an attribute [NotNullWhen(true)] to follow common try-get-* patterns seen commonly in .NET.

Steps to Reproduce

public void DoSomething(ITenantIdentificationStrategy strategy)
{
    if (strategy.TryIdentifyTenant(out var id))
    {
        Foo(id);
    }
}
public void Foo(object input) { }

Expected Behavior

No compiler warning.

Actual Behavior

CS8604: Possible null reference argument for parameter 'input'

Additional Info

https://github.com/dotnet/csharplang/blob/main/meetings/2019/LDM-2019-05-15.md#conditional-postconditions

Mscorelib offers some guidelines intended to be specific to that library but that turn out to be in general good to follow: The api guidelines for that repo say https://github.com/dotnet/runtime/blob/e12e2fa6cbdd1f4b0c8ad1b1e2d960a480c21703/docs/coding-guidelines/api-guidelines/nullability.md?plain=1#L88 and from this the following bullet seems to apply:

  • DO annotate non-generic out reference type arguments on Try methods as being nullable and with [NotNullWhen(true)] (e.g. TryCreate([NotNullWhen(true)] out Semaphore? semaphore)). For non-generic arguments, the argument should be nullable because a failed call will generally store null into the argument, and it should be [NotNullWhen(true)] because a successful call will store non-null.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions