Golangci-lint: declared but not used error

Created on 10 Dec 2020  路  7Comments  路  Source: golangci/golangci-lint

Thank you for creating the issue!

  • [x] Yes, I'm using a binary release within 2 latest major releases. Only such installations are supported.
  • [x] Yes, I've searched similar issues on GitHub and didn't find any.
  • [x] Yes, I've included all information below (version, config, etc).

Please include the following information:

Version of golangci-lint

$ golangci-lint --version
# paste output here

golangci-lint has version 1.30.0 built from 45b90f6 on 2020-08-03T03:10:34Z

Config file

$ cat .golangci.yml
# paste output here
no file

Go environment

$ go version && go env
# paste output here

go version go1.15.2 darwin/amd64
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/dongliangwu/Library/Caches/go-build"
GOENV="/Users/dongliangwu/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/dongliangwu/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/dongliangwu/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.15.2/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.15.2/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/dongliangwu/go/src/wdlgoproject/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/6p/95ngmhtn2zlgw8vhf27kr7bw0000gp/T/go-build725774482=/tmp/go-build -gno-record-gcc-switches -fno-common"

Source code

package main

func main() {
    var foo string
    defer func() {
        foo = "foo"
    }()
}

Verbose output of running

$ golangci-lint cache clean
$ golangci-lint run main.go -v
# paste output here

INFO [config_reader] Config search paths: [./ /Users/dongliangwu/go/src/wdlgoproject /Users/dongliangwu/go/src /Users/dongliangwu/go /Users/dongliangwu /Users /] 
INFO [lintersdb] Active 10 linters: [deadcode errcheck gosimple govet ineffassign staticcheck structcheck typecheck unused varcheck] 
INFO [loader] Go packages loading at mode 575 (compiled_files|deps|exports_file|name|files|imports|types_sizes) took 280.049501ms 
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 2.152653ms 
INFO [linters context/goanalysis] analyzers took 38.792319ms with top 10 stages: buildir: 3.299578ms, fact_deprecated: 3.246354ms, SA4019: 932.858碌s, S1000: 862.422碌s, S1039: 843.395碌s, S1008: 834.936碌s, S1005: 829.879碌s, S1034: 800.894碌s, bools: 795.269碌s, composites: 788.427碌s 
WARN [runner] Can't run linter goanalysis_metalinter: S1012: failed prerequisites: [(inspect@command-line-arguments, isgenerated@command-line-arguments): analysis skipped: errors in package: [/Users/dongliangwu/go/src/wdlgoproject/main1.go:4:6: foo declared but not used]] 
INFO [linters context/goanalysis] analyzers took 195.369碌s with top 10 stages: buildir: 180.576碌s, U1000: 14.793碌s 
WARN [runner] Can't run linter unused: buildir: analysis skipped: errors in package: [/Users/dongliangwu/go/src/wdlgoproject/main1.go:4:6: foo declared but not used] 
INFO [runner] processing took 5.75碌s with stages: max_same_issues: 3.265碌s, nolint: 381ns, max_from_linter: 248ns, skip_dirs: 224ns, cgo: 169ns, path_prettifier: 131ns, autogenerated_exclude: 130ns, skip_files: 128ns, uniq_by_line: 127ns, diff: 126ns, filename_unadjuster: 126ns, path_prefixer: 125ns, identifier_marker: 123ns, max_per_file_from_linter: 72ns, path_shortener: 69ns, sort_results: 67ns, source_code: 62ns, exclude: 62ns, exclude-rules: 58ns, severity-rules: 57ns 
INFO [runner] linters took 9.359809ms with stages: goanalysis_metalinter: 8.115094ms, unused: 1.202616ms 
ERRO Running error: buildir: analysis skipped: errors in package: [/Users/dongliangwu/go/src/wdlgoproject/main1.go:4:6: foo declared but not used] 
INFO Memory: 5 samples, avg is 71.3MB, max is 72.3MB 
INFO Execution took 335.145943ms                

bug

All 7 comments

Hey, thank you for opening your first Issue ! 馃檪 If you would like to contribute we have a guide for contributors.

The linter is correct, it is declare but not used. It's re-assigned but still not used. Unless you pass it to another method, compare it or return it, it will not be seen as used.

@DeloWu feel free to reopen this issue if you still think it is a bug.

This code passed by go build.I think golangci-lint should report it as a issue rather than report a running error and exit.

golangci-lint doesn't even run if the code doesn't compile so I don't think golangci-lint should ever be used to catch compiler errors. It's a collection of linters to reduce code smell and keep users from doing unwanted patterns. Passing a string around without using it other than re-assigning it is very match a bad pattern in my opinion so I think this check is valid. So use the compiler to catch code that can't compile and golangci-lint to catch code smell and code complexity seems like a good combination to me!

Also, if you don't agree with the linter it's always possible to disable it or disable single rules from a linter.

EDIT: I might have misread your comment, I'm not sure I follow. Do you mean that this specific linter makes golangci-lint stop and no other linters are run or code analyzed? If so, that's a bug but I'm pretty sure the linter just reports this as an issue and continues with all linters on all files.

golangci-lint doesn't even run if the code doesn't compile so I don't think golangci-lint should ever be used to catch compiler errors. It's a collection of linters to reduce code smell and keep users from doing unwanted patterns. Passing a string around without using it other than re-assigning it is very match a bad pattern in my opinion so I think this check is valid. So use the compiler to catch code that can't compile and golangci-lint to catch code smell and code complexity seems like a good combination to me!

Also, if you don't agree with the linter it's always possible to disable it or disable single rules from a linter.

EDIT: I might have misread your comment, I'm not sure I follow. Do you mean that this specific linter makes golangci-lint stop and no other linters are run or code analyzed? If so, that's a bug but I'm pretty sure the linter just reports this as an issue and continues with all linters on all files.

yes,so it is a bug!This specific linter makes golangci-lint stop and no other linters are run or code analyzed.
I konw the source code I provided is not pretty, but go.1.12 and go1.15 can compile successfully.
I ran golangci-lint by default linter to check this source code, and all linter stopped.
CMD:

golangci-lint run main1.go --out-format json

OUTPUT:

WARN [runner] Can't run linter goanalysis_metalinter: S1023: failed prerequisites: [(inspect@command-line-arguments, isgenerated@command-line-arguments): analysis skipped: errors in package: [/Users/dongliangwu/go/src/wdlgoproject/main1.go:4:6: foo declared but not used]] 
WARN [runner] Can't run linter unused: buildir: analysis skipped: errors in package: [/Users/dongliangwu/go/src/wdlgoproject/main1.go:4:6: foo declared but not used] 
ERRO Running error: buildir: analysis skipped: errors in package: [/Users/dongliangwu/go/src/wdlgoproject/main1.go:4:6: foo declared but not used] 

I tried by many linters, and most of them stop all linter like govet , errcheck and etc..., but some linter is good like ineffassign
CMD-good:

golangci-lint run main1.go --out-format json -E ineffassign --disable-all

OUTPUT-good:

{"Issues":null,"Report":{"Linters":[{"Name":"govet","EnabledByDefault":true},{"Name":"bodyclose"},{"Name":"noctx"},{"Name":"errcheck","EnabledByDefault":true},{"Name":"golint"},{"Name":"rowserrcheck"},{"Name":"staticcheck","EnabledByDefault":true},{"Name":"unused","EnabledByDefault":true},{"Name":"gosimple","EnabledByDefault":true},{"Name":"stylecheck"},{"Name":"gosec"},{"Name":"structcheck","EnabledByDefault":true},{"Name":"varcheck","EnabledByDefault":true},{"Name":"interfacer"},{"Name":"unconvert"},{"Name":"ineffassign","Enabled":true,"EnabledByDefault":true},{"Name":"dupl"},{"Name":"goconst"},{"Name":"deadcode","EnabledByDefault":true},{"Name":"gocyclo"},{"Name":"gocognit"},{"Name":"typecheck","EnabledByDefault":true},{"Name":"asciicheck"},{"Name":"gofmt"},{"Name":"gofumpt"},{"Name":"goimports"},{"Name":"goheader"},{"Name":"gci"},{"Name":"maligned"},{"Name":"depguard"},{"Name":"misspell"},{"Name":"lll"},{"Name":"unparam"},{"Name":"dogsled"},{"Name":"nakedret"},{"Name":"prealloc"},{"Name":"scopelint"},{"Name":"gocritic"},{"Name":"gochecknoinits"},{"Name":"gochecknoglobals"},{"Name":"godox"},{"Name":"funlen"},{"Name":"whitespace"},{"Name":"wsl"},{"Name":"goprintffuncname"},{"Name":"gomnd"},{"Name":"goerr113"},{"Name":"gomodguard"},{"Name":"godot"},{"Name":"testpackage"},{"Name":"nestif"},{"Name":"exportloopref"},{"Name":"exhaustive"},{"Name":"sqlclosecheck"},{"Name":"nlreturn"},{"Name":"nolintlint"}]}}

CMD-bad:

golangci-lint run main1.go --out-format json -E errcheck --disable-all

OUTPUT-bad:

WARN [runner] Can't run linter errcheck: errcheck: analysis skipped: errors in package: [/Users/dongliangwu/go/src/wdlgoproject/main1.go:4:6: foo declared but not used] 
ERRO Running error: errcheck: analysis skipped: errors in package: [/Users/dongliangwu/go/src/wdlgoproject/main1.go:4:6: foo declared but not used] 

@DeloWu we will look into this. Your last comment make clear what is your issue, thank you.
I have to state that I am not very optimistic because different linters have different expectations and some of them might not work when other do.

Was this page helpful?
0 / 5 - 0 ratings