Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

unused: false positive involving unnamed struct types, named struct types and implicit conversions #810

Closed
kylrth opened this issue Aug 12, 2020 · 4 comments
Labels
false-positive started Issues we've started working on

Comments

@kylrth
Copy link

kylrth commented Aug 12, 2020

I discovered this while using the unused linter in golangci-lint. I hope I found the right place to report this issue. 馃

Here's some code that reproduces it:

package unused_test

import "fmt"

type Thing struct {
	stuff int
	has   struct {
		a bool
		b bool
	}
}

func ExampleThing() {
	type temp struct {
		a bool
		b bool
	}

	stuff := map[string]Thing{
		"hello": {has: temp{true, false}},
	}

	fmt.Println(stuff["hello"].stuff, stuff["hello"].has)
	// Output: 0 {true false}
}

Running ./golangci-lint run unused_test.go results in unused_test.go:14:7: type `temp` is unused (unused).

@kylrth kylrth added false-positive needs-triage Newly filed issue that needs triage labels Aug 12, 2020
@dgryski
Copy link

dgryski commented Aug 12, 2020

Does this reproduce with the latest staticcheck?

@kylrth
Copy link
Author

kylrth commented Aug 12, 2020

Yes, I get the following output with staticcheck built from master on this repo:

unused_test.go:14:7: type temp is unused (U1000)

Run './staticcheck -explain <check>' or visit https://staticcheck.io/docs/checks for documentation on checks.

@dominikh
Copy link
Owner

Simpler reproducer:

package foo

type Thing struct {
	has struct {
		a bool
	}
}

func Fn() {
	type temp struct {
		a bool
	}

	x := Thing{
		has: temp{true},
	}
	_ = x
}

the issue seems to be caused by the implicit conversion to the unnamed struct type in Thing.

@dominikh dominikh removed the needs-triage Newly filed issue that needs triage label Aug 12, 2020
@dominikh dominikh changed the title unused: type declared inside function and used in struct literal unused: false positive involving unnamed struct types, named struct types and implicit conversions Aug 12, 2020
@qdm12
Copy link

qdm12 commented Mar 18, 2022

I can confirm (found using golangci-lint v1.44.2 built using Go 1.18):

package main

import (
	"fmt"
)

type someStruct struct {
	parentField struct {
		n int
	}
}

type someStruct2 struct {
	n int
}

func main() {
	x := someStruct{
		parentField: someStruct2{n: 1},
	}
	fmt.Println(x)
}

Error:

type `someStruct2` is unused (unused)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false-positive started Issues we've started working on
Projects
None yet
Development

No branches or pull requests

4 participants