The result of as is used directly, which throws NullReferenceException instead of a clear cast error

tx0/csharp:cast-after-asrecommendedwarningreliability/api-misusedepth fastbug

What it finds

(shape as Circle).Radius throws NullReferenceException when shape is not a circle, which hides what went wrong. A cast, ((Circle)shape).Radius, fails with a message that names the types; a pattern, shape is Circle c, handles the other case.

From the rule's source, cast_after_as.rs.

Example from the rule's tests
class Shapes{    double Radius(object shape)    {        var a = (shape as Circle).Radius;tx0 reports: shape as Circle is used without a null check        var b = (shape as Circle)?.Radius ?? 0;        return shape is Circle c ? c.Radius + a + b : 0;    }} class Circle{    public double Radius;}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:cast-after-as" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/csharp:cast-after-as] reason="..."

tx0 explain tx0/csharp:cast-after-as shows the same in the terminal. Found a false positive? Report it.