dotnet/roslyn

No warning/error Severity CS8602 Dereference of a possibly null reference for optional argument

Offen

#52.792 geöffnet am 21.04.2021

 (1 Kommentar) (0 Reaktionen) (1 zugewiesene Person)C# (4.257 Forks)batch import
Area-CompilersBugFeature - Nullable Reference Typeshelp wanted

Repository-Metriken

Stars
 (20.414 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (256 gemergte PRs in 30 T)

Beschreibung

Microsoft Visual Studio Enterprise 2019 Version 16.9.4 (.NET Core 3.1 and .NET 5)

Looks like some edge case is missed when checking possible null references of optional parameters. In the code below the value for data reference will be null by default.

Function like: static void BadUse([Optional] Data data) => Console.WriteLine($"Data: ${data.Count}");

Expected Behavior: Warning CS8602 Dereference of a possibly null reference.

Actual Behavior: No suggestion/warning/error and null reference exception thrown on execution.

Note that it works fine when you do add DefaultParameterValue(null) attribute like (we get warning/error): static void BadUse([Optional, DefaultParameterValue(null)] Data data) => Console.WriteLine($"Data: ${data.Count}");

Contributor Guide