Go-tools: unused: mistakenly flags fields in an anonymous struct

Created on 19 Feb 2018  路  3Comments  路  Source: dominikh/go-tools

I encountered this while running unused on rs/zerolog.

% unused ./...
log_test.go:371:3: field l is unused (U1000)
log_test.go:372:3: field p is unused (U1000)

Here is the offending code.

type levelWriter struct {
    ops []struct {
        l Level
        p string
    }
}

...but it appears to be used right below.

func (lw *levelWriter) WriteLevel(lvl Level, p []byte) (int, error) {
    lw.ops = append(lw.ops, struct {
        l Level
        p string
    }{lvl, string(p)})
    return len(p), nil
}

Is unused confused by the anonymous struct?

Version

I'm using the latest master commit (Feb 19, 2018), built with Go 1.10.

false-positive

Most helpful comment

This should be fixed in master.

All 3 comments

It is not used "right below". Merely assigning to the fields does not count as usage if the fields are never being read (either directly, or indirectly via reflection.)

You are right that they are being used, however, via reflect.DeepEqual. I will have to check if this bug is caused by the use of DeepEqual, or by the use of anonymous structs.

https://github.com/dominikh/go-tools/issues/203 would suggest that we are struggling with anonymous types, though. Which isn't surprising; unused is due for a redesign and rewrite.

This should be fixed in master.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

axcdnt picture axcdnt  路  3Comments

dominikh picture dominikh  路  4Comments

Shikkic picture Shikkic  路  4Comments

jtreleaven picture jtreleaven  路  3Comments

dominikh picture dominikh  路  3Comments