named

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

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

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

README

named

Go Reference test codecov

Linter named ensures a target function to be called with a named return value.

A typical use case is to prevent the misuse of a error wrapping function such as derrors.Wrap, which does not allow the resulted error to be unwrapped. Function Bad below fails to wrap the error simply because it doesn't use the named return value as an argument.

func Good() (err error) {
  err = DoSomething()
  defer Wrap(&err, "wrapped!!") // ok because a named return value is passed.
  return fmt.Errorf("error from Good: %w", err)
}

func Bad() error {
  err := DoSomething()
  defer Wrap(&err, "wrapped!!") // <- err is not a named return value.
  return fmt.Errorf("error from Bad: %w", err)
}

func Wrap(errp *error, msg string) {
  if *errp == nil {
    return
  }
  *errp = fmt.Errorf("%s: %w", msg, *errp)
}

func DoSomething() error {
  return errors.New("original error")
}

func main() {
  err := Good()
  fmt.Println(err)
  err = Bad()
  fmt.Println(err)
  // Output:
  // wrapped!!: error from Good: original error
  // error from Bad: original error
}

How to use

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

package main

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

func main() {
  unitchecker.Main(
    named.NewAnalyzer(
      named.Deferred{
        PkgPath:  "pkg/in/which/target/func/is/defined",
        FuncName: "Wrap",
        ArgPos:   0,
      },
    ),
  )
}

Then, run go vet with your named binary.

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAnalyzer

func NewAnalyzer(deferred ...Deferred) *analysis.Analyzer

Types

type Deferred

type Deferred struct {
	// Package path of the function or method.
	PkgPath string
	// Name of the function or method.
	FuncName string
	// Position of an argument which should be a named return value.
	// ArgPos is 0-indexed.
	ArgPos int
}

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

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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