D-Scanner icon indicating copy to clipboard operation
D-Scanner copied to clipboard

D-Scanner misses variable usage in struct literals

Open Geod24 opened this issue 4 years ago • 3 comments

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.

Geod24 avatar Nov 05 '21 04:11 Geod24

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

WebFreak001 avatar Nov 05 '21 09:11 WebFreak001

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 ?

Geod24 avatar Nov 05 '21 09:11 Geod24

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.

WebFreak001 avatar Nov 05 '21 09:11 WebFreak001