Skip to content

errcheck does not work on Close #4262

Closed
@Seb-C

Description

@Seb-C

Welcome

Description of the problem

errcheck does not report any problem related to any call to the Close method, despite having an appropriate configuration. I have a use case where those errors are significant and must be detected and handled.

When I run errcheck -blank -asserts ./... (latest version of this repository) manually without golangci-lint, I get the expected issues reported:

cmd/main.go:30:18:      defer file.Close()
cmd/main.go:31:12:      file.Close()
cmd/main.go:32:2:       _ = file.Close()
cmd/main.go:34:5:       _, _ = os.Create("bar")

When I do the same thing via golangci-lint, I don't get the expected errors, only the last one:

cmd/main.go:34:5: Error return value of `os.Create` is not checked (errcheck)
        _, _ = os.Create("bar")
           ^

Version of golangci-lint

$ golangci-lint --version
golangci-lint has version 1.55.2 built with go1.21.3 from e3c2265f on 2023-11-03T12:59:25Z

Configuration

linters:
  disable-all: true
  enable:
    - errcheck
linters-settings:
  errcheck:
    check-type-assertions: true
    check-blank: true
    disable-default-exclusions: true

Go environment

$ go version && go env
go version go1.21.3 linux/amd64
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/sebastien/.cache/go-build'
GOENV='/home/sebastien/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/sebastien/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/sebastien/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go-1.21'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go-1.21/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.3'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/sebastien/Programmation/astral-divide/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 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2728494281=/tmp/go-build -gno-record-gcc-switches'

Verbose output of running

$ golangci-lint cache clean
$ golangci-lint run -v
INFO [config_reader] Config search paths: [./ /[redacted path to project] /[redacted path to parent dir of the project] /[redacted path to home] /home /] 
INFO [config_reader] Used config file .golangci.yml 
INFO [lintersdb] Active 1 linters: [errcheck]     
INFO [loader] Go packages loading at mode 575 (types_sizes|exports_file|files|imports|compiled_files|deps|name) took 432.683742ms 
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 9.728706ms 
INFO [linters_context/goanalysis] analyzers took 17.777243ms with top 10 stages: errcheck: 17.58221ms, typecheck: 195.033µs 
INFO [runner] Issues before processing: 41, after processing: 1 
INFO [runner] Processors filtering stat (out/in): diff: 1/1, max_same_issues: 1/1, path_shortener: 1/1, path_prettifier: 41/41, skip_dirs: 41/41, identifier_marker: 41/41, max_per_file_from_linter: 1/1, max_from_linter: 1/1, severity-rules: 1/1, fixer: 1/1, sort_results: 1/1, cgo: 41/41, filename_unadjuster: 41/41, skip_files: 41/41, nolint: 1/1, source_code: 1/1, uniq_by_line: 1/1, path_prefixer: 1/1, autogenerated_exclude: 41/41, exclude: 41/41, exclude-rules: 1/41 
INFO [runner] processing took 1.333199ms with stages: path_prettifier: 374.815µs, identifier_marker: 328.037µs, autogenerated_exclude: 224.271µs, nolint: 216.868µs, skip_dirs: 89.639µs, exclude-rules: 60.534µs, source_code: 29.686µs, cgo: 2.645µs, max_same_issues: 1.413µs, path_shortener: 1.122µs, uniq_by_line: 982ns, filename_unadjuster: 881ns, max_from_linter: 712ns, fixer: 290ns, max_per_file_from_linter: 271ns, skip_files: 271ns, exclude: 201ns, severity-rules: 190ns, diff: 161ns, sort_results: 140ns, path_prefixer: 70ns 
INFO [runner] linters took 149.488983ms with stages: goanalysis_metalinter: 148.101482ms 
cmd/main.go:34:5: Error return value of `os.Create` is not checked (errcheck)
        _, _ = os.Create("bar")
           ^
INFO File cache stats: 1 entries of total size 10.1KiB 
INFO Memory: 7 samples, avg is 40.8MB, max is 98.6MB 
INFO Execution took 595.449448ms

A minimal reproducible example or link to a public repository

	file, err := os.Create("foo")
	if err != nil {
		panic(err)
	}

	// I expect an error for each case below
	defer file.Close()
	file.Close()
	_ = file.Close()

	_, _ = os.Create("bar") // I only get an error here

Validation

  • Yes, I've included all information above (version, config, etc.).

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions