Compiler doesn't warn for declared but unused variables

Kapendev alexandroskapretsos at gmail.com
Fri Nov 27 19:51:57 UTC 2026


On Friday, 27 November 2026 at 16:34:07 UTC, DLearner wrote:
> This code
> ```
> void main() {
>
>    size_t a;
>    size_t b;
>
>    a = 1;
> }
> ```
> compiles and executes silently, despite 'b' declared but not 
> used.
>
> It's not an error, but should it not generate a warning?

It's 8 or 4 bytes on the stack that are going to be removed by 
the optimizer. One reason D allows them is because of code 
generation.

No idea what linters exist for D, but they can do a better job 
than a compiler for things like this. Scope the warnings, change 
how strict they need to be, ...


More information about the Digitalmars-d-learn mailing list