Why is this an issue?

In C#, the is type testing operator can be used to check if the run-time type of an object is compatible with a given type. If the object is not null, then the is operator performs a cast, and so performing another cast following the check result is redundant.

This can impact:

How to fix it

Use pattern macthing to perform the check and retrieve the cast result.

Code examples

Noncompliant code example

if (x is Fruit)  // Noncompliant
{
  var f = (Fruit)x; // or x as Fruit
  // ...
}

Compliant solution

if (x is Fruit fruit)
{
  // ...
}

Resources

Documentation