StinkyBooleanExpression

StinkyBooleanExpression

Summary

Do not use the conditional operator (?:) where either the second or third operand is a bool literal (true or false), resulting in bool values.

Default severity

Warning

Description

There are some stinky boolean expressions with a conditional operator that can be replaced with the && (conditional logical AND) or || (conditional logical OR) operator, as follows:

(b1 ? b2 : false)
(b1 ? true : b2)

where the type of b1 and b2 is bool. It is possible to replace the former conditional expression with b1 && b2, the latter with b1 || b2.

Remarks

The diagnostics IDE0057 providing Simplify conditional expressions refactoring, which is available with Visual Studio 2019 version 16.6 preview 2, includes the same feature as this analyzer.

Code fix

The code fix provides an option replacing the conditional operator with the && or || operator. However, if the diagnostics IDE0057 provides an option "Simplify conditional expression" with Visual Studio, you should use it.

Example

Diagnostic

_ = (b1 ? b2 : false);
_ = (b1 ? false : b2);

_ = (b1 ? true : b2);
_ = (b1 ? b2 : true);

Code fix

_ = ((b1) && (b2));
_ = (!(b1) && (b2));

_ = ((b1) || (b2));
_ = (!(b1) || (b2));