nilnop

package module
v0.0.0-...-b59c8c4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2023 License: MIT Imports: 8 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAnalyzer

func NewAnalyzer(tgt ...Target) *analysis.Analyzer

Types

type Target

type Target struct {
	// Package path of the function or method.
	PkgPath string
	// Name of the function or method.
	FuncName string
	// Position of an argument which should not be nil.
	// ArgPos is 0-indexed.
	ArgPos int
}

Target represents a function or a method to be checked by nilnop.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL