Compiler doesn't warn for declared but unused variables
Forum User
forumuser at example.com
Sun Nov 29 19:16:44 UTC 2026
On Friday, 27 November 2026 at 19:51:57 UTC, Kapendev wrote:
> 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.
Maybe I misunderstood the question: Isn't DLearner asking for a
missed opportunity to flag a potential programmer's mistake and
not one of a memory allocation inefficiency?
> One reason D allows them is because of code generation.
Everything that is not forbidden is allowed. It's not forbidden.
But what about warnings on unused variables of non-trivial type:
```
void main() {
size_t a;
Lock b;
a = 1;
}
```
Or unused function arguments:
```
void foo (ref size_t a, size_t b) {
a = 1;
}
```
Shall it warn?
More information about the Digitalmars-d-learn
mailing list