dotnet/roslyn

Incorrect "Nullability doesn't match the target delegate" when tuple element names don't explicitly match

Open

#74,493 创建于 2024年7月23日

在 GitHub 查看
 (6 评论) (2 反应) (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: 17.11.0 Preview 3.0

The following code erroneously causes this warning to appear for each lambda in the Error List and at the command line, though squiggles are not shown:

⚠️ CS8621 Nullability of reference types in return type of 'lambda expression' doesn't match the target delegate 'Func<(string, object?), ?>' (possibly because of nullability attributes).

class C
{
    void M((string Name, object? Value)[] parameters)
    {
        _ = parameters.Append(("A", "A")).ToDictionary(
            tuple => tuple.Name,
            tuple => tuple.Value);
    }
}

If you name the elements in the tuple expression to exactly match the tuple element names in the collection, the warning goes away.

class C
{
    void M((string Name, object? Value)[] parameters)
    {
        _ = parameters.Append((Name: "A", Value: "A")).ToDictionary(
            tuple => tuple.Name,
            tuple => tuple.Value);
    }
}

For each element where a name is missing or does not exactly match, a nullability warning returns.

贡献者指南