dotnet/roslyn

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

Aperta

#43.587 aperta il 23 apr 2020

 (0 commenti) (0 reazioni) (0 assegnatari)C# (4257 fork)batch import
Area-CompilersBugFeature - Nullable Reference Typeshelp wanted

Metriche repository

Star
 (20.414 stelle)
Metriche merge PR
 (Merge medio 6g 17h) (256 PR mergiate in 30 g)

Descrizione

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

Guida contributor