Bug 4924 – Suspect indentation warning

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-09-23T12:26:00Z
Last change time
2015-06-09T05:11:34Z
Keywords
diagnostic
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2010-09-23T12:26:40Z
Unlike Python, Haskell, F# and few other languages, line indentations in D don't determine the semantics of the program. Yet programmers are humans, they do make mistakes, and for them indentations have meaning and importance (this is why good programmers usually indent their code carefully). A badly indented code doesn't just look sloppy, it may hide semantic problems. To reduce the noise it's important to minimize false positives, so the D compiler may issue warnings (errors are too much) only in few specific situations where a bad indentation is a clue of a possible semantic bug, and ignore indentations in all other situations. A situation that may justify a warning, an unexpected positive indentation after a single-line then/else/for/foreach/while: if (x > 5) a = 1; b = 2; // suspect indentation warning if (y > 2) c = 3; else d = 4; e = 5; // suspect indentation warning for (int i = 0; i < 10; i++) f++; g++; // suspect indentation warning foreach (j; 0 .. 10) h++; j++; // suspect indentation warning while (x < 10) x = foo(x); y++; // suspect indentation warning if (x > 5) a = 1; b = 2; // no warning here? if (x > 5) a = 1; b = 2; // no warning here if (x > 5) a = 1; b = 2; // no warning here This simple warning is able to catch some common bugs (just as requiring {} instead of just a semicolon avoids other similar bugs). See also bug 4357
Comment #1 by bearophile_hugs — 2010-09-23T12:33:50Z
Sorry, my mistake, I meant see also bug 4375
Comment #2 by bugzilla — 2011-01-08T12:31:49Z
D is insensitive to whitespace formatting, and trying to force it into sort of being one would be a mistake. Enforcing indenting rules is the job of a pretty printer, not the compiler.