Skip to content
This repository has been archived by the owner on Jun 2, 2023. It is now read-only.

golangci: support running errcheck with all its options #1

Merged
merged 1 commit into from
Oct 3, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions golangci/golangci.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,59 @@ import (
"golang.org/x/tools/go/loader"
)

var dotStar = regexp.MustCompile(".*")

type Issue errcheck.UncheckedError

func Run(program *loader.Program, checkBlank, checkAsserts bool) ([]Issue, error) {
return RunWithConfig(program, &Config{
Blank: checkBlank,
Asserts: checkAsserts,
})
}

// Config is a copy of the `errcheck.Checker` with exported `Exclude` field.
type Config struct {
// ignore is a map of package names to regular expressions. Identifiers from a package are
// checked against its regular expressions and if any of the expressions match the call
// is not checked.
Ignore map[string]*regexp.Regexp

// If blank is true then assignments to the blank identifier are also considered to be
// ignored errors.
Blank bool

// If asserts is true then ignored type assertion results are also checked
Asserts bool

// build tags
Tags []string

Verbose bool

// If true, checking of _test.go files is disabled
WithoutTests bool

// Excluded functions.
Exclude map[string]bool
}

// RunWithConfig runs the `errchecker` linter with all its options.
func RunWithConfig(program *loader.Program, c *Config) ([]Issue, error) {
checker := errcheck.NewChecker()
checker.Blank = checkBlank
checker.Asserts = checkAsserts
checker.Tags = c.Tags
checker.Blank = c.Blank
checker.Asserts = c.Asserts
checker.Verbose = c.Verbose
checker.WithoutTests = c.WithoutTests

checker.SetExclude(c.Exclude)

checker.Ignore = map[string]*regexp.Regexp{
"fmt": regexp.MustCompile(".*"),
"fmt": dotStar,
}
for pkg, re := range c.Ignore {
checker.Ignore[pkg] = re
}

if err := checker.CheckProgram(program); err != nil {
Expand Down