Skip to content

Nullable reference types | Nullable warning context – incomplete list of not null compiler detected state? #25027

Closed
@deep-outcome

Description

@deep-outcome

From Nullable warning context

The nullable warning context is distinct from the nullable annotation context. Warnings can be enabled even when the new annotations are disabled. The compiler uses static flow analysis to determine the null state of any reference. The null state is either not null or maybe null when the nullable warning context isn't disabled. If you dereference a reference when the compiler has determined it's maybe null, the compiler warns you. The state of a reference is maybe null unless the compiler can determine one of two conditions:

  1. The variable has been definitely assigned a non-null value.
  2. The variable or expression has been checked against null before de-referencing it.

The compiler generates warnings when you dereference a variable or expression that is maybe null in a nullable warning context.

In sample bellow on str3 variable one can see that compiler directly counts on presence of ? type annotation to make conclusions about null state.

Particularly it claims that anything returned from Str3 is not null. I am not 100 % sure if this fits to # 1 rule – Str3 returns null as string with no problem thus it is obvious that presence/absence of ? annotation cannot serve as null state indication what in turn obviously is not taken into account on str3 dereference.

It seems it could be also bug in analysis.

#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
#nullable disable
#nullable enable warnings
class NullableWarningContext
{
  static string Str1 => "string";
  static string? Str2 => "";
  static string Str3 => null;
  
  static void Test2 ()
  {
    {
      string? str1 = Str1;
      var len1 = str1.Length;
    }
    {
      var str2 = Str2;
      var len2 = str2.Length;  // CS8602  Dereference of a possibly null reference. Caused by ? annotation.
    }
    {
      string? str3 = Str3;
      var len3 = str3.Length;
    }
    {
      string str4 = null;
      var len4 = str4.Length; // CS8602  Dereference of a possibly null reference. Caused by known null state.
    }
  }
}
#nullable restore
#pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

P.S.

Repeating is good for memorizing but in technical documents it is usual considered as unintended/unwelcome element.

If you dereference a reference when the compiler has determined it's maybe null, the compiler warns you.

The compiler generates warnings when you dereference a variable or expression that is maybe null in a nullable warning context.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions