llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Potentially misleading diagnistics in -Wconditional-uninitialized

Open earnol opened this issue 1 year ago • 0 comments

The conditional-uninitialized diagnostics checker issue warnings even in cases when the initialization is clearly performed. Here is the example:

int a(int p1) 
{    
  int cv = 1;
  int ibn;
  if ((cv < 5) && (p1 == 1))
  {
    ibn = 2;
    p1 += 1;
    cv++;
  }
  if(cv == 2)
  {
    return ibn; 
  } 
  return 8;
}

The warning generated is: "warning: variable 'ibn' may be uninitialized when used here [-Wconditional-uninitialized]". In this case depending on the passed value p1 the variable ibn might be not initialized when used in the return statement. However the code is written in such way that variable return ibn; line is executed only when ibn = 2; is also executed. Thus the warning is technically superfluous. The godbolt example to play with: https://godbolt.org/z/qnhq9YE61.

earnol avatar Jan 31 '25 21:01 earnol