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, it 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.
Remarks
This warning isn't 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.
Code analysis name: LOGICALORNONZERO
Example
This code shows how warning C6236 can appear. Because INPUT_TYPE
isn't 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's 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" );
}
}