为什么我不会在未使用的局部变量上出现编译器错误?

Why don't I get compiler errors on unused local variables?

Dlang documentation 状态:

It is an error to declare a local variable that is never referred to. Dead variables, like anachronistic dead code, are just a source of confusion for maintenance programmers.

我在 ldc 0.14gdc 5.1dmd 2.067.1 中编译了以下代码:

void main()
{
    int i;
    //local variable i not used...
} 

我没有遇到任何编译器错误。这应该编译失败吗?

很多这些错误的东西实际上并没有实现。它们是很好的东西,编译器作者保留犯错误的权利(根据规范的理由,你的代码无论如何都是错误的),但实际上还没有完成......也许永远不会真正做。

It is an error to use a local variable without first assigning it a value. The implementation may not always be able to detect these cases. Other language compilers sometimes issue a warning for this, but since it is always a bug, it should be an error.

这还没有实现,而且可能永远不会实现,因为人们依赖自动初始化作为一项功能。

It is an error to declare a local variable that is never referred to. Dead variables, like anachronistic dead code, are just a source of confusion for maintenance programmers.

未实施

It is an error to return the address of or a reference to a local variable.

仅部分实施。

我从未见过为未使用的变量生成错误的编译器。肯定有警告,但没有错误。

我建议您尝试 Dscanner 来检测它们,尽管它有时会发出错误警告,尤其是用作输出参数时。