[Issue 4375] New: Require explicit braces when 'else' is ambiguous

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jun 23 05:12:03 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4375

           Summary: Require explicit braces when 'else' is ambiguous
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2010-06-23 05:12:01 PDT ---
This program is valid code both in D2 and C:

void foo() {}
void bar() {}
int main() {
    int a = 1;
    int b = 1;
    if (a)
        if (b)
            foo();
    else bar();
    return 0;
}


The problem is that the indentations do not reflect the program semantics, this
can cause bugs (this bug source is missing in Python). The problem can be
avoided adding explicit braces to that code.

Compiled with GCC V.4.5.0 it prints:
...>gcc -Wall test.c -o test
test.c: In function 'main':
test.c:6:8: warning: suggest explicit braces to avoid ambiguous 'else'


The warning used here by -Wall is just -Wparentheses:
...>gcc -Wparentheses test.c -o test
test.c: In function 'main':
test.c:6:8: warning: suggest explicit braces to avoid ambiguous 'else'


So in D2 I propose to turn that code into an actual syntax error, similar to
using the warnings-as-errors option of GCC:
...>gcc -Wall -Werror test.c -o test
cc1.exe: warnings being treated as errors
test.c: In function 'main':
test.c:6:8: error: suggest explicit braces to avoid ambiguous 'else'


Note: this enhancement request doesn't mean that explicit braces are always
required in "if". They are required only in situations where the code can be
ambiguous for a human programmer. Well written D code does not raise this
error.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list