Re: [PATCH] Drop -Wdeclaration-after-statement
From: Andrew Morton
Date: Fri Feb 25 2022 - 20:10:10 EST
On Fri, 25 Feb 2022 11:15:58 +0300 Alexey Dobriyan <adobriyan@xxxxxxxxx> wrote:
> Putting declarations before statement is relict of single pass compiler
> era. It was necessary to allocate stack slots before generating code.
>
> Recently added static_assert() is a declaration. -Wdeclaration-after-statement
> prevents its placement anywhere in the code for no reason.
>
> Placing variable declarations in the beginning of the block increases
> variable "LOC lifetime" so to speak and chances that it will be misused.
> This is very low probability bug but still. Declaring variables right
> before first use will make "LOC lifetime" smaller.
>
> {
> int x;
> [x is misused due to a typo]
> f(x); // first correct use
> }
>
> vs
>
> {
> [can't misuse undeclared variable]
> int x;
> f(x); // first correct use
> }
>
Oh man. I so wish you'd cc'ed Linus on this one so we could admire his
reaction.