V3131. The expression is checked for compatibility with the type 'A', but is casted to the 'B' type.


The analyzer detected a likely error that has to do with checking if an expression is compatible with one type and casting it to another type inside the body of the conditional statement.

Consider the following example:

if (obj is A)
{
  return (B)obj;
} 

The programmer must have made a mistake, since a type conversion like that is very likely to cause a bug. What was actually meant is either to check the expression for type 'B' or cast it to type 'A'.

This is what the correct version could look like:

if (obj is B)
{
  return (B)obj;
}

According to Common Weakness Enumeration, potential errors found by using this diagnostic are classified as CWE-704.


Bugs Found

Checked Projects
355
Collected Errors
13 303