Code Inspection: The given expression of 'is' operator is always of the provided type
JetBrains Rider detects issues when there is a compatibility check (
expr is type) between reference types. Such a check might be redundant if the
is statement always evaluates to
true. According to the C# documentation, "The is statement is true if expr is non-null and the object that results from evaluating the expression can be converted to type; otherwise, it returns false."
If the result of the expression on the left is always of the specified type, we should check the expression for null to be able to eliminate the cast, because if the expression evaluates to
null, the result of
is will be
JetBrains Rider detects whether the expression (
cat) is compatible with the type (
Animal), and, if it is always compatible, JetBrains Rider aims to eliminate the cast. To be able to do so, it suggests that you compare the result of the expression (
cat) with null. The comparison will ensure that the result of the
if statement without cast will be the same as it would be with the cast used.
This warning is similar to the compiler warning CS0183: The given expression is always of the provided ('type') type (which JetBrains Rider also detects), but it deals with reference types, while the compiler warning occurs when there is conversion of value types.
|Suboptimal code||After the quick-fix|
| || |