Skip to content

Add default exclude for shadowing of err/ctx #166

@sudo-suhas

Description

@sudo-suhas

Version of golangci-lint: e17b954

Go environment:

go version && go env
go version go1.10.1 linux/amd64
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/suhaskaranth/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/suhaskaranth/installs/go-tools"
GORACE=""
GOROOT="/usr/lib/go-1.10"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.10/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build692454894=/tmp/go-build   -gno-record-gcc-switches"

It is considered idiomatic in Go to use the variable name err for error which could be returned from function invocations in a block of code which could also have shadowing like this:

err := funcA()
if err != nil {
	// ...
}

val, err := funcB()
if err != nil {
	// ...
}

Along the same lines, derived contexts are also named ctx and shadowing is common:

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

{
	ctx, cancel := context.WithCancel(ctx)

	db, err := mysql.NewClient(ctx)
	if err != nil {
		// ...
	}

	db.DoSomething()

	cancel() // close the DB conn of our imaginary pkg
}

This is not an issue if the default settings are used. It only shows up if check-shadowing is enabled for govet. So how about adding a default exclusion for this with declaration of "(err|ctx)" shadows declaration at?

Metadata

Metadata

Assignees

No one assigned

    Labels

    false positiveAn error is reported when one does not exist

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions