[cfe-commits] [patch] Add -Wdangling-else-parentheses

Nico Weber thakis at chromium.org
Tue Dec 20 13:36:08 PST 2011


Hi,

the attached patch implements a warning for dangling elses, like
described at http://drdobbs.com/blogs/cpp/231602010 and as requested
in http://llvm.org/pr11609. The warning fires 0 times for chromium and
webkit, and it seems to catch a superset of what gcc's -Wparentheses
catches.

Examples (see the test case for more):

This warns:
$ cat test.cc
void f() {}

int main() {
  if (false)
    if (false)
      return 0;
  else
    return 1;
}
$ Release+Asserts/bin/clang -c test.cc
test.cc:7:3: warning: add explicit braces to avoid dangling else
[-Wdangling-else-parentheses]
  else
  ^
1 warning generated.


These don't:
$ cat test.cc
void f() {}

int main() {
  if (false) {
    if (false)
      return 0;
  } else
    return 1;
}
$ Release+Asserts/bin/clang -c test.cc
$ cat test.cc
void f() {}

int main() {
  if (false)
    if (false) {
      return 0;
    } else
      return 1;
}
$ Release+Asserts/bin/clang -c test.cc


Comments?

Nico
-------------- next part --------------
A non-text attachment was scrubbed...
Name: clang-warn.patch
Type: application/octet-stream
Size: 11072 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20111220/95415557/attachment.obj>


More information about the cfe-commits mailing list