Skip to content

Linter: nilnop detects nil is passed to a function that does nothing for nil

License

Notifications You must be signed in to change notification settings

qawatake/nilnop

Repository files navigation

nilnop

Go Reference test

Linter nilnop detects nil is passed to a function that does nothing for nil.

func f() (err error) {
  reportError(err) // <- nil is passed to reportError
  err = errors.New("new error")
  reportError(err) // ok because err is not nil
  return err
}

// reportError panics if err is not nil.
func reportError(err error) {
  if err != nil {
    panic(err)
  }
}

You can try an example by running make run.example.

How to use

Build your nilnop binary by writing main.go like below.

package main

import (
  "github.com/qawatake/nilnop"
  "golang.org/x/tools/go/analysis/unitchecker"
)

func main() {
  unitchecker.Main(
    nilnop.NewAnalyzer(
      nilnop.Target{
        PkgPath:  "github.com/qawatake/nilnop/internal/example",
        FuncName: "reportError",
        ArgPos:   0,
      },
    ),
  )
}

Then, run go vet with your nilnop binary.

go vet -vettool=/path/to/your/nilnop ./...

About

Linter: nilnop detects nil is passed to a function that does nothing for nil

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published