Skip to content

unused: marking all fields as read doesn't work recursively #1249

@dominikh

Description

@dominikh

Some operations mark all fields in a struct as used, because we can't do any better. One example is converting to unsafe.Pointer – who knows what the user will do with the data afterwards.

Unfortunately, we're only marking the immediate fields as used:

type t struct {
	a, b int
	c    t2
}

type t2 struct {
	d, e int
}

func Foo() {
	var x t
	_ = unsafe.Pointer(&x)
}
$ run-dev-staticcheck baz.go
baz.go:11:2: field d is unused (U1000)
baz.go:11:5: field e is unused (U1000)

Fields d and e should also be marked as used, as they are part of t's memory layout. The same problem occurs for embedded fields.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions