void f() { Object get() { return null; } if( auto a = get() ) {} // OK for( auto a = get(); a; a = get() ) {} // OK while( auto a = get() ) {} // Does not compile } autowhile.d(7): expression expected, not 'auto' autowhile.d(7): found 'a' when expecting ')' autowhile.d(7): found '=' instead of statement autowhile.d(8): unrecognized declaration This is clearly inconsistent, so logged as a bug and not an enhancement.
*** Issue 6550 has been marked as a duplicate of this issue. ***
https://github.com/D-Programming-Language/dmd/pull/342
The for statement has a separate initialization and assignment. Initialization and assignment are very distinct operations, and the rewrites confuse the two. I'm not sure there even is a correct answer here. Is the variable created once, default initialized, and then assigned to each time, or is a new one created and initialized each time through the loop? For the programmer who wants to declare a variable, a for loop does the job in a straightforward manner.