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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

U1000: field is unused #1360

Closed
Dreamacro opened this issue Feb 3, 2023 · 2 comments
Closed

U1000: field is unused #1360

Dreamacro opened this issue Feb 3, 2023 · 2 comments

Comments

@Dreamacro
Copy link

Dreamacro commented Feb 3, 2023

staticcheck version: staticcheck 2023.1 (v0.4.0)
go version: go version go1.19.5 darwin/arm64

PoC:

package main

import (
	"github.com/phuslu/log"
)

type Log struct {
	Time      string `json:"time"`
	Level     string `json:"level"`
	Caller    string `json:"-"`
	Goid      string `json:"-"`
	Stack     string `json:"-"`
	Message   string `json:"message"`
	KeyValues []struct {
		Key       string `json:"key"`
		Value     string `json:"value"`
		ValueType byte   `json:"-"`
	} `json:"fields"`
}

//	type FormatterArgs struct {
//		Time      string // "2019-07-10T05:35:54.277Z"
//		Level     string // "info"
//		Caller    string // "prog.go:42"
//		Goid      string // "123"
//		Stack     string // "<stack string>"
//		Message   string // "a structure message"
//		KeyValues []struct {
//			Key       string // "foo"
//			Value     string // "bar"
//			ValueType byte   // 's'
//		}
//	}
type Event = log.FormatterArgs

func main() {
	e := Event{}
	l := Log(e)

	log.Info().Msgf("log: %+v", l)
}

staticcheck output:

/Users/dreamacro/go/pkg/mod/github.com/phuslu/log@v1.0.83/formatter.go:13:1: field Time is unused (U1000)
/Users/dreamacro/go/pkg/mod/github.com/phuslu/log@v1.0.83/formatter.go:14:1: field Level is unused (U1000)
/Users/dreamacro/go/pkg/mod/github.com/phuslu/log@v1.0.83/formatter.go:15:1: field Caller is unused (U1000)
/Users/dreamacro/go/pkg/mod/github.com/phuslu/log@v1.0.83/formatter.go:16:1: field Goid is unused (U1000)
/Users/dreamacro/go/pkg/mod/github.com/phuslu/log@v1.0.83/formatter.go:17:1: field Stack is unused (U1000)
/Users/dreamacro/go/pkg/mod/github.com/phuslu/log@v1.0.83/formatter.go:18:1: field Message is unused (U1000)
/Users/dreamacro/go/pkg/mod/github.com/phuslu/log@v1.0.83/formatter.go:19:1: field KeyValues is unused (U1000)

FormatterArgs does not have the required json format, so a structure with the same but json fields is set. It is used for http response.

I think this is a false positive.

@Dreamacro Dreamacro added false-positive needs-triage Newly filed issue that needs triage labels Feb 3, 2023
@dominikh dominikh removed the needs-triage Newly filed issue that needs triage label Feb 3, 2023
@dominikh
Copy link
Owner

dominikh commented Feb 3, 2023

Fixed on master, will be backported to a release by Monday.

@aalexand
Copy link

aalexand commented Feb 7, 2023

@dominikh Is there a release tag where the fix for this issue has been backported to?

dominikh added a commit that referenced this issue Feb 14, 2023
When converting between two struct types, the fields use each other.
However, we mustn't track the fields in a struct type from another
package, as they should always be considered used, and we may end up
creating nodes for them without ever recording uses.

The old implementation of unused behaved correctly; we accidentally
dropped a check.

Closes gh-1360.

(cherry picked from commit 4ceb5b2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants