Warning C6237

('zero' && 'expression') is always zero. 'expression' is never evaluated and may have side effects

This warning indicates that a constant value of zero was detected on the left side of a logical-and operation that occurs in a test context. The resulting expression always evaluates to false. Therefore, the right side of the logical-AND operation isn't evaluated. This language feature is referred to as "short-circuit evaluation."

Remarks

You should examine the right side of the expression carefully: Ensure that any side effects such as assignment, function call, increment, and decrement operations needed for proper functionality aren't affected by the short-circuit evaluation.

The expression (0 && n) produces no side effects and is commonly used to selectively choose code paths.

Code analysis name: ZEROLOGICALANDLOSINGSIDEEFFECTS

Example

The following code shows various code samples that generate this warning:

#include <stdio.h>
#define INPUT_TYPE 0

int test();

// side effect: n not incremented
void f1( int n )
{
   if(INPUT_TYPE && n++) //warning: 6237
   {
      puts("code path disabled");
   }
   else
   {
      printf_s("%d - n was not incremented",n);
   }
}

// side effect: test() not called
void f2( )
{
   if(INPUT_TYPE && test()) //warning: 6237
   {
      puts("code path disabled");
   }
   else
   {
      puts("test() was not called");
   }
}

//side effect: assignment and function call did not occur
void f3( int n )
{
   if(INPUT_TYPE && ( n=test() )) //warning: 6237
   {
      puts("code path disabled");
   }
   else
   {
      printf_s("%d -- n unchanged. test() was not called", n);
   }
}

To correct this warning, use the following code:

#include <stdio.h>
#define INPUT_TYPE 0
int test();

void f1( int n )
{
   if(INPUT_TYPE)
   {
      if(n++)
      {
         puts("code path disabled");
      }
   }
   else
   {
      puts("n was not incremented");
   }
}

void f2( )
{
   if(INPUT_TYPE)
   {
      if( test() )
      {
         puts("code path disabled");
      }
   }
   else
   {
      puts("test() was not called");
   }
}

void f3( int n )
{
   if(INPUT_TYPE)
   {
      n = test();
      if( n )
      {
         puts("code path disabled");
      }
   }
   else
   {
      puts("test() was not called");
   }
}

See also

C Logical Operators