dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.02k stars 4.03k forks source link

Incorrect nullability warnings on increment/decrement operators on nullable variables #50108

Open TessenR opened 3 years ago

TessenR commented 3 years ago

Version Used:

Branch master (19 Dec 2020)
Latest commit 80e9227 by msftbot[bot]:
Merge pull request #50073 from dotnet/merges/release/dev16.9-to-master

Merge release/dev16.9 to master

Steps to Reproduce:

#nullable enable
class C
{
  void Test(C? c)
  {
    c = new C();
    c++;
  }

  public static C? operator ++(C? c) => c;
}

Expected Behavior: No warnings. The variable c allows null values, the operator's parameter allows null values. There's nothing to warn about.

Actual Behavior: warning CS8601: Possible null reference assignment. is repoted for c++

Notes Note that if you remove the assignment c = new C() the warning disappears. It looks like Roslyn uses the actual nullability state of the variable c to determine allowed nullability instead of its annotation

jcouv commented 3 years ago

Assigned to @RikkiGibson for load balancing. Thanks