Skip to content

Incorrect CS8604 with captured variable in ternary operator #40561

@MrJul

Description

@MrJul

Version Used:
VisualStudio.16.Release/16.4.2+29613.14
C# Tools 3.4.1-beta4-19610-02+c4e5d138903b899477649a17f197abd2bcb22f9e

Steps to Reproduce:
In the following program, s in never reassigned later in M2 so the compiler should know that it's never null when M1 is called, even though it's captured. Writing the equivalent code using if/else doesn't produce a warning.

#nullable enable

using System;

class C {
    void M1(string s) { }
    Action? M2(string? s) => s != null ? () => M1(s) : (Action?) null;
}

Expected Behavior:
No warning.

Actual Behavior:
Warning CS8604: Possible null reference argument for parameter 's' in 'void C.M1(string s)'.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions