Extremely simple go/analysis-based linter for forbidding passing literal nil as an interface argument to function calls. The function expects something that implements the interface; why are you passing something that'll panic if used?
See the testdata directory for short examples of programs that violate the rule.
-
Install the binary; either run
go install github.com/lukasschwab/nilinterface@latestor clone this repo and install from local source:go install ./cmd/nilinterface/nilinterface.go
-
Invoke
nilinterfacedirectly:nilinterface ./...
Example from project root
# Standalone binary installation
go install ./cmd/nilinterface/nilinterface.go
cd ./pkg/analyzer/testdata
# Invocation
go vet -vettool=$(which nilinterface) ./...-
Follow the Standalone instructions to install a
nilinterfacebinary. -
Invoke
nilinterfacethroughvet:go vet -vettool=$(which nilinterface)
Example from project root
# Standalone binary installation
go install ./cmd/nilinterface/nilinterface.go
cd ./pkg/analyzer/testdata
# Invocation
go vet -vettool=$(which nilinterface) ./...Warning
This guide describes using golangci-lint's Module Plugin System, not the Go plugin system. These are easily confused, and old documentation on golangci-lint extensibility doesn't explicitly distinguish the two.
If you're troubleshooting, advice regarding .so files and the CGO_ENABLED build flag is likely specific to the Go plugin system.
This guide assumes you're already using golangci-lint. See also golangci-lint's (currently crummy) docs: Module Plugin System / The Automatic Way.
-
In your project, create a new
.custom-gcl.ymlfile pinning a version of this linter:version: v1.63.4 plugins: - module: 'github.com/lukasschwab/nilinterface' import: 'github.com/lukasschwab/nilinterface/pkg/golangci-linter' version: v0.0.6
For an example, see .custom-gcl.yml.
-
Build
custom-gcl, a version ofgolangci-lintwithnilinterface(and any others pinned in.custom-gcl.yml) available to it.golangci-lint custom -v
-
If you have a
.golangci.ymlconfiguration file, update it to enable the custom linter.linters-settings: custom: nilinterface: type: 'module' description: 'forbids passing `nil` as an interface argument to function calls' # If you have `disable-all: true`, add nilinterface to your enabled linters. linters: disable-all: true enable: - nilinterface
When you update .custom-gcl.yml (e.g. to pin a new linter version), you will need to rebuild custom-gcl to reflect those changes. If you use make, you can integrate the linter build step into your Makefile targets; see Makefile.
custom-gcl is a drop-in replacement for golangci-lint. You can specify the substitution in VS Code with the go.alternateTools setting. If custom-gcl is built in your VS Code workspace root:
{
"go.lintTool": "golangci-lint",
"go.alternateTools": {
"golangci-lint": "${workspaceFolder}/custom-gcl"
},
}At time of writing, golangci-lint's official GitHub Action doesn't support building and running custom linters.
- Consider using
golangci-lint-custom-plugins-action. - To build your own, see @fr12k's comment suggesting a workaround custom action.