Skip to content

lukasschwab/nilinterface

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nilinterface

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?

Examples

See the testdata directory for short examples of programs that violate the rule.

Usage

Standalone

  1. Install the binary; either run go install github.com/lukasschwab/nilinterface@latest or clone this repo and install from local source:

    go install ./cmd/nilinterface/nilinterface.go
  2. Invoke nilinterface directly:

    nilinterface ./...
Example from project root
# Standalone binary installation
go install ./cmd/nilinterface/nilinterface.go
cd ./pkg/analyzer/testdata
# Invocation
go vet -vettool=$(which nilinterface) ./...

With go vet

  1. Follow the Standalone instructions to install a nilinterface binary.

  2. Invoke nilinterface through vet:

    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) ./...

With golangci-lint

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.

  1. In your project, create a new .custom-gcl.yml file 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.

  2. Build custom-gcl, a version of golangci-lint with nilinterface (and any others pinned in .custom-gcl.yml) available to it.

    golangci-lint custom -v
  3. If you have a .golangci.yml configuration 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.

In VS Code

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"
  },
}

In GitHub Actions

At time of writing, golangci-lint's official GitHub Action doesn't support building and running custom linters.

About

Go linter that forbids passing `nil` as an interface argument to function calls.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors