Using core.reflect to check for unused parameters
Stefan Koch
uplink.coder at googlemail.com
Thu Sep 2 21:27:33 UTC 2021
On Monday, 30 August 2021 at 18:13:59 UTC, Stefan Koch wrote:
> Hi,
>
> I've just extended core.reflect's reflection to function bodies.
>
> Which means you can now do fun things such as checking for
> unused parameters at compile-time.
>
Ah the bugs are biting again.
if you have played with the code I posted you will have seen that
it didn't count parameters which were used as variable initializer
i.e.
```d
void func(int p)
{
auto x = p;
}
```
would have complained about unused parameters.
The reason is that we didn't visit the `_init` field of the
`VariableDeclaration`.
luckily it's easily fixed.
```d
class Marker : TransitiveVisitor
{
bool[const(VariableDeclaration)]* parameterUsageList;
this(bool[const(VariableDeclaration)] *parameterUsageList)
{
this.parameterUsageList = parameterUsageList;
}
alias visit = TransitiveVisitor.visit;
override void visit(VariableDeclaration vd)
{
if (auto b = vd in *parameterUsageList)
{
*b = true;
}
vd._init.accept(this);
}
}
```
now this visitor will reliably detect usage of parameters.
More information about the Digitalmars-d
mailing list