A value is tested with is and then cast to the same type

tx0/csharp:type-check-then-caststrictwarningperformance/algorithmicdepth fastperformance, style

What it finds

if (shape is Circle) { var c = (Circle)shape; ... } checks the type twice. A pattern, if (shape is Circle c), tests and converts at once.

From the rule's source, type_check_then_cast.rs.

Example from the rule's tests
class Shapes{    double Radius(object shape)    {        if (shape is Circle)        {            return ((Circle)shape).Radius;tx0 reports: the value was already tested for this type        }        if (shape is Circle c)        {            return c.Radius;        }        return 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 strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:type-check-then-cast" = "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:type-check-then-cast] reason="..."

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