[cfe-commits] r147202 - in /cfe/trunk: include/clang/Basic/DiagnosticGroups.td include/clang/Basic/DiagnosticParseKinds.td include/clang/Parse/Parser.h lib/Parse/ParseStmt.cpp test/Parser/warn-dangling-else.cpp

Chandler Carruth chandlerc at google.com
Mon Jan 16 19:15:17 PST 2012


On Mon, Jan 16, 2012 at 7:03 PM, Chad Rosier <mcrosier at apple.com> wrote:

> Hi Nico,
> I get a warning with the following test case.
>
> int foo(int a, int b, int c) {
>    int rval = 0;
>    if (a != b)
>        if (b == c) {
>            rval = 0;
>        } else {
>            rval = 1;
>        }
>    return rval;
> }
>
> mcrosier$ clang -O3 t.c -Wall -c -o /dev/null
> t.c:6:11: warning: add explicit braces to avoid dangling else
> [-Wdangling-else]
>        } else {
>          ^
> 1 warning generated.
>
> Is this expected behavior?  This code seems rather explicit to me.


Yes, this was discussed and folks wanted the warning here as well.

This is no more explicit than:

if (foo)
  if (bar)
    ...;
  else
    ...;

The grammar indicates that the else (in both cases) attach to the inner if,
but the {}s on the inner case don't actually make a difference.

I wasn't the biggest fan of these semantics, but they are at least
consistent...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120116/512103df/attachment.html>


More information about the cfe-commits mailing list