>
>
>
V3003. The use of 'if (A) {...} else if…


V3003. The use of 'if (A) {...} else if (A) {...}' pattern was detected. There is a probability of logical error presence.

The analyzer has detected a potential error in a construct consisting of conditional statements.

Consider the following example:

if (a == 1)
  Foo1();
else if (a == 2)
  Foo2();
else if (a == 1)
  Foo3();

In this code, the 'Foo3()' method will never get control. We are most likely dealing with a logical error here and the correct version of this code should look as follows:

if (a == 1)
  Foo1();
else if (a == 2)
  Foo2();
else if (a == 3)
  Foo3();

In practice though, errors of this type can take more complicated forms, as shown below.

For example, the analyzer has found the following incorrect construct.

....
} else if (b.NodeType == ExpressionType.Or || 
           b.NodeType == ExpressionType.OrEqual){
                   current.Condition = ConstraintType.Or;
} else if(...) { 
.... 
} else if (b.NodeType == ExpressionType.OrEqual ||
           b.NodeType == ExpressionType.Or){
                   current.Condition = ConstraintType.Or | 
                   ConstraintType.Equal;
} else if(....

In this example, the uppermost if statement checks the following condition:

b.NodeType == ExpressionType.Or || 
b.NodeType == ExpressionType.OrEqual.

while the lowermost if statement checks a condition with the same logic but written in a reverse order, which is hardly noticeable for a human yet results in a runtime error.

b.NodeType == ExpressionType.OrEqual ||  
b.NodeType == ExpressionType.Or

This diagnostic is classified as:

You can look at examples of errors detected by the V3003 diagnostic.