D-Scanner misses variable usage in struct literals
Take the following code:
private:
struct Foo { char[] arg; }
struct Bar { Foo f; }
void main ()
{
Foo var = Foo("Hello".dup);
Bar b = Bar(var);
foobar(b);
}
void foobar (Bar) {}
It passes dscanner --styleCheck test.d without a single message.
Now change it to use a struct literal:
private:
struct Foo { char[] arg; }
struct Bar { Foo f; }
void main ()
{
Foo var = Foo("Hello".dup);
Bar b = { f: var };
foobar(b);
}
void foobar (Bar) {}
And you get:
test.d(8:9)[warn]: Variable var is never modified and could have been declared const or immutable.
I think in the first case it doesn't know if var is used as ref/out parameter in Bar, so it doesn't emit a warning - it should actually emit the warning there
In the bottom case it correctly issues the warning
A const Foo would not be assign-able to Bar.f. So following D-Scanner advice would lead to a compilation error. How's that correct ?
ah yeah Foo has indirections, so it doesn't assign to f if it's const. I don't think that's something D-Scanner knows either, so it assumes structs don't have indirections. In the end the test is nearly useless because on parameters it is disabled because they could be ref and if we also disable it for not knowing if an assignment won't work it will essentially never emit.