dotnet/roslyn

Got a false CS8602 warnning when use Null-conditional operator and Null-coalescing opreator in expression

Open

#78,432 创建于 2025年5月3日

在 GitHub 查看
 (2 评论) (0 反应) (0 负责人)C# (4,257 fork)batch import
Area-CompilersBugFeature - Nullable Reference Typeshelp wanted

仓库指标

Star
 (20,414 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 256 个 PR)

描述

Version Used:

  • csc.dll: 4.13.0-3.25155.17+398aa319196d4684c751a3d7c75c847e519a393c
  • .NET SDK: 9.0.203-dc7acfa194

Steps to Reproduce:

  1. Compile follow code
  2. Got a false CS8602 warnning

Sharplab

using System;

AnotherTestClass? anotherTestClass = null;
if (!anotherTestClass?.IsTrue ?? false)
{
    // CS8602 at `anotherTestClass`
    Console.WriteLine(anotherTestClass.IsTrue
        ? "Cancellation requested"
        : "Cancellation not requested");
}
Console.WriteLine("Test method ended");

class AnotherTestClass
{
    public bool IsTrue = false;
}

Diagnostic Id:

CS8602: Dereference of a possibly null reference.

Expected Behavior:

No warnning

Actual Behavior:

Got a falseCS8602 warnning

贡献者指南