C6236
The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.
The latest version of this topic can be found at C6236.
warning C6236: (<expression> || <non-zero constant>) is always a non-zero constant
This warning indicates that a non-zero constant value, other than one, was detected on the right side of a logical OR operation that occurs in a test context. Logically, this implies that the test is redundant and can be removed safely. Alternatively, it suggests that the programmer may have intended to use a different operator, for example, the equality (==), bitwise-AND (&) or bitwise-XOR (^) operator, to test for a specific value or flag.
This warning is not generated for the common idiom when the non-zero constant is 1, because of its use for selectively enabling code paths at compile time. However, the warning is generated if the non-zero constant is formed by an expression that evaluates to 1, for example, 1 + 0.
This code shows how warning C6236 can appear. Because INPUT_TYPE is not 0, the expression n || INPUT_TYPE is always non-zero, and the else clause is never executed. However, INPUT_TYPE is a constant with a value other than one, which suggests that it is meant as a value for comparison:
#define INPUT_TYPE 2
#include <stdio.h>
void f( int n )
{
if ( n || INPUT_TYPE ) // analysis warning C6236
{
puts( "Always gets here" );
}
else
{
puts( "Never enters here" );
}
}
The following code instead uses a bitwise-AND (&) operator to test whether the INPUT_TYPE bit of the input parameter n is set:
#define INPUT_TYPE 2
#include <stdio.h>
void f( int n )
{
if ( n & INPUT_TYPE ) // no warning
{
puts( "Bitwise-AND comparison is true" );
}
else
{
puts( "Bitwise-AND comparison is false" );
}
}