Skip to main content

CRR0006 - Suspect variable reference in null-check following as-cast

This analyzer detects the following situation.

  1. The variable is assigned with a type-casted value of another variable.
  2. The original variable is checked for null (Nothing in VB).

The example is shown below.

public void Test(object obj){
    var str = obj as string;
    if (obj == null) return; // CRR0006
    //...
}

In this case, the null-check is performed against the wrong variable, because such code is unable to detect the situation when the null value appeared as a result of type-casting a non-null object. You need to change the code as follows.

public void Test(object obj){
    var str = obj as string;
    if (str == null) return;
    //...
}