dominikh / go-tools

Staticcheck - The advanced Go linter
https://staticcheck.dev
MIT License
6.21k stars 377 forks source link

unused: struct comparison #1577

Open s1na opened 4 months ago

s1na commented 4 months ago

Hello. I'm using unused via golangci-lint. Here is the version of the packages:

❯ golangci-lint --version
golangci-lint has version 1.59.1 built with go1.22.4 from 1a55854 on 2024-06-08T22:05:49Z

❯ go version
go version go1.22.5 darwin/amd64

I can see that the version of go-tools that golanci-lint is using is honnef.co/go/tools v0.4.7.

go env

GO111MODULE='' GOARCH='amd64' GOBIN='' GOCACHE='/Users/sina/Library/Caches/go-build' GOENV='/Users/sina/Library/Application Support/go/env' GOEXE='' GOEXPERIMENT='' GOFLAGS='' GOHOSTARCH='amd64' GOHOSTOS='darwin' GOINSECURE='' GOMODCACHE='/Users/sina/dev/go/pkg/mod' GONOPROXY='' GONOSUMDB='' GOOS='darwin' GOPATH='/Users/sina/dev/go' GOPRIVATE='' GOPROXY='https://proxy.golang.org,direct' GOROOT='/usr/local/Cellar/go/1.22.5/libexec' GOSUMDB='sum.golang.org' GOTMPDIR='' GOTOOLCHAIN='auto' GOTOOLDIR='/usr/local/Cellar/go/1.22.5/libexec/pkg/tool/darwin_amd64' GOVCS='' GOVERSION='go1.22.5' GCCGO='gccgo' GOAMD64='v1' AR='ar' CC='cc' CXX='c++' CGO_ENABLED='1' GOMOD='/Users/sina/dev/go/src/github.com/s1na/test-unused-ci/go.mod' GOWORK='' CGO_CFLAGS='-O2 -g' CGO_CPPFLAGS='' CGO_CXXFLAGS='-O2 -g' CGO_FFLAGS='-O2 -g' CGO_LDFLAGS='-O2 -g' PKG_CONFIG='pkg-config' GOGCCFLAGS='-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/cp/k3x0xm3959qf9l0pcbbdxdt80000gn/T/go-build4190770178=/tmp/go-build -gno-record-gcc-switches -fno-common'

I'm running this command:

golangci-lint run

My config is:

linters:
  disable-all: true
  enable:
    - unused

linters-settings:
  unused:
    field-writes-are-uses: false
    generated-is-used: true

This is the code:

package main

import "fmt"

type A struct {
    foo  bool
    bar  bool
    used string
}

func main() {
    a := A{foo: true, bar: true, used: "hi"}
    b := A{foo: true, bar: true, used: "hi"}
    fmt.Printf("%s %s %v\n", a.used, b.used, a == b)
}

unused thinks foo and bar are unused in this case. Their values are read by the struct comparison.