OmniSharp / OmniSharp/csharp-language-server-protocol

Container<T>.implicit operator(T[]) does not accept null, but is implemented to handle it

Open
#659 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
638
Forks
109
Avg merge
1m
Merged PRs (30d)
2

Description

Code for context:

[return: NotNullIfNotNull("items")]
public static implicit operator Container<T>?(T[] items) => items switch {
    not null => new Container<T>(items),
    _        => null
};

The annotation only accepts a non-null T[], so the _ branch of the switch cannot be used (assuming the caller is adhering to the annotated requirement).

However, this causes annoying workarounds when constructing objects where the value could be null, e.g:

string[]? commitCharacters = SomeCalculation();
CompletionItem c = new CompletionItem()
{
    ...,
    // error CS8604: Possible null reference argument for parameter 'items' in 'Container<string>.implicit operator Container<string>?(string[] items)'.
    CommitCharacters = commitCharacters,
    // alternatively, this can be written inconveniently as:
    CommitCharacters = commitCharacters ?? (Container<string>)null; // cast required to tell the compiler it isn't (string[])null
}

The same issue exists for the constructor, which prevents using new Container<string>(commitCharacters) either.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/Protocol/Models/Container.cs around line 63 and inspect the constructor as well. Confirm that nullable arrays can be passed to both the implicit conversion and constructor, while a null value remains null without requiring a cast.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.