golangci / golangci-lint

Fast linters runner for Go
https://golangci-lint.run
GNU General Public License v3.0
15.34k stars 1.37k forks source link

unused: finding issues in 3rd-party dependencies #3600

Closed ItalyPaleAle closed 1 year ago

ItalyPaleAle commented 1 year ago

Welcome

Description of the problem

After updating to 1.51.1 (due to Go 1.20), we are getting linter failures due to the unused linter reporting issues form 3rd-party dependencies:

golangci-lint run --timeout=20m
../../go/pkg/mod/github.com/go-redis/redis/v8@v8.11.5/command.go:1642:1      unused  field `ID` is unused
../../go/pkg/mod/github.com/go-redis/redis/v8@v8.11.5/command.go:1643:1      unused  field `Consumer` is unused
../../go/pkg/mod/github.com/go-redis/redis/v8@v8.11.5/command.go:1644:1      unused  field `Idle` is unused
../../go/pkg/mod/github.com/go-redis/redis/v8@v8.11.5/command.go:1645:1      unused  field `RetryCount` is unused
../../go/pkg/mod/github.com/go-redis/redis/v8@v8.11.5/command.go:1348:1      unused  field `ID` is unused
../../go/pkg/mod/github.com/go-redis/redis/v8@v8.11.5/command.go:1349:1      unused  field `Values` is unused
../../go/pkg/mod/github.com/go-redis/redis/v9@v9.0.0-rc.2/command.go:1666:1  unused  field `ID` is unused
../../go/pkg/mod/github.com/go-redis/redis/v9@v9.0.0-rc.2/command.go:1667:1  unused  field `Consumer` is unused
../../go/pkg/mod/github.com/go-redis/redis/v9@v9.0.0-rc.2/command.go:1668:1  unused  field `Idle` is unused
../../go/pkg/mod/github.com/go-redis/redis/v9@v9.0.0-rc.2/command.go:1669:1  unused  field `RetryCount` is unused
../../go/pkg/mod/github.com/go-redis/redis/v9@v9.0.0-rc.2/command.go:1406:1  unused  field `Values` is unused

Although these issues are valid, we cannot act on them as they're external dependencies

Version of golangci-lint

```console $ golangci-lint --version golangci-lint has version 1.51.1 built from b87d2c19 on 2023-02-05T13:06:33Z ```

Configuration file

```yaml # options for analysis running run: # default concurrency is a available CPU number concurrency: 4 # timeout for analysis, e.g. 30s, 5m, default is 1m deadline: 15m # exit code when at least one issue was found, default is 1 issues-exit-code: 1 # include test files or not, default is true tests: true # list of build tags, all linters use it. Default is empty list. build-tags: - certtests # which dirs to skip: they won't be analyzed; # can use regexp here: generated.*, regexp is applied on full path; # default value is empty list, but next dirs are always skipped independently # from this option's value: # vendor$, third_party$, testdata$, examples$, Godeps$, builtin$ skip-dirs: - ^vendor$ # which files to skip: they will be analyzed, but issues from them # won't be reported. Default value is empty list, but there is # no need to include all autogenerated files, we confidently recognize # autogenerated files. If it's not please let us know. # skip-files: # - ".*\\.my\\.go$" # - lib/bad.go # output configuration options output: # colored-line-number|line-number|json|tab|checkstyle, default is "colored-line-number" format: tab # print lines of code with issue, default is true print-issued-lines: true # print linter name in the end of issue text, default is true print-linter-name: true # all available settings of specific linters linters-settings: errcheck: # report about not checking of errors in type assetions: `a := b.(MyStruct)`; # default is false: such cases aren't reported by default. check-type-assertions: false # report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`; # default is false: such cases aren't reported by default. check-blank: false # [deprecated] comma-separated list of pairs of the form pkg:regex # the regex is used to ignore names within pkg. (default "fmt:.*"). # see https://github.com/kisielk/errcheck#the-deprecated-method for details ignore: fmt:.*,io/ioutil:^Read.* # path to a file containing a list of functions to exclude from checking # see https://github.com/kisielk/errcheck#excluding-functions for details # exclude: funlen: lines: 60 statements: 40 govet: # report about shadowed variables check-shadowing: true # settings per analyzer settings: printf: # analyzer name, run `go tool vet help` to see all analyzers funcs: # run `go tool vet help printf` to see available settings for `printf` analyzer - (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof - (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf - (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf - (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf # enable or disable analyzers by name enable: - atomicalign enable-all: false disable: - shadow disable-all: false revive: # minimal confidence for issues, default is 0.8 confidence: 0.8 gofmt: # simplify code: gofmt with `-s` option, true by default simplify: true goimports: # put imports beginning with prefix after 3rd-party packages; # it's a comma-separated list of prefixes local-prefixes: github.com/dapr/ gocyclo: # minimal code complexity to report, 30 by default (but we recommend 10-20) min-complexity: 10 gocognit: # minimal code complexity to report, 30 by default (but we recommend 10-20) min-complexity: 10 maligned: # print struct with more effective memory layout or not, false by default suggest-new: true dupl: # tokens count to trigger issue, 150 by default threshold: 100 goconst: # minimal length of string constant, 3 by default min-len: 3 # minimal occurrences count to trigger, 3 by default min-occurrences: 5 depguard: list-type: denylist include-go-root: false packages-with-error-message: - "go.uber.org/atomic": "must use sync/atomic" - "golang.org/x/net/context": "must use context" - "github.com/pkg/errors": "must use standard library (errors package and/or fmt.Errorf)" - "github.com/Sirupsen/logrus": "must use github.com/dapr/kit/logger" - "github.com/agrea/ptr": "must use github.com/dapr/kit/ptr" - "github.com/cenkalti/backoff$": "must use github.com/cenkalti/backoff/v4" - "github.com/cenkalti/backoff/v2": "must use github.com/cenkalti/backoff/v4" - "github.com/cenkalti/backoff/v3": "must use github.com/cenkalti/backoff/v4" - "github.com/dgrijalva/jwt-go": "must use github.com/lestrrat-go/jwx/v2" - "github.com/golang-jwt/jwt$": "must use github.com/lestrrat-go/jwx/v2" - "github.com/golang-jwt/jwt/v4": "must use github.com/lestrrat-go/jwx/v2" - "github.com/lestrrat-go/jwx/jwa": "must use github.com/lestrrat-go/jwx/v2" - "github.com/lestrrat-go/jwx/jwt": "must use github.com/lestrrat-go/jwx/v2" misspell: # Correct spellings using locale preferences for US or UK. # Default is to use a neutral variety of English. # Setting locale to US will correct the British spelling of 'colour' to 'color'. # locale: Default ignore-words: - someword lll: # max line length, lines longer will be reported. Default is 120. # '\t' is counted as 1 character by default, and can be changed with the tab-width option line-length: 120 # tab width in spaces. Default to 1. tab-width: 1 unused: # treat code as a program (not a library) and report unused exported identifiers; default is false. # XXX: if you enable this setting, unused will report a lot of false-positives in text editors: # if it's called for subdir of a project it can't find funcs usages. All text editor integrations # with golangci-lint call it on a directory with the changed file. check-exported: false unparam: # Inspect exported functions, default is false. Set to true if no external program/library imports your code. # XXX: if you enable this setting, unparam will report a lot of false-positives in text editors: # if it's called for subdir of a project it can't find external interfaces. All text editor integrations # with golangci-lint call it on a directory with the changed file. check-exported: false nakedret: # make an issue if func has more lines of code than this setting and it has naked returns; default is 30 max-func-lines: 30 prealloc: # XXX: we don't recommend using this linter before doing performance profiling. # For most programs usage of prealloc will be a premature optimization. # Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them. # True by default. simple: true range-loops: true # Report preallocation suggestions on range loops, true by default for-loops: false # Report preallocation suggestions on for loops, false by default gocritic: # Which checks should be enabled; can't be combined with 'disabled-checks'; # See https://go-critic.github.io/overview#checks-overview # To check which checks are enabled run `GL_DEBUG=gocritic golangci-lint run` # By default list of stable checks is used. # enabled-checks: # Which checks should be disabled; can't be combined with 'enabled-checks'; default is empty disabled-checks: - regexpMust - rangeValCopy - hugeParam - ifElseChain - singleCaseSwitch - exitAfterDefer # Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint run` to see all tags and checks. # Empty list by default. See https://github.com/go-critic/go-critic#usage -> section "Tags". enabled-tags: - performance settings: # settings passed to gocritic captLocal: # must be valid enabled check name paramsOnly: true godox: # report any comments starting with keywords, this is useful for TODO or FIXME comments that # might be left in the code accidentally and should be resolved before merging keywords: # default keywords are TODO, BUG, and FIXME, these can be overwritten by this setting - NOTE - OPTIMIZE # marks code that should be optimized before merging - HACK # marks hack-arounds that should be removed before merging godot: exclude: - 'nosec' - '\}' capital: false scope: all dogsled: # checks assignments with too many blank identifiers; default is 2 max-blank-identifiers: 2 whitespace: multi-if: false # Enforces newlines (or comments) after every multi-line if statement multi-func: false # Enforces newlines (or comments) after every multi-line function signature wsl: # If true append is only allowed to be cuddled if appending value is # matching variables, fields or types on line above. Default is true. strict-append: true # Allow calls and assignments to be cuddled as long as the lines have any # matching variables, fields or types. Default is true. allow-assign-and-call: true # Allow multiline assignments to be cuddled. Default is true. allow-multiline-assign: true # Allow case blocks to end with a whitespace. allow-case-traling-whitespace: true # Allow declarations (var) to be cuddled. allow-cuddle-declarations: false linters: fast: false enable-all: true disable: # TODO Enforce the below linters later - dupl - musttag - errcheck - funlen - gochecknoglobals - gochecknoinits - gocyclo - gocognit - godox - interfacer - lll - maligned - scopelint - unparam - wsl - gomnd - testpackage - goerr113 - nestif - nlreturn - exhaustive - exhaustruct - noctx - gci - golint - tparallel - paralleltest - wrapcheck - tagliatelle - ireturn - exhaustivestruct - errchkjson - contextcheck - gomoddirectives - godot - cyclop - varnamelen - errorlint - forcetypeassert - ifshort - maintidx - nilnil - predeclared - tenv - thelper - wastedassign - containedctx - gosimple - nonamedreturns - asasalint - rowserrcheck - sqlclosecheck - structcheck - deadcode - nosnakecase - varcheck ```

Go environment

```console $ go version && go env go version go1.20.1 linux/amd64 GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/home/alessandro/.cache/go-build" GOENV="/home/alessandro/.config/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GOMODCACHE="/home/alessandro/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/home/alessandro/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/home/alessandro/.go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/home/alessandro/.go/pkg/tool/linux_amd64" GOVCS="" GOVERSION="go1.20.1" GCCGO="gccgo" GOAMD64="v1" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="0" GOMOD="/home/alessandro/dapr/components-contrib/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 -m64 -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2124533097=/tmp/go-build -gno-record-gcc-switches" ```

Verbose output of running

```console $ golangci-lint run -v INFO [config_reader] Config search paths: [./ /home/alessandro/dapr/components-contrib /home/alessandro/dapr /home/alessandro /home /] INFO [config_reader] Used config file .golangci.yml INFO [lintersdb] Active 47 linters: [asciicheck bidichk bodyclose decorder depguard dogsled dupword durationcheck errname execinquery exportloopref forbidigo ginkgolinter gocheckcompilerdirectives goconst gocritic gofmt gofumpt goheader goimports gomodguard goprintffuncname gosec govet grouper importas ineffassign interfacebloat loggercheck makezero misspell nakedret nilerr nolintlint nosprintfhostport prealloc promlinter reassign revive staticcheck stylecheck testableexamples typecheck unconvert unused usestdlibvars whitespace] INFO [loader] Using build tags: [certtests] INFO [loader] Go packages loading at mode 575 (exports_file|files|imports|name|types_sizes|compiled_files|deps) took 1.273126048s INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 43.883129ms INFO [linters_context] importas settings found, but no aliases listed. List aliases under alias: key. INFO [linters_context/goanalysis] analyzers took 5m29.467908427s with top 10 stages: buildir: 2m10.60496444s, gocritic: 1m39.420742352s, buildssa: 15.582930881s, bidichk: 14.274298592s, the_only_name: 9.867672996s, unconvert: 9.85310142s, inspect: 3.933478977s, gofumpt: 2.064222479s, nilness: 1.72842401s, fact_deprecated: 1.720431535s INFO [runner/max_same_issues] 1/4 issues with text "field `ID` is unused" were hidden, use --max-same-issues INFO [runner] Issues before processing: 718, after processing: 11 INFO [runner] Processors filtering stat (out/in): sort_results: 11/11, path_prettifier: 718/718, skip_files: 718/718, exclude-rules: 141/712, nolint: 12/141, max_from_linter: 11/11, source_code: 11/11, path_shortener: 11/11, cgo: 718/718, skip_dirs: 718/718, autogenerated_exclude: 712/718, identifier_marker: 712/712, exclude: 712/712, max_same_issues: 11/12, uniq_by_line: 12/12, max_per_file_from_linter: 12/12, path_prefixer: 11/11, filename_unadjuster: 718/718, diff: 12/12, severity-rules: 11/11 INFO [runner] processing took 65.217495ms with stages: nolint: 26.372558ms, exclude-rules: 14.234077ms, identifier_marker: 9.535984ms, autogenerated_exclude: 7.858988ms, path_prettifier: 5.188792ms, skip_dirs: 1.637397ms, source_code: 251.299µs, cgo: 54.4µs, max_same_issues: 43.8µs, filename_unadjuster: 28.7µs, uniq_by_line: 4.1µs, path_shortener: 2.6µs, max_from_linter: 1.8µs, max_per_file_from_linter: 900ns, severity-rules: 600ns, skip_files: 400ns, sort_results: 300ns, diff: 300ns, path_prefixer: 300ns, exclude: 200ns INFO [runner] linters took 1m6.384613792s with stages: goanalysis_metalinter: 1m6.319300997s ../../go/pkg/mod/github.com/go-redis/redis/v8@v8.11.5/command.go:1642:2 unused field `ID` is unused ../../go/pkg/mod/github.com/go-redis/redis/v8@v8.11.5/command.go:1643:2 unused field `Consumer` is unused ../../go/pkg/mod/github.com/go-redis/redis/v8@v8.11.5/command.go:1644:2 unused field `Idle` is unused ../../go/pkg/mod/github.com/go-redis/redis/v8@v8.11.5/command.go:1645:2 unused field `RetryCount` is unused ../../go/pkg/mod/github.com/go-redis/redis/v8@v8.11.5/command.go:1348:2 unused field `ID` is unused ../../go/pkg/mod/github.com/go-redis/redis/v8@v8.11.5/command.go:1349:2 unused field `Values` is unused ../../go/pkg/mod/github.com/go-redis/redis/v9@v9.0.0-rc.2/command.go:1666:2 unused field `ID` is unused ../../go/pkg/mod/github.com/go-redis/redis/v9@v9.0.0-rc.2/command.go:1667:2 unused field `Consumer` is unused ../../go/pkg/mod/github.com/go-redis/redis/v9@v9.0.0-rc.2/command.go:1668:2 unused field `Idle` is unused ../../go/pkg/mod/github.com/go-redis/redis/v9@v9.0.0-rc.2/command.go:1669:2 unused field `RetryCount` is unused ../../go/pkg/mod/github.com/go-redis/redis/v9@v9.0.0-rc.2/command.go:1406:2 unused field `Values` is unused INFO File cache stats: 442 entries of total size 2.5MiB INFO Memory: 542 samples, avg is 2001.3MB, max is 2975.0MB INFO Execution took 1m7.709985455s ```

Code example or link to a public repository

https://github.com/dapr/components-contrib/pull/2538
boring-cyborg[bot] commented 1 year ago

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

ldez commented 1 year ago

Hello,

You have checked the following box:

[x] Yes, I've tried with the standalone linter if available (e.g., gocritic, go vet, etc.). (https://golangci-lint.run/usage/linters/)

unused is related to staticcheck, can you give me the output of the run of the standalone binary of staticcheck? (golangci-lint v1.51.1 uses honnef.co/go/tools@v0.4.0)

https://github.com/golangci/golangci-lint/blob/v1.51.1/go.mod#L115

I think the problem is already fixed by https://github.com/golangci/golangci-lint/pull/3591 but you have to check.

ItalyPaleAle commented 1 year ago

Thanks for the quick response :)

I checked that box because I tried installing the unused linter directly and I couldn't get it to work, so I figured it fell under the "if [not] available"

I can confirm that the issue is reproducible with staticheck v0.4.0, but it's fixed with the latest version of staticheck.

So looks like #3591 does fix that. Thanks!