dotnet / dotnet/roslyn

Nullable check ignores NotNullIfNotNullAttribute when the argument is method group

Open
#77,475 5 comments 0 reactions 0 assignees View on GitHub
Area-Compilers Bug Feature - Nullable Reference Types help wanted
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

**.NET SDK 9.0.200**:

**Steps to Reproduce**:

```cs
#nullable enable
using System.Linq; // Enumerable.Select

string[] a = ["1", "a"]; // element not null

_ = a.Select(Foo) // returns string?[]
.First().ToString(); // CS8602, unexpected

_ = a.Select(t => Foo(t)) // returns string[]
.First().ToString(); // correct, expected

[return: NotNullIfNotNull(nameof(x))]
string? Foo(string? x) => x;
```

**Diagnostic Id**:

CS8602

**Expected Behavior**:

Nullable check Does not ignore NotNullIfNotNullAttribute when the argument is method group

**Actual Behavior**:

Nullable check ignores NotNullIfNotNullAttribute when the argument is method group

Contributor guide

Open the contributing guide

Research direction

Use the supplied nullable-enabled C# reproduction as the starting test case; trace how a method-group argument to Enumerable.Select is analyzed versus the equivalent lambda. Done means the method-group call preserves NotNullIfNotNull flow and no CS8602 is reported, without changing the already-correct lambda case.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.