dotnet/roslyn

'IDE0031 Use null propagation' incorrectly suggested for LINQ expression with left join (DefaultIfEmpty)

オープン

#47,788 opened on 2020/09/17

 (1 件のコメント) (1 件のリアクション) (0 人の担当者)C# (4,257 件のフォーク)batch import
Area-CompilersBugFeature - IDE0031IDE-CodeStylehelp wanted

Repository metrics

Stars
 (20,414 個のスター)
PR merge metrics
 (平均マージ 6d 17h) (30d で 256 merged PRs)

説明

[Update: IDE issue was traced to a problem with the GetSymbolInfo Compiler API, in part. See details below. May relate to another issue with visitors on this LINQ query]

Version Used: 16.7.3

The motivating situation that led to me filing https://github.com/dotnet/roslyn/issues/33992 was not covered by the fix for the minimal repro I gave in that issue.

Using the standard LINQ left outer join pattern:

_ = from itemA in new[] { new { X = (int?)1 } }.AsQueryable()
    join itemB in new[] { new { X = (int?)2 } }.AsQueryable() on itemA.X equals itemB.X into itemBGroup
    from itemB in itemBGroup.DefaultIfEmpty()
    //     ↓ IDE0031 Use null propagation
    select itemB == null ? null : itemB.X;

After applying the suggested fix:

_ = from itemA in new[] { new { X = (int?)1 } }.AsQueryable()
    join itemB in new[] { new { X = (int?)2 } }.AsQueryable() on itemA.X equals itemB.X into itemBGroup
    from itemB in itemBGroup.DefaultIfEmpty()
    //     ↓↓↓↓↓↓↓↓ CS8072 An expression tree lambda may not contain a null propagating operator.
    select itemB?.X;

/cc @CyrusNajmabadi

コントリビューターガイド