errcheck

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2024 License: MIT Imports: 14 Imported by: 35

Documentation

Overview

Package errcheck is the library used to implement the errcheck command-line tool.

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name:       "errcheck",
	Doc:        "check for unchecked errors",
	Run:        runAnalyzer,
	ResultType: reflect.TypeOf(Result{}),
}
View Source
var DefaultExcludedSymbols = []string{

	"(*bytes.Buffer).Write",
	"(*bytes.Buffer).WriteByte",
	"(*bytes.Buffer).WriteRune",
	"(*bytes.Buffer).WriteString",

	"fmt.Print",
	"fmt.Printf",
	"fmt.Println",
	"fmt.Fprint(*bytes.Buffer)",
	"fmt.Fprintf(*bytes.Buffer)",
	"fmt.Fprintln(*bytes.Buffer)",
	"fmt.Fprint(*strings.Builder)",
	"fmt.Fprintf(*strings.Builder)",
	"fmt.Fprintln(*strings.Builder)",
	"fmt.Fprint(os.Stderr)",
	"fmt.Fprintf(os.Stderr)",
	"fmt.Fprintln(os.Stderr)",

	"(*io.PipeReader).CloseWithError",
	"(*io.PipeWriter).CloseWithError",

	"math/rand.Read",
	"(*math/rand.Rand).Read",

	"(*strings.Builder).Write",
	"(*strings.Builder).WriteByte",
	"(*strings.Builder).WriteRune",
	"(*strings.Builder).WriteString",

	"(hash.Hash).Write",
}

DefaultExcludedSymbols is a list of symbol names that are usually excluded from checks by default.

Note, that they still need to be explicitly copied to Checker.Exclusions.Symbols

View Source
var (
	// ErrNoGoFiles is returned when CheckPackage is run on a package with no Go source files
	ErrNoGoFiles = errors.New("package contains no go source files")
)

Functions

func ReadExcludes added in v1.6.1

func ReadExcludes(path string) ([]string, error)

ReadExcludes reads an excludes file, a newline delimited file that lists patterns for which to allow unchecked errors.

Lines that start with two forward slashes are considered comments and are ignored.

Types

type Checker

type Checker struct {
	// Exclusions defines code packages, symbols, and other elements that will not be checked.
	Exclusions Exclusions

	// Tags are a list of build tags to use.
	Tags []string

	// The mod flag for go build.
	Mod string
}

Checker checks that you checked errors.

func (*Checker) CheckPackage

func (c *Checker) CheckPackage(pkg *packages.Package) Result

CheckPackage checks packages for errors that have not been checked.

It will exclude specific errors from analysis if the user has configured exclusions.

func (*Checker) LoadPackages

func (c *Checker) LoadPackages(paths ...string) ([]*packages.Package, error)

LoadPackages loads all the packages in all the paths provided. It uses the exclusions and build tags provided to by the user when loading the packages.

type Exclusions

type Exclusions struct {

	// Packages lists paths of excluded packages.
	Packages []string

	// SymbolRegexpsByPackage maps individual package paths to regular
	// expressions that match symbols to be excluded.
	//
	// Packages whose paths appear both here and in Packages list will
	// be excluded entirely.
	//
	// This is a legacy input that will be deprecated in errcheck version 2 and
	// should not be used.
	SymbolRegexpsByPackage map[string]*regexp.Regexp

	// Symbols lists patterns that exclude individual package symbols.
	//
	// For example:
	//
	//   "fmt.Errorf"              // function
	//   "fmt.Fprintf(os.Stderr)"  // function with set argument value
	//   "(hash.Hash).Write"       // method
	//
	Symbols []string

	// TestFiles excludes _test.go files.
	TestFiles bool

	// GeneratedFiles excludes generated source files.
	//
	// Source file is assumed to be generated if its contents
	// match the following regular expression:
	//
	//   ^// Code generated .* DO NOT EDIT\\.$
	//
	GeneratedFiles bool

	// BlankAssignments ignores assignments to blank identifier.
	BlankAssignments bool

	// TypeAssertions ignores unchecked type assertions.
	TypeAssertions bool
}

Exclusions define symbols and language elements that will be not checked

type Result

type Result struct {
	// UncheckedErrors is a list of all the unchecked errors in the package.
	// Printing an error reports its position within the file and the contents of the line.
	UncheckedErrors []UncheckedError
}

Result is returned from the CheckPackage function, and holds all the errors that were found to be unchecked in a package.

Aggregation can be done using the Append method for users that want to combine results from multiple packages.

func (*Result) Append

func (r *Result) Append(other Result)

Append appends errors to e. Append does not do any duplicate checking.

func (Result) Unique

func (r Result) Unique() Result

Returns the unique errors that have been accumulated. Duplicates may occur when a file containing an unchecked error belongs to > 1 package.

The method receiver remains unmodified after the call to Unique.

type UncheckedError

type UncheckedError struct {
	Pos          token.Position
	Line         string
	FuncName     string
	SelectorName string
}

UncheckedError indicates the position of an unchecked error return.

Jump to

Keyboard shortcuts

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