dotnet/roslyn

[MemberNotNull] is not processed for write acceses to ref returning properties

Open

#43,587 opened on Apr 23, 2020

View on GitHub
 (0 comments) (0 reactions) (0 assignees)C# (4,257 forks)batch import
Area-CompilersBugFeature - Nullable Reference Typeshelp wanted

Repository metrics

Stars
 (20,414 stars)
PR merge metrics
 (Avg merge 6d 17h) (256 merged PRs in 30d)

Description

Version Used:

commit a1b867054dff97e5e5f44c7ad742c8ffb740b2eb
Merge: 8f4fdad99ae 6bf71e1a37d
Author: msftbot[bot] <48340428+msftbot[bot]@users.noreply.github.com>
Date:   Wed Apr 22 09:50:57 2020 +0000

    Merge pull request #43554 from CyrusNajmabadi/renameOOP4

    Move the 'finding' portoin of rename out of process.

Steps to Reproduce:

Compile the following code

using System.Diagnostics.CodeAnalysis;

class C
{
  public string? Prop1 { get; set; }
  public string? Prop2 { get; set; }

  private int field;

  [MemberNotNull(nameof(Prop1))]
  private ref int Init
  {
    [MemberNotNull(nameof(Prop2))]
    get
    {
      Prop1 = Prop2 = "";
      return ref field;
    }
  }                                    

  void M1()
  {
    Init = 1;
    Prop1.ToString(); // warning
    Prop2.ToString(); // warning
  }

  void M2()
  {
    _ = Init;
    Prop1.ToString();
    Prop2.ToString();
  }
}

Expected Behavior: No warnings. Both methods contain calls to ``get_Init()that is guaranteed to initialize bothProp1andProp2` to non-nullable values before the methods access them

Actual Behavior: CS8602: Dereference of a possibly null reference. warnings reported for Prop1 and Prop2 in M1 but not M2

Contributor guide