[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

Nico Weber thakis at chromium.org
Mon Jan 16 19:26:37 PST 2012


Hi Chad,

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.

This was discussed when I proposed the warning:
http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20111219/050474.html

Richard and Eli both preferred the warning to fire in this case,
Chandler was against it.

I'm happy with adding any tweak we agree on (say "don't warn if the
else starts on the same line the previous if starts" or something like
that).

How often do you run into this in practice?

Nico




More information about the cfe-commits mailing list