V3031. An excessive check can be simplified. The operator '||' operator is surrounded by opposite expressions 'x' and '!x'.
The analyzer has detected a code fragment that can be simplified. In this code, expressions with opposite meanings are used as operands of the '||' operator. This code is redundant and, therefore, can be simplified by using fewer checks.
Consider this example:
if (str == null || (str != null && str == "Unknown"))
In the "str != null && str == "Unknown"" expression, the condition "str != null" is redundant since an opposite condition, "str == null", is checked before it, while both expressions act as operands of operator '||'. So the superfluous check inside the parentheses can be left out to make the code shorter:
if (str == null || str == "Unknown"))
Redundancy may be a sign of an error – for example use of a wrong variable. If this is the case, the fixed version of the code above should look like this:
if (cond || (str != null && str == "Unknown"))
Sometimes the condition is written in a reversed order and on first glance can not be simplified:
if ((s != null && s == "Unknown") || s == null)
It seems that we can't get rid neither of a (s!=null) nor of a (s==null) check. This is not the case. This expression and the case described above, can be simplified:
if (s == null || s == "Unknown")
You can look at examples of errors detected by the V3031 diagnostic.