golangci / golangci-lint

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

unused linter does not detect unused exported fields #4969

Closed mcastorina closed 2 weeks ago

mcastorina commented 2 weeks ago

Welcome

Description of the problem

The unused linter does not detect unused exported struct members when "exported-fields-are-used" configuration is set to false.

Version of golangci-lint

```console $ golangci-lint --version golangci-lint has version 1.60.3 built with go1.23.0 from c2e095c on 2024-08-22T21:45:24Z ```

Configuration

```console --config <(echo '{"linters-settings":{"unused":{"exported-fields-are-used":false}}}') ``` this is equivalent to the yaml config ```console linter-settings: unused: exported-fields-are-used: false ```

Go environment

```console $ go version && go env go version go1.23.0 darwin/arm64 GO111MODULE='' GOARCH='arm64' GOBIN='' GOEXE='' GOEXPERIMENT='' GOFLAGS='' GOHOSTARCH='arm64' GOHOSTOS='darwin' GOINSECURE='' GONOPROXY='' GONOSUMDB='' GOOS='darwin' GOPRIVATE='' GOPROXY='https://proxy.golang.org,direct' GOROOT='/opt/homebrew/Cellar/go/1.23.0/libexec' GOSUMDB='sum.golang.org' GOTMPDIR='' GOTOOLCHAIN='local' GOTOOLDIR='/opt/homebrew/Cellar/go/1.23.0/libexec/pkg/tool/darwin_arm64' GOVCS='' GOVERSION='go1.23.0' GODEBUG='' GOTELEMETRY='local' GCCGO='gccgo' GOARM64='v8.0' AR='ar' CC='cc' CXX='c++' CGO_ENABLED='1' GOMOD='/dev/null' 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 arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/s2/32jxgw6j2tl3lr6q7_p_0_th0000gn/T/go-build3082553639=/tmp/go-build -gno-record-gcc-switches -fno-common' ```

Verbose output of running

```console $ golangci-lint cache clean $ golangci-lint run --config <(echo '{"linters-settings":{"unused":{"exported-fields-are-used":false}}}') blah.go -v INFO golangci-lint has version 1.60.3 built with go1.23.0 from c2e095c on 2024-08-22T21:45:24Z INFO [config_reader] Used config file ../../dev/fd/11 INFO [lintersdb] Active 6 linters: [errcheck gosimple govet ineffassign staticcheck unused] INFO [loader] Go packages loading at mode 575 (files|types_sizes|exports_file|deps|imports|name|compiled_files) took 67.919958ms INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 126.334µs INFO [linters_context/goanalysis] analyzers took 853.935539ms with top 10 stages: buildir: 636.946627ms, printf: 33.912087ms, SA5012: 29.531086ms, ctrlflow: 29.333536ms, fact_deprecated: 27.19442ms, inspect: 26.515128ms, nilness: 23.992833ms, fact_purity: 23.913584ms, typedness: 19.195332ms, tokenfileanalyzer: 2.332332ms INFO [runner] processing took 2.086µs with stages: max_same_issues: 375ns, filename_unadjuster: 208ns, skip_dirs: 167ns, identifier_marker: 167ns, nolint: 167ns, path_prettifier: 125ns, skip_files: 125ns, source_code: 125ns, cgo: 125ns, invalid_issue: 125ns, path_shortener: 42ns, uniq_by_line: 42ns, fixer: 42ns, max_per_file_from_linter: 42ns, autogenerated_exclude: 42ns, diff: 42ns, exclude: 42ns, max_from_linter: 42ns, sort_results: 41ns, path_prefixer: 0s, exclude-rules: 0s, severity-rules: 0s INFO [runner] linters took 581.634083ms with stages: goanalysis_metalinter: 581.611875ms INFO File cache stats: 0 entries of total size 0B INFO Memory: 8 samples, avg is 150.9MB, max is 231.2MB INFO Execution took 657.469ms ```

A minimal reproducible example or link to a public repository

```go package main import "fmt" type Foo struct { Bar string Baz string } func main() { f := Foo{} fmt.Println(f.Baz) } ``` Please note: if you unexport `Bar`, the linter works as expected.

Validation

Supporter

boring-cyborg[bot] commented 2 weeks ago

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

ldez commented 2 weeks ago

duplicate of #4218