dotnet / dotnet/roslyn

Incorrect nullability analysis for extension method with inference

Open
#78,022 0 comments 1 reaction 1 assignee Claimed by @RikkiGibson View on GitHub
Area-Compilers Feature - Extension Everything Feature - Nullable Reference Types
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

In the example below, the only argument for the extension method is `oNull` which may be null.
So `oNull.Id` has signature `object? ()`.

The conversion to an explicit type works properly (warn on nullability issue with return type).
But in the function type scenario, it looks like `var x` isn't analyzed with that inferred type.

Also, the semantic model (`GetSymbolInfo`) has incorrect nullability for both member accesses.

This issue also impacts the semantic model for deconstructions.

This issue also impacts new extension members.

```c#
#nullable enable

object? oNull = null;
var x = oNull.Id;
x().ToString(); // Missing nullabiliy warning, indicating that the type inferred for `var` is incorrect

System.Func y = oNull.Id; // correct warning on return type of delegate type

static class E
{
public static T Id(this T t) => t;
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.