misleading "-Wmisleading-indentation" warning on break in switch/case?

Stefan Sobernig ss@thinkersfoot.net
Thu Aug 24 15:35:00 GMT 2017


Hi

I got a code base with the following indentation style for break within
switch/case:

---------------%<---------------
#include <string.h>

int main(void)
{
  const char *c = "XX";
  int out = 0;

  switch (c[0]) {
  case 'X': if (strcmp(c, "XX")) out = 1; break; /* ok */
  case 'Y': if (strcmp(c, "XY") &&
		!strcmp(c, "YY")
		) out = 1; break; /* not ok ? */
  }
}
---------------%<---------------

With

$ gcc --version
gcc (GCC) 7.1.1 20170622 (Red Hat 7.1.1-3)

I get the following with -Wall or -Wmisleading-indentation on:

> d.c: In function 'main':
> d.c:10:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
>    case 'Y': if (strcmp(c, "XY") &&
>              ^~
> d.c:12:14: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
>    ) out = 1; break;
>               ^~~~~
> d.c:6:7: warning: variable 'out' set but not used [-Wunused-but-set-variable]
>    int out = 0;
>        ^~~

Droping/aligning the second break on its own line silences the warning,
but I see no reason to do so. Pls. advise!

Stefan



More information about the Gcc mailing list