Update vendored tools/.

This commit is contained in:
Will Rouesnel 2017-08-03 21:19:17 +10:00
parent 1afbd62ab1
commit a7ff84a674
22 changed files with 1452 additions and 1109 deletions

6
tools/.gitignore vendored
View File

@ -1,4 +1,4 @@
/pkg
bin
tools.deps
metatools.deps
/bin
/tools.deps
/metatools.deps

View File

@ -18,6 +18,7 @@
- [1. Update to the latest build of gometalinter and all linters](#1-update-to-the-latest-build-of-gometalinter-and-all-linters)
- [2. Analyse the debug output](#2-analyse-the-debug-output)
- [3. Report an issue.](#3-report-an-issue)
- [How do I filter issues between two git refs?](#how-do-i-filter-issues-between-two-git-refs)
- [Details](#details)
- [Checkstyle XML format](#checkstyle-xml-format)
@ -54,7 +55,7 @@ It is intended for use with editor/IDE integration.
## Supported linters
- [go vet](https://golang.org/cmd/vet/) - Reports potential errors that otherwise compile.
- [go vet --shadow](https://golang.org/cmd/vet/#hdr-Shadowed_variables) - Reports variables that may have been unintentionally shadowed.
- [go tool vet --shadow](https://golang.org/cmd/vet/#hdr-Shadowed_variables) - Reports variables that may have been unintentionally shadowed.
- [gotype](https://golang.org/x/tools/cmd/gotype) - Syntactic and semantic analysis similar to the Go compiler.
- [deadcode](https://github.com/tsenart/deadcode) - Finds unused code.
- [gocyclo](https://github.com/alecthomas/gocyclo) - Computes the cyclomatic complexity of functions.
@ -63,13 +64,12 @@ It is intended for use with editor/IDE integration.
- [structcheck](https://github.com/opennota/check) - Find unused struct fields.
- [aligncheck](https://github.com/opennota/check) - Warn about un-optimally aligned structures.
- [errcheck](https://github.com/kisielk/errcheck) - Check that error return values are used.
- [megacheck](https://github.com/dominikh/go-tools/tree/master/cmd/megacheck) - Run staticcheck, gosimple and unused, sharing work.
- [dupl](https://github.com/mibk/dupl) - Reports potentially duplicated code.
- [ineffassign](https://github.com/gordonklaus/ineffassign/blob/master/list) - Detect when assignments to *existing* variables are not used.
- [interfacer](https://github.com/mvdan/interfacer) - Suggest narrower interfaces that can be used.
- [unconvert](https://github.com/mdempsky/unconvert) - Detect redundant type conversions.
- [goconst](https://github.com/jgautheron/goconst) - Finds repeated strings that could be replaced by a constant.
- [gosimple](https://github.com/dominikh/go-tools/tree/master/cmd/gosimple) - Report simplifications in code.
- [staticcheck](https://github.com/dominikh/go-tools/tree/master/cmd/staticcheck) - Statically detect bugs, both obvious and subtle ones.
- [gas](https://github.com/GoASTScanner/gas) - Inspects source code for security problems by scanning the Go AST.
Disabled by default (enable with `--enable=<linter>`):
@ -78,11 +78,13 @@ Disabled by default (enable with `--enable=<linter>`):
- [test](http://golang.org/pkg/testing/) - Show location of test failures from the stdlib testing module.
- [gofmt -s](https://golang.org/cmd/gofmt/) - Checks if the code is properly formatted and could not be further simplified.
- [goimports](https://godoc.org/golang.org/x/tools/cmd/goimports) - Checks missing or unreferenced package imports.
- [gosimple](https://github.com/dominikh/go-tools/tree/master/cmd/gosimple) - Report simplifications in code.
- [lll](https://github.com/walle/lll) - Report long lines (see `--line-length=N`).
- [misspell](https://github.com/client9/misspell) - Finds commonly misspelled English words.
- [unparam](https://github.com/mvdan/unparam) - Find unused function parameters.
- [unused](https://github.com/dominikh/go-tools/tree/master/cmd/unused) - Find unused variables.
- [safesql](https://github.com/stripe/safesql) - Finds potential SQL injection vulnerabilities.
- [staticcheck](https://github.com/dominikh/go-tools/tree/master/cmd/staticcheck) - Statically detect bugs, both obvious and subtle ones.
Additional linters can be added through the command line with `--linter=NAME:COMMAND:PATTERN` (see [below](#details)).
@ -95,13 +97,13 @@ in `config.go`.
The configuration file mostly corresponds to command-line flags, with the following exceptions:
- Linters defined in the configuration file will overlay existing definitions, not replace them.
- "Enable" defines the exact set of linters that will be enabled.
- "Enable" defines the exact set of linters that will be enabled (default
linters are disabled).
Here is an example configuration file:
```json
{
"DisableAll": true,
"Enable": ["deadcode", "unconvert"]
}
```
@ -291,6 +293,21 @@ failing.
Failing all else, if the problem looks like a bug please file an issue and
include the output of `gometalinter --debug`.
### How do I filter issues between two git refs?
[revgrep](https://github.com/bradleyfalzon/revgrep) can be used to filter the output of `gometalinter`
to show issues on lines that have changed between two git refs, such as unstaged changes, changes in
`HEAD` vs `master` and between `master` and `origin/master`. See the project's documentation and `-help`
usage for more information.
```
go get -u github.com/bradleyfalzon/revgrep/...
gometalinter |& revgrep # If unstaged changes or untracked files, those issues are shown.
gometalinter |& revgrep # Else show issues in the last commit.
gometalinter |& revgrep master # Show issues between master and HEAD (or any other reference).
gometalinter |& revgrep origin/master # Show issues that haven't been pushed.
```
## Details
Additional linters can be configured via the command line:

View File

@ -18,6 +18,13 @@ type (
}
)
func maybeAggregateIssues(issues chan *Issue) chan *Issue {
if !config.Aggregate {
return issues
}
return aggregateIssues(issues)
}
func aggregateIssues(issues chan *Issue) chan *Issue {
out := make(chan *Issue, 1000000)
issueMap := make(map[issueKey]*multiIssue)
@ -30,18 +37,18 @@ func aggregateIssues(issues chan *Issue) chan *Issue {
message: issue.Message,
}
if existing, ok := issueMap[key]; ok {
existing.linterNames = append(existing.linterNames, issue.Linter.Name)
existing.linterNames = append(existing.linterNames, issue.Linter)
} else {
issueMap[key] = &multiIssue{
Issue: issue,
linterNames: []string{issue.Linter.Name},
linterNames: []string{issue.Linter},
}
}
}
for _, multi := range issueMap {
issue := multi.Issue
sort.Strings(multi.linterNames)
issue.Linter.Name = strings.Join(multi.linterNames, ", ")
issue.Linter = strings.Join(multi.linterNames, ", ")
out <- issue
}
close(out)

View File

@ -52,7 +52,7 @@ func outputToCheckstyle(issues chan *Issue) int {
Line: issue.Line,
Message: issue.Message,
Severity: string(issue.Severity),
Source: issue.Linter.Name,
Source: issue.Linter,
})
status = 1
}

View File

@ -1,11 +1,10 @@
package main
import (
"encoding/json"
"runtime"
"text/template"
"time"
"gopkg.in/alecthomas/kingpin.v3-unstable"
)
// Config for gometalinter. This can be loaded from a JSON file with --config.
@ -45,144 +44,66 @@ type Config struct { // nolint: aligncheck
DuplThreshold int
Sort []string
Test bool
Deadline time.Duration `json:"-"`
Deadline jsonDuration
Errors bool
JSON bool
Checkstyle bool
EnableGC bool
Aggregate bool
DeadlineJSONCrutch string `json:"Deadline"`
EnableAll bool
}
type jsonDuration time.Duration
func (td *jsonDuration) UnmarshalJSON(raw []byte) error {
var durationAsString string
if err := json.Unmarshal(raw, &durationAsString); err != nil {
return err
}
duration, err := time.ParseDuration(durationAsString)
*td = jsonDuration(duration)
return err
}
// Duration returns the value as a time.Duration
func (td *jsonDuration) Duration() time.Duration {
return time.Duration(*td)
}
// TODO: should be a field on Config struct
var formatTemplate = &template.Template{}
var sortKeys = []string{"none", "path", "line", "column", "severity", "message", "linter"}
// Configuration defaults.
var (
vetRe = `^(?:vet:.*?\.go:\s+(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*))|(?:(?P<path>.*?\.go):(?P<line>\d+):\s*(?P<message>.*))$`
var config = &Config{
Format: "{{.Path}}:{{.Line}}:{{if .Col}}{{.Col}}{{end}}:{{.Severity}}: {{.Message}} ({{.Linter}})",
predefinedPatterns = map[string]string{
"PATH:LINE:COL:MESSAGE": `^(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*)$`,
"PATH:LINE:MESSAGE": `^(?P<path>.*?\.go):(?P<line>\d+):\s*(?P<message>.*)$`,
}
formatTemplate = &template.Template{}
installMap = map[string]string{
"aligncheck": "github.com/opennota/check/cmd/aligncheck",
"deadcode": "github.com/tsenart/deadcode",
"dupl": "github.com/mibk/dupl",
"errcheck": "github.com/kisielk/errcheck",
"gas": "github.com/GoASTScanner/gas",
"goconst": "github.com/jgautheron/goconst/cmd/goconst",
"gocyclo": "github.com/alecthomas/gocyclo",
"goimports": "golang.org/x/tools/cmd/goimports",
"golint": "github.com/golang/lint/golint",
"gosimple": "honnef.co/go/tools/cmd/gosimple",
"gotype": "golang.org/x/tools/cmd/gotype",
"ineffassign": "github.com/gordonklaus/ineffassign",
"interfacer": "github.com/mvdan/interfacer/cmd/interfacer",
"lll": "github.com/walle/lll/cmd/lll",
"misspell": "github.com/client9/misspell/cmd/misspell",
"safesql": "github.com/stripe/safesql",
"staticcheck": "honnef.co/go/tools/cmd/staticcheck",
"structcheck": "github.com/opennota/check/cmd/structcheck",
"unconvert": "github.com/mdempsky/unconvert",
"unparam": "github.com/mvdan/unparam",
"unused": "honnef.co/go/tools/cmd/unused",
"varcheck": "github.com/opennota/check/cmd/varcheck",
}
acceptsEllipsis = map[string]bool{
"aligncheck": true,
"errcheck": true,
"golint": true,
"gosimple": true,
"interfacer": true,
"staticcheck": true,
"structcheck": true,
"test": true,
"varcheck": true,
"unconvert": true,
}
slowLinters = []string{"structcheck", "varcheck", "errcheck", "aligncheck", "testify", "test", "interfacer", "unconvert", "deadcode", "safesql", "staticcheck", "unparam", "unused", "gosimple"}
sortKeys = []string{"none", "path", "line", "column", "severity", "message", "linter"}
// Linter definitions.
linterDefinitions = map[string]string{
"aligncheck": `aligncheck {path}:^(?:[^:]+: )?(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.+)$`,
"deadcode": `deadcode {path}:^deadcode: (?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*)$`,
"dupl": `dupl -plumbing -threshold {duplthreshold} {path}/*.go:^(?P<path>.*?\.go):(?P<line>\d+)-\d+:\s*(?P<message>.*)$`,
"errcheck": `errcheck -abspath {path}:PATH:LINE:COL:MESSAGE`,
"gas": `gas -fmt=csv {path}/*.go:^(?P<path>.*?\.go),(?P<line>\d+),(?P<message>[^,]+,[^,]+,[^,]+)`,
"goconst": `goconst -min-occurrences {min_occurrences} -min-length {min_const_length} {path}:PATH:LINE:COL:MESSAGE`,
"gocyclo": `gocyclo -over {mincyclo} {path}:^(?P<cyclo>\d+)\s+\S+\s(?P<function>\S+)\s+(?P<path>.*?\.go):(?P<line>\d+):(\d+)$`,
"gofmt": `gofmt -l -s {path}/*.go:^(?P<path>.*?\.go)$`,
"goimports": `goimports -l {path}/*.go:^(?P<path>.*?\.go)$`,
"golint": "golint -min_confidence {min_confidence} {path}:PATH:LINE:COL:MESSAGE",
"gosimple": "gosimple {path}:PATH:LINE:COL:MESSAGE",
"gotype": "gotype -e {tests=-a} {path}:PATH:LINE:COL:MESSAGE",
"ineffassign": `ineffassign -n {path}:PATH:LINE:COL:MESSAGE`,
"interfacer": `interfacer {path}:PATH:LINE:COL:MESSAGE`,
"lll": `lll -g -l {maxlinelength} {path}/*.go:PATH:LINE:MESSAGE`,
"misspell": "misspell -j 1 {path}/*.go:PATH:LINE:COL:MESSAGE",
"safesql": `safesql {path}:^- (?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+)$`,
"staticcheck": "staticcheck {path}:PATH:LINE:COL:MESSAGE",
"structcheck": `structcheck {tests=-t} {path}:^(?:[^:]+: )?(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.+)$`,
"test": `go test {path}:^--- FAIL: .*$\s+(?P<path>.*?\.go):(?P<line>\d+): (?P<message>.*)$`,
"testify": `go test {path}:Location:\s+(?P<path>.*?\.go):(?P<line>\d+)$\s+Error:\s+(?P<message>[^\n]+)`,
"unconvert": "unconvert {path}:PATH:LINE:COL:MESSAGE",
"unparam": `unparam {path}:PATH:LINE:COL:MESSAGE`,
"unused": `unused {path}:PATH:LINE:COL:MESSAGE`,
"varcheck": `varcheck {path}:^(?:[^:]+: )?(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*)$`,
"vet": `go tool vet {path}/*.go:` + vetRe,
"vetshadow": `go tool vet --shadow {path}/*.go:` + vetRe,
}
pathsArg = kingpin.Arg("path", "Directories to lint. Defaults to \".\". <path>/... will recurse.").Strings()
config = &Config{
Format: "{{.Path}}:{{.Line}}:{{if .Col}}{{.Col}}{{end}}:{{.Severity}}: {{.Message}} ({{.Linter}})",
Severity: map[string]string{
"gotype": "error",
"test": "error",
"testify": "error",
"vet": "error",
},
MessageOverride: map[string]string{
"errcheck": "error return value not checked ({message})",
"gocyclo": "cyclomatic complexity {cyclo} of function {function}() is high (> {mincyclo})",
"gofmt": "file is not gofmted with -s",
"goimports": "file is not goimported",
"safesql": "potentially unsafe SQL statement",
"structcheck": "unused struct field {message}",
"unparam": "parameter {message}",
"varcheck": "unused variable or constant {message}",
},
Enable: []string{
"aligncheck",
"deadcode",
"errcheck",
"gas",
"goconst",
"gocyclo",
"golint",
"gosimple",
"gotype",
"ineffassign",
"interfacer",
"staticcheck",
"structcheck",
"unconvert",
"varcheck",
"vet",
"vetshadow",
},
VendoredLinters: true,
Concurrency: runtime.NumCPU(),
Cyclo: 10,
LineLength: 80,
MinConfidence: 0.8,
MinOccurrences: 3,
MinConstLength: 3,
DuplThreshold: 50,
Sort: []string{"none"},
Deadline: time.Second * 30,
}
)
Severity: map[string]string{
"gotype": "error",
"test": "error",
"testify": "error",
"vet": "error",
},
MessageOverride: map[string]string{
"errcheck": "error return value not checked ({message})",
"gocyclo": "cyclomatic complexity {cyclo} of function {function}() is high (> {mincyclo})",
"gofmt": "file is not gofmted with -s",
"goimports": "file is not goimported",
"safesql": "potentially unsafe SQL statement",
"structcheck": "unused struct field {message}",
"unparam": "parameter {message}",
"varcheck": "unused variable or constant {message}",
},
Enable: defaultEnabled(),
VendoredLinters: true,
Concurrency: runtime.NumCPU(),
Cyclo: 10,
LineLength: 80,
MinConfidence: 0.8,
MinOccurrences: 3,
MinConstLength: 3,
DuplThreshold: 50,
Sort: []string{"none"},
Deadline: jsonDuration(time.Second * 30),
}

View File

@ -24,14 +24,14 @@ func (i *ignoredRange) matches(issue *Issue) bool {
return true
}
for _, l := range i.linters {
if l == issue.Linter.Name {
if l == issue.Linter {
return true
}
}
return false
}
func (i *ignoredRange) near(col, start, end int) bool {
func (i *ignoredRange) near(col, start int) bool {
return col == i.col && i.end == start-1
}
@ -42,15 +42,13 @@ func (ir ignoredRanges) Swap(i, j int) { ir[i], ir[j] = ir[j], ir[i] }
func (ir ignoredRanges) Less(i, j int) bool { return ir[i].end < ir[j].end }
type directiveParser struct {
paths []string
lock sync.Mutex
files map[string]ignoredRanges
fset *token.FileSet
}
func newDirectiveParser(paths []string) *directiveParser {
func newDirectiveParser() *directiveParser {
return &directiveParser{
paths: paths,
files: map[string]ignoredRanges{},
fset: token.NewFileSet(),
}
@ -92,7 +90,7 @@ func (a *rangeExpander) Visit(node ast.Node) ast.Visitor {
found := sort.Search(len(a.ranges), func(i int) bool {
return a.ranges[i].end+1 >= start
})
if found < len(a.ranges) && a.ranges[found].near(startPos.Column, start, end) {
if found < len(a.ranges) && a.ranges[found].near(startPos.Column, start) {
r := a.ranges[found]
if r.start > start {
r.start = start
@ -144,12 +142,6 @@ func extractCommentGroupRange(fset *token.FileSet, comments ...*ast.CommentGroup
return
}
func (d *directiveParser) in(n ast.Node, issue *Issue) bool {
start := d.fset.Position(n.Pos())
end := d.fset.Position(n.End())
return issue.Line >= start.Line && issue.Line <= end.Line
}
func filterIssuesViaDirectives(directives *directiveParser, issues chan *Issue) chan *Issue {
out := make(chan *Issue, 1000000)
go func() {

View File

@ -0,0 +1,384 @@
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/google/shlex"
"gopkg.in/alecthomas/kingpin.v3-unstable"
)
type Vars map[string]string
func (v Vars) Copy() Vars {
out := Vars{}
for k, v := range v {
out[k] = v
}
return out
}
func (v Vars) Replace(s string) string {
for k, v := range v {
prefix := regexp.MustCompile(fmt.Sprintf("{%s=([^}]*)}", k))
if v != "" {
s = prefix.ReplaceAllString(s, "$1")
} else {
s = prefix.ReplaceAllString(s, "")
}
s = strings.Replace(s, fmt.Sprintf("{%s}", k), v, -1)
}
return s
}
// Severity of linter message.
type Severity string
// Linter message severity levels.
const ( // nolint: deadcode
Error Severity = "error"
Warning Severity = "warning"
)
type Issue struct {
Linter string `json:"linter"`
Severity Severity `json:"severity"`
Path string `json:"path"`
Line int `json:"line"`
Col int `json:"col"`
Message string `json:"message"`
}
func (i *Issue) String() string {
buf := new(bytes.Buffer)
err := formatTemplate.Execute(buf, i)
kingpin.FatalIfError(err, "Invalid output format")
return buf.String()
}
type linterState struct {
*Linter
paths []string
issues chan *Issue
vars Vars
exclude *regexp.Regexp
include *regexp.Regexp
deadline <-chan time.Time
}
func (l *linterState) Partitions() ([][]string, error) {
command := l.vars.Replace(l.Command)
cmdArgs, err := parseCommand(command)
if err != nil {
return nil, err
}
parts, err := l.Linter.PartitionStrategy(cmdArgs, l.paths)
if err != nil {
return nil, err
}
return parts, nil
}
func runLinters(linters map[string]*Linter, paths []string, concurrency int, exclude, include *regexp.Regexp) (chan *Issue, chan error) {
errch := make(chan error, len(linters))
concurrencych := make(chan bool, concurrency)
incomingIssues := make(chan *Issue, 1000000)
processedIssues := filterIssuesViaDirectives(
newDirectiveParser(),
maybeSortIssues(maybeAggregateIssues(incomingIssues)))
vars := Vars{
"duplthreshold": fmt.Sprintf("%d", config.DuplThreshold),
"mincyclo": fmt.Sprintf("%d", config.Cyclo),
"maxlinelength": fmt.Sprintf("%d", config.LineLength),
"min_confidence": fmt.Sprintf("%f", config.MinConfidence),
"min_occurrences": fmt.Sprintf("%d", config.MinOccurrences),
"min_const_length": fmt.Sprintf("%d", config.MinConstLength),
"tests": "",
}
if config.Test {
vars["tests"] = "-t"
}
wg := &sync.WaitGroup{}
for _, linter := range linters {
deadline := time.After(config.Deadline.Duration())
state := &linterState{
Linter: linter,
issues: incomingIssues,
paths: paths,
vars: vars,
exclude: exclude,
include: include,
deadline: deadline,
}
partitions, err := state.Partitions()
if err != nil {
errch <- err
continue
}
for _, args := range partitions {
wg.Add(1)
// Call the goroutine with a copy of the args array so that the
// contents of the array are not modified by the next iteration of
// the above for loop
go func(args []string) {
concurrencych <- true
err := executeLinter(state, args)
if err != nil {
errch <- err
}
<-concurrencych
wg.Done()
}(append(args))
}
}
go func() {
wg.Wait()
close(incomingIssues)
close(errch)
}()
return processedIssues, errch
}
func executeLinter(state *linterState, args []string) error {
if len(args) == 0 {
return fmt.Errorf("missing linter command")
}
start := time.Now()
debug("executing %s", strings.Join(args, " "))
buf := bytes.NewBuffer(nil)
command := args[0]
cmd := exec.Command(command, args[1:]...) // nolint: gas
cmd.Stdout = buf
cmd.Stderr = buf
err := cmd.Start()
if err != nil {
return fmt.Errorf("failed to execute linter %s: %s", command, err)
}
done := make(chan bool)
go func() {
err = cmd.Wait()
done <- true
}()
// Wait for process to complete or deadline to expire.
select {
case <-done:
case <-state.deadline:
err = fmt.Errorf("deadline exceeded by linter %s (try increasing --deadline)",
state.Name)
kerr := cmd.Process.Kill()
if kerr != nil {
warning("failed to kill %s: %s", state.Name, kerr)
}
return err
}
if err != nil {
debug("warning: %s returned %s: %s", command, err, buf.String())
}
processOutput(state, buf.Bytes())
elapsed := time.Since(start)
debug("%s linter took %s", state.Name, elapsed)
return nil
}
func parseCommand(command string) ([]string, error) {
args, err := shlex.Split(command)
if err != nil {
return nil, err
}
if len(args) == 0 {
return nil, fmt.Errorf("invalid command %q", command)
}
exe, err := exec.LookPath(args[0])
if err != nil {
return nil, err
}
return append([]string{exe}, args[1:]...), nil
}
// nolint: gocyclo
func processOutput(state *linterState, out []byte) {
re := state.regex
all := re.FindAllSubmatchIndex(out, -1)
debug("%s hits %d: %s", state.Name, len(all), state.Pattern)
cwd, err := os.Getwd()
if err != nil {
warning("failed to get working directory %s", err)
}
// Create a local copy of vars so they can be modified by the linter output
vars := state.vars.Copy()
for _, indices := range all {
group := [][]byte{}
for i := 0; i < len(indices); i += 2 {
var fragment []byte
if indices[i] != -1 {
fragment = out[indices[i]:indices[i+1]]
}
group = append(group, fragment)
}
issue := &Issue{Line: 1, Linter: state.Linter.Name}
for i, name := range re.SubexpNames() {
if group[i] == nil {
continue
}
part := string(group[i])
if name != "" {
vars[name] = part
}
switch name {
case "path":
issue.Path = relativePath(cwd, part)
case "line":
n, err := strconv.ParseInt(part, 10, 32)
kingpin.FatalIfError(err, "line matched invalid integer")
issue.Line = int(n)
case "col":
n, err := strconv.ParseInt(part, 10, 32)
kingpin.FatalIfError(err, "col matched invalid integer")
issue.Col = int(n)
case "message":
issue.Message = part
case "":
}
}
// TODO: set messageOveride and severity on the Linter instead of reading
// them directly from the static config
if m, ok := config.MessageOverride[state.Name]; ok {
issue.Message = vars.Replace(m)
}
if sev, ok := config.Severity[state.Name]; ok {
issue.Severity = Severity(sev)
} else {
issue.Severity = Warning
}
if state.exclude != nil && state.exclude.MatchString(issue.String()) {
continue
}
if state.include != nil && !state.include.MatchString(issue.String()) {
continue
}
state.issues <- issue
}
}
func relativePath(root, path string) string {
fallback := path
root = resolvePath(root)
path = resolvePath(path)
var err error
path, err = filepath.Rel(root, path)
if err != nil {
warning("failed to make %s a relative path: %s", fallback, err)
return fallback
}
return path
}
func resolvePath(path string) string {
var err error
fallback := path
if !filepath.IsAbs(path) {
path, err = filepath.Abs(path)
if err != nil {
warning("failed to make %s an absolute path: %s", fallback, err)
return fallback
}
}
path, err = filepath.EvalSymlinks(path)
if err != nil {
warning("failed to resolve symlinks in %s: %s", fallback, err)
return fallback
}
return path
}
type sortedIssues struct {
issues []*Issue
order []string
}
func (s *sortedIssues) Len() int { return len(s.issues) }
func (s *sortedIssues) Swap(i, j int) { s.issues[i], s.issues[j] = s.issues[j], s.issues[i] }
// nolint: gocyclo
func (s *sortedIssues) Less(i, j int) bool {
l, r := s.issues[i], s.issues[j]
for _, key := range s.order {
switch key {
case "path":
if l.Path > r.Path {
return false
}
case "line":
if l.Line > r.Line {
return false
}
case "column":
if l.Col > r.Col {
return false
}
case "severity":
if l.Severity > r.Severity {
return false
}
case "message":
if l.Message > r.Message {
return false
}
case "linter":
if l.Linter > r.Linter {
return false
}
}
}
return true
}
func maybeSortIssues(issues chan *Issue) chan *Issue {
if reflect.DeepEqual([]string{"none"}, config.Sort) {
return issues
}
out := make(chan *Issue, 1000000)
sorted := &sortedIssues{
issues: []*Issue{},
order: config.Sort,
}
go func() {
for issue := range issues {
sorted.issues = append(sorted.issues, issue)
}
sort.Sort(sorted)
for _, issue := range sorted.issues {
out <- issue
}
close(out)
}()
return out
}

View File

@ -0,0 +1,392 @@
package main
import (
"fmt"
"os"
"os/exec"
"regexp"
"sort"
"strings"
"gopkg.in/alecthomas/kingpin.v3-unstable"
)
type LinterConfig struct {
Name string
Command string
Pattern string
InstallFrom string
PartitionStrategy partitionStrategy
IsFast bool
defaultEnabled bool
}
type Linter struct {
LinterConfig
regex *regexp.Regexp
}
// NewLinter returns a new linter from a config
func NewLinter(config LinterConfig) (*Linter, error) {
if p, ok := predefinedPatterns[config.Pattern]; ok {
config.Pattern = p
}
regex, err := regexp.Compile("(?m:" + config.Pattern + ")")
if err != nil {
return nil, err
}
return &Linter{
LinterConfig: config,
regex: regex,
}, nil
}
func (l *Linter) String() string {
return l.Name
}
var predefinedPatterns = map[string]string{
"PATH:LINE:COL:MESSAGE": `^(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*)$`,
"PATH:LINE:MESSAGE": `^(?P<path>.*?\.go):(?P<line>\d+):\s*(?P<message>.*)$`,
}
func getLinterByName(name string, customSpec string) *Linter {
if customSpec != "" {
return parseLinterSpec(name, customSpec)
}
linter, _ := NewLinter(defaultLinters[name])
return linter
}
func parseLinterSpec(name string, spec string) *Linter {
parts := strings.SplitN(spec, ":", 2)
if len(parts) < 2 {
kingpin.Fatalf("invalid linter: %q", spec)
}
config := defaultLinters[name]
config.Command, config.Pattern = parts[0], parts[1]
linter, err := NewLinter(config)
kingpin.FatalIfError(err, "invalid linter %q", name)
return linter
}
func makeInstallCommand(linters ...string) []string {
cmd := []string{"get"}
if config.VendoredLinters {
cmd = []string{"install"}
} else {
if config.Update {
cmd = append(cmd, "-u")
}
if config.Force {
cmd = append(cmd, "-f")
}
if config.DownloadOnly {
cmd = append(cmd, "-d")
}
}
if config.Debug {
cmd = append(cmd, "-v")
}
cmd = append(cmd, linters...)
return cmd
}
func installLintersWithOneCommand(targets []string) error {
cmd := makeInstallCommand(targets...)
debug("go %s", strings.Join(cmd, " "))
c := exec.Command("go", cmd...) // nolint: gas
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return c.Run()
}
func installLintersIndividually(targets []string) {
failed := []string{}
for _, target := range targets {
cmd := makeInstallCommand(target)
debug("go %s", strings.Join(cmd, " "))
c := exec.Command("go", cmd...) // nolint: gas
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if err := c.Run(); err != nil {
warning("failed to install %s: %s", target, err)
failed = append(failed, target)
}
}
if len(failed) > 0 {
kingpin.Fatalf("failed to install the following linters: %s", strings.Join(failed, ", "))
}
}
func installLinters() {
names := make([]string, 0, len(defaultLinters))
targets := make([]string, 0, len(defaultLinters))
for name, config := range defaultLinters {
if config.InstallFrom == "" {
continue
}
names = append(names, name)
targets = append(targets, config.InstallFrom)
}
sort.Strings(names)
namesStr := strings.Join(names, "\n ")
if config.DownloadOnly {
fmt.Printf("Downloading:\n %s\n", namesStr)
} else {
fmt.Printf("Installing:\n %s\n", namesStr)
}
err := installLintersWithOneCommand(targets)
if err == nil {
return
}
warning("failed to install one or more linters: %s (installing individually)", err)
installLintersIndividually(targets)
}
func getDefaultLinters() []*Linter {
out := []*Linter{}
for _, config := range defaultLinters {
linter, err := NewLinter(config)
kingpin.FatalIfError(err, "invalid linter %q", config.Name)
out = append(out, linter)
}
return out
}
func defaultEnabled() []string {
enabled := []string{}
for name, config := range defaultLinters {
if config.defaultEnabled {
enabled = append(enabled, name)
}
}
return enabled
}
const vetPattern = `^(?:vet:.*?\.go:\s+(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*))|(?:(?P<path>.*?\.go):(?P<line>\d+):\s*(?P<message>.*))$`
var defaultLinters = map[string]LinterConfig{
"aligncheck": {
Name: "aligncheck",
Command: "aligncheck",
Pattern: `^(?:[^:]+: )?(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.+)$`,
InstallFrom: "github.com/opennota/check/cmd/aligncheck",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
defaultEnabled: true,
},
"deadcode": {
Name: "deadcode",
Command: "deadcode",
Pattern: `^deadcode: (?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*)$`,
InstallFrom: "github.com/tsenart/deadcode",
PartitionStrategy: partitionToMaxArgSize,
defaultEnabled: true,
},
"dupl": {
Name: "dupl",
Command: `dupl -plumbing -threshold {duplthreshold}`,
Pattern: `^(?P<path>.*?\.go):(?P<line>\d+)-\d+:\s*(?P<message>.*)$`,
InstallFrom: "github.com/mibk/dupl",
PartitionStrategy: partitionToMaxArgSizeWithFileGlobs,
IsFast: true,
},
"errcheck": {
Name: "errcheck",
Command: `errcheck -abspath`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "github.com/kisielk/errcheck",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
defaultEnabled: true,
},
"gas": {
Name: "gas",
Command: `gas -fmt=csv`,
Pattern: `^(?P<path>.*?\.go),(?P<line>\d+),(?P<message>[^,]+,[^,]+,[^,]+)`,
InstallFrom: "github.com/GoASTScanner/gas",
PartitionStrategy: partitionToMaxArgSize,
defaultEnabled: true,
IsFast: true,
},
"goconst": {
Name: "goconst",
Command: `goconst -min-occurrences {min_occurrences} -min-length {min_const_length}`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "github.com/jgautheron/goconst/cmd/goconst",
PartitionStrategy: partitionToMaxArgSize,
defaultEnabled: true,
IsFast: true,
},
"gocyclo": {
Name: "gocyclo",
Command: `gocyclo -over {mincyclo}`,
Pattern: `^(?P<cyclo>\d+)\s+\S+\s(?P<function>\S+)\s+(?P<path>.*?\.go):(?P<line>\d+):(\d+)$`,
InstallFrom: "github.com/alecthomas/gocyclo",
PartitionStrategy: partitionToMaxArgSize,
defaultEnabled: true,
IsFast: true,
},
"gofmt": {
Name: "gofmt",
Command: `gofmt -l -s`,
Pattern: `^(?P<path>.*?\.go)$`,
PartitionStrategy: partitionToMaxArgSizeWithFileGlobs,
IsFast: true,
},
"goimports": {
Name: "goimports",
Command: `goimports -l`,
Pattern: `^(?P<path>.*?\.go)$`,
InstallFrom: "golang.org/x/tools/cmd/goimports",
PartitionStrategy: partitionToMaxArgSizeWithFileGlobs,
IsFast: true,
},
"golint": {
Name: "golint",
Command: `golint -min_confidence {min_confidence}`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "github.com/golang/lint/golint",
PartitionStrategy: partitionToMaxArgSize,
defaultEnabled: true,
IsFast: true,
},
"gosimple": {
Name: "gosimple",
Command: `gosimple`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "honnef.co/go/tools/cmd/gosimple",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
},
"gotype": {
Name: "gotype",
Command: `gotype -e {tests=-t}`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "golang.org/x/tools/cmd/gotype",
PartitionStrategy: partitionToMaxArgSize,
defaultEnabled: true,
IsFast: true,
},
"ineffassign": {
Name: "ineffassign",
Command: `ineffassign -n`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "github.com/gordonklaus/ineffassign",
PartitionStrategy: partitionToMaxArgSize,
defaultEnabled: true,
IsFast: true,
},
"interfacer": {
Name: "interfacer",
Command: `interfacer`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "github.com/mvdan/interfacer/cmd/interfacer",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
defaultEnabled: true,
},
"lll": {
Name: "lll",
Command: `lll -g -l {maxlinelength}`,
Pattern: `PATH:LINE:MESSAGE`,
InstallFrom: "github.com/walle/lll/cmd/lll",
PartitionStrategy: partitionToMaxArgSizeWithFileGlobs,
IsFast: true,
},
"megacheck": {
Name: "megacheck",
Command: `megacheck`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "honnef.co/go/tools/cmd/megacheck",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
defaultEnabled: true,
},
"misspell": {
Name: "misspell",
Command: `misspell -j 1`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "github.com/client9/misspell/cmd/misspell",
PartitionStrategy: partitionToMaxArgSizeWithFileGlobs,
IsFast: true,
},
"safesql": {
Name: "safesql",
Command: `safesql`,
Pattern: `^- (?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+)$`,
InstallFrom: "github.com/stripe/safesql",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
},
"staticcheck": {
Name: "staticcheck",
Command: `staticcheck`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "honnef.co/go/tools/cmd/staticcheck",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
},
"structcheck": {
Name: "structcheck",
Command: `structcheck {tests=-t}`,
Pattern: `^(?:[^:]+: )?(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.+)$`,
InstallFrom: "github.com/opennota/check/cmd/structcheck",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
defaultEnabled: true,
},
"test": {
Name: "test",
Command: `go test`,
Pattern: `^--- FAIL: .*$\s+(?P<path>.*?\.go):(?P<line>\d+): (?P<message>.*)$`,
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
},
"testify": {
Name: "testify",
Command: `go test`,
Pattern: `Location:\s+(?P<path>.*?\.go):(?P<line>\d+)$\s+Error:\s+(?P<message>[^\n]+)`,
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
},
"unconvert": {
Name: "unconvert",
Command: `unconvert`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "github.com/mdempsky/unconvert",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
defaultEnabled: true,
},
"unparam": {
Name: "unparam",
Command: `unparam`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "github.com/mvdan/unparam",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
},
"unused": {
Name: "unused",
Command: `unused`,
Pattern: `PATH:LINE:COL:MESSAGE`,
InstallFrom: "honnef.co/go/tools/cmd/unused",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
},
"varcheck": {
Name: "varcheck",
Command: `varcheck`,
Pattern: `^(?:[^:]+: )?(?P<path>.*?\.go):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*)$`,
InstallFrom: "github.com/opennota/check/cmd/varcheck",
PartitionStrategy: partitionToMaxArgSizeWithPackagePaths,
defaultEnabled: true,
},
"vet": {
Name: "vet",
Command: `go tool vet`,
Pattern: vetPattern,
PartitionStrategy: partitionToPackageFileGlobs,
defaultEnabled: true,
IsFast: true,
},
"vetshadow": {
Name: "vetshadow",
Command: `go tool vet --shadow`,
Pattern: vetPattern,
PartitionStrategy: partitionToPackageFileGlobs,
defaultEnabled: true,
IsFast: true,
},
}

View File

@ -5,32 +5,18 @@ import (
"encoding/json"
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
"reflect"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"text/template"
"time"
"github.com/google/shlex"
"gopkg.in/alecthomas/kingpin.v3-unstable"
)
// Severity of linter message.
type Severity string
// Linter message severity levels.
const ( // nolint
Warning Severity = "warning"
Error Severity = "error"
)
var (
// Locations to look for vendored linters.
vendoredSearchPaths = [][]string{
@ -39,129 +25,43 @@ var (
}
)
type Linter struct {
Name string `json:"name"`
Command string `json:"command"`
CompositeCommand string `json:"composite_command,omitempty"`
Pattern string `json:"pattern"`
InstallFrom string `json:"install_from"`
SeverityOverride Severity `json:"severity,omitempty"`
MessageOverride string `json:"message_override,omitempty"`
regex *regexp.Regexp
}
func (l *Linter) MarshalJSON() ([]byte, error) {
return json.Marshal(l.Name)
}
func (l *Linter) String() string {
return l.Name
}
func LinterFromName(name string) *Linter {
s := linterDefinitions[name]
parts := strings.SplitN(s, ":", 2)
if len(parts) < 2 {
kingpin.Fatalf("invalid linter: %q", name)
}
pattern := parts[1]
if p, ok := predefinedPatterns[pattern]; ok {
pattern = p
}
re, err := regexp.Compile("(?m:" + pattern + ")")
kingpin.FatalIfError(err, "invalid regex for %q", name)
return &Linter{
Name: name,
Command: s[0:strings.Index(s, ":")],
Pattern: pattern,
InstallFrom: installMap[name],
SeverityOverride: Severity(config.Severity[name]),
MessageOverride: config.MessageOverride[name],
regex: re,
}
}
type sortedIssues struct {
issues []*Issue
order []string
}
func (s *sortedIssues) Len() int { return len(s.issues) }
func (s *sortedIssues) Swap(i, j int) { s.issues[i], s.issues[j] = s.issues[j], s.issues[i] }
// nolint: gocyclo
func (s *sortedIssues) Less(i, j int) bool {
l, r := s.issues[i], s.issues[j]
for _, key := range s.order {
switch key {
case "path":
if l.Path > r.Path {
return false
}
case "line":
if l.Line > r.Line {
return false
}
case "column":
if l.Col > r.Col {
return false
}
case "severity":
if l.Severity > r.Severity {
return false
}
case "message":
if l.Message > r.Message {
return false
}
case "linter":
if l.Linter.Name > r.Linter.Name {
return false
}
}
}
return true
}
func init() {
kingpin.Flag("config", "Load JSON configuration from file.").Action(loadConfig).String()
kingpin.Flag("disable", "Disable previously enabled linters.").PlaceHolder("LINTER").Short('D').Action(disableAction).Strings()
kingpin.Flag("enable", "Enable previously disabled linters.").PlaceHolder("LINTER").Short('E').Action(enableAction).Strings()
kingpin.Flag("linter", "Define a linter.").PlaceHolder("NAME:COMMAND:PATTERN").StringMapVar(&config.Linters)
kingpin.Flag("message-overrides", "Override message from linter. {message} will be expanded to the original message.").PlaceHolder("LINTER:MESSAGE").StringMapVar(&config.MessageOverride)
kingpin.Flag("severity", "Map of linter severities.").PlaceHolder("LINTER:SEVERITY").StringMapVar(&config.Severity)
kingpin.Flag("disable-all", "Disable all linters.").Action(disableAllAction).Bool()
kingpin.Flag("enable-all", "Enable all linters.").Action(enableAllAction).Bool()
kingpin.Flag("format", "Output format.").PlaceHolder(config.Format).StringVar(&config.Format)
kingpin.Flag("vendored-linters", "Use vendored linters (recommended).").BoolVar(&config.VendoredLinters)
kingpin.Flag("fast", "Only run fast linters.").BoolVar(&config.Fast)
kingpin.Flag("install", "Attempt to install all known linters.").Short('i').BoolVar(&config.Install)
kingpin.Flag("update", "Pass -u to go tool when installing.").Short('u').BoolVar(&config.Update)
kingpin.Flag("force", "Pass -f to go tool when installing.").Short('f').BoolVar(&config.Force)
kingpin.Flag("download-only", "Pass -d to go tool when installing.").BoolVar(&config.DownloadOnly)
kingpin.Flag("debug", "Display messages for failed linters, etc.").Short('d').BoolVar(&config.Debug)
kingpin.Flag("concurrency", "Number of concurrent linters to run.").PlaceHolder(fmt.Sprintf("%d", runtime.NumCPU())).Short('j').IntVar(&config.Concurrency)
kingpin.Flag("exclude", "Exclude messages matching these regular expressions.").Short('e').PlaceHolder("REGEXP").StringsVar(&config.Exclude)
kingpin.Flag("include", "Include messages matching these regular expressions.").Short('I').PlaceHolder("REGEXP").StringsVar(&config.Include)
kingpin.Flag("skip", "Skip directories with this name when expanding '...'.").Short('s').PlaceHolder("DIR...").StringsVar(&config.Skip)
kingpin.Flag("vendor", "Enable vendoring support (skips 'vendor' directories and sets GO15VENDOREXPERIMENT=1).").BoolVar(&config.Vendor)
kingpin.Flag("cyclo-over", "Report functions with cyclomatic complexity over N (using gocyclo).").PlaceHolder("10").IntVar(&config.Cyclo)
kingpin.Flag("line-length", "Report lines longer than N (using lll).").PlaceHolder("80").IntVar(&config.LineLength)
kingpin.Flag("min-confidence", "Minimum confidence interval to pass to golint.").PlaceHolder(".80").FloatVar(&config.MinConfidence)
kingpin.Flag("min-occurrences", "Minimum occurrences to pass to goconst.").PlaceHolder("3").IntVar(&config.MinOccurrences)
kingpin.Flag("min-const-length", "Minimumum constant length.").PlaceHolder("3").IntVar(&config.MinConstLength)
kingpin.Flag("dupl-threshold", "Minimum token sequence as a clone for dupl.").PlaceHolder("50").IntVar(&config.DuplThreshold)
kingpin.Flag("sort", fmt.Sprintf("Sort output by any of %s.", strings.Join(sortKeys, ", "))).PlaceHolder("none").EnumsVar(&config.Sort, sortKeys...)
kingpin.Flag("tests", "Include test files for linters that support this option").Short('t').BoolVar(&config.Test)
kingpin.Flag("deadline", "Cancel linters if they have not completed within this duration.").PlaceHolder("30s").DurationVar(&config.Deadline)
kingpin.Flag("errors", "Only show errors.").BoolVar(&config.Errors)
kingpin.Flag("json", "Generate structured JSON rather than standard line-based output.").BoolVar(&config.JSON)
kingpin.Flag("checkstyle", "Generate checkstyle XML rather than standard line-based output.").BoolVar(&config.Checkstyle)
kingpin.Flag("enable-gc", "Enable GC for linters (useful on large repositories).").BoolVar(&config.EnableGC)
kingpin.Flag("aggregate", "Aggregate issues reported by several linters.").BoolVar(&config.Aggregate)
kingpin.CommandLine.GetFlag("help").Short('h')
func setupFlags(app *kingpin.Application) {
app.Flag("config", "Load JSON configuration from file.").Action(loadConfig).String()
app.Flag("disable", "Disable previously enabled linters.").PlaceHolder("LINTER").Short('D').Action(disableAction).Strings()
app.Flag("enable", "Enable previously disabled linters.").PlaceHolder("LINTER").Short('E').Action(enableAction).Strings()
app.Flag("linter", "Define a linter.").PlaceHolder("NAME:COMMAND:PATTERN").StringMapVar(&config.Linters)
app.Flag("message-overrides", "Override message from linter. {message} will be expanded to the original message.").PlaceHolder("LINTER:MESSAGE").StringMapVar(&config.MessageOverride)
app.Flag("severity", "Map of linter severities.").PlaceHolder("LINTER:SEVERITY").StringMapVar(&config.Severity)
app.Flag("disable-all", "Disable all linters.").Action(disableAllAction).Bool()
app.Flag("enable-all", "Enable all linters.").Action(enableAllAction).Bool()
app.Flag("format", "Output format.").PlaceHolder(config.Format).StringVar(&config.Format)
app.Flag("vendored-linters", "Use vendored linters (recommended).").BoolVar(&config.VendoredLinters)
app.Flag("fast", "Only run fast linters.").BoolVar(&config.Fast)
app.Flag("install", "Attempt to install all known linters.").Short('i').BoolVar(&config.Install)
app.Flag("update", "Pass -u to go tool when installing.").Short('u').BoolVar(&config.Update)
app.Flag("force", "Pass -f to go tool when installing.").Short('f').BoolVar(&config.Force)
app.Flag("download-only", "Pass -d to go tool when installing.").BoolVar(&config.DownloadOnly)
app.Flag("debug", "Display messages for failed linters, etc.").Short('d').BoolVar(&config.Debug)
app.Flag("concurrency", "Number of concurrent linters to run.").PlaceHolder(fmt.Sprintf("%d", runtime.NumCPU())).Short('j').IntVar(&config.Concurrency)
app.Flag("exclude", "Exclude messages matching these regular expressions.").Short('e').PlaceHolder("REGEXP").StringsVar(&config.Exclude)
app.Flag("include", "Include messages matching these regular expressions.").Short('I').PlaceHolder("REGEXP").StringsVar(&config.Include)
app.Flag("skip", "Skip directories with this name when expanding '...'.").Short('s').PlaceHolder("DIR...").StringsVar(&config.Skip)
app.Flag("vendor", "Enable vendoring support (skips 'vendor' directories and sets GO15VENDOREXPERIMENT=1).").BoolVar(&config.Vendor)
app.Flag("cyclo-over", "Report functions with cyclomatic complexity over N (using gocyclo).").PlaceHolder("10").IntVar(&config.Cyclo)
app.Flag("line-length", "Report lines longer than N (using lll).").PlaceHolder("80").IntVar(&config.LineLength)
app.Flag("min-confidence", "Minimum confidence interval to pass to golint.").PlaceHolder(".80").FloatVar(&config.MinConfidence)
app.Flag("min-occurrences", "Minimum occurrences to pass to goconst.").PlaceHolder("3").IntVar(&config.MinOccurrences)
app.Flag("min-const-length", "Minimumum constant length.").PlaceHolder("3").IntVar(&config.MinConstLength)
app.Flag("dupl-threshold", "Minimum token sequence as a clone for dupl.").PlaceHolder("50").IntVar(&config.DuplThreshold)
app.Flag("sort", fmt.Sprintf("Sort output by any of %s.", strings.Join(sortKeys, ", "))).PlaceHolder("none").EnumsVar(&config.Sort, sortKeys...)
app.Flag("tests", "Include test files for linters that support this option").Short('t').BoolVar(&config.Test)
app.Flag("deadline", "Cancel linters if they have not completed within this duration.").PlaceHolder("30s").DurationVar((*time.Duration)(&config.Deadline))
app.Flag("errors", "Only show errors.").BoolVar(&config.Errors)
app.Flag("json", "Generate structured JSON rather than standard line-based output.").BoolVar(&config.JSON)
app.Flag("checkstyle", "Generate checkstyle XML rather than standard line-based output.").BoolVar(&config.Checkstyle)
app.Flag("enable-gc", "Enable GC for linters (useful on large repositories).").BoolVar(&config.EnableGC)
app.Flag("aggregate", "Aggregate issues reported by several linters.").BoolVar(&config.Aggregate)
app.GetFlag("help").Short('h')
}
func loadConfig(app *kingpin.Application, element *kingpin.ParseElement, ctx *kingpin.ParseContext) error {
@ -174,9 +74,6 @@ func loadConfig(app *kingpin.Application, element *kingpin.ParseElement, ctx *ki
if err != nil {
return err
}
if config.DeadlineJSONCrutch != "" {
config.Deadline, err = time.ParseDuration(config.DeadlineJSONCrutch)
}
for _, disable := range config.Disable {
for i, enable := range config.Enable {
if enable == disable {
@ -210,28 +107,13 @@ func disableAllAction(app *kingpin.Application, element *kingpin.ParseElement, c
}
func enableAllAction(app *kingpin.Application, element *kingpin.ParseElement, ctx *kingpin.ParseContext) error {
for linter := range linterDefinitions {
for linter := range defaultLinters {
config.Enable = append(config.Enable, linter)
}
config.EnableAll = true
return nil
}
type Issue struct {
Linter *Linter `json:"linter"`
Severity Severity `json:"severity"`
Path string `json:"path"`
Line int `json:"line"`
Col int `json:"col"`
Message string `json:"message"`
}
func (i *Issue) String() string {
buf := new(bytes.Buffer)
err := formatTemplate.Execute(buf, i)
kingpin.FatalIfError(err, "Invalid output format")
return buf.String()
}
func debug(format string, args ...interface{}) {
if config.Debug {
fmt.Fprintf(os.Stderr, "DEBUG: "+format+"\n", args...)
@ -244,13 +126,13 @@ func warning(format string, args ...interface{}) {
func formatLinters() string {
w := bytes.NewBuffer(nil)
for name := range linterDefinitions {
linter := LinterFromName(name)
for _, linter := range getDefaultLinters() {
install := "(" + linter.InstallFrom + ")"
if install == "()" {
install = ""
}
fmt.Fprintf(w, " %s %s\n %s\n %s\n", name, install, linter.Command, linter.Pattern)
fmt.Fprintf(w, " %s %s\n %s\n %s\n",
linter.Name, install, linter.Command, linter.Pattern)
}
return w.String()
}
@ -263,33 +145,11 @@ func formatSeverity() string {
return w.String()
}
type Vars map[string]string
func (v Vars) Copy() Vars {
out := Vars{}
for k, v := range v {
out[k] = v
}
return out
}
func (v Vars) Replace(s string) string {
for k, v := range v {
prefix := regexp.MustCompile(fmt.Sprintf("{%s=([^}]*)}", k))
if v != "" {
s = prefix.ReplaceAllString(s, "$1")
} else {
s = prefix.ReplaceAllString(s, "")
}
s = strings.Replace(s, fmt.Sprintf("{%s}", k), v, -1)
}
return s
}
func main() {
// Linters are by their very nature, short lived, so disable GC.
// Reduced (user) linting time on kingpin from 0.97s to 0.64s.
kingpin.CommandLine.Help = fmt.Sprintf(`Aggregate and normalise the output of a whole bunch of Go linters.
pathsArg := kingpin.Arg("path", "Directories to lint. Defaults to \".\". <path>/... will recurse.").Strings()
app := kingpin.CommandLine
setupFlags(app)
app.Help = fmt.Sprintf(`Aggregate and normalise the output of a whole bunch of Go linters.
PlaceHolder linters:
@ -301,21 +161,23 @@ Severity override map (default is "warning"):
`, formatLinters(), formatSeverity())
kingpin.Parse()
configureEnvironment()
if config.Install {
if config.VendoredLinters {
configureEnvironmentForInstall()
}
installLinters()
return
}
configureEnvironment()
include, exclude := processConfig(config)
start := time.Now()
paths := expandPaths(*pathsArg, config.Skip)
paths := resolvePaths(*pathsArg, config.Skip)
linters := lintersFromFlags()
linters := lintersFromConfig(config)
issues, errch := runLinters(linters, paths, config.Concurrency, exclude, include)
status := 0
issues, errch := runLinters(linters, paths, *pathsArg, config.Concurrency, exclude, include)
if config.JSON {
status |= outputToJSON(issues)
} else if config.Checkstyle {
@ -334,14 +196,12 @@ Severity override map (default is "warning"):
// nolint: gocyclo
func processConfig(config *Config) (include *regexp.Regexp, exclude *regexp.Regexp) {
// Move configured linters into linterDefinitions.
for name, definition := range config.Linters {
linterDefinitions[name] = definition
}
tmpl, err := template.New("output").Parse(config.Format)
kingpin.FatalIfError(err, "invalid format %q", config.Format)
formatTemplate = tmpl
// Linters are by their very nature, short lived, so disable GC.
// Reduced (user) linting time on kingpin from 0.97s to 0.64s.
if !config.EnableGC {
_ = os.Setenv("GOGC", "off")
}
@ -413,74 +273,13 @@ func outputToJSON(issues chan *Issue) int {
return status
}
func runLinters(linters map[string]*Linter, paths, ellipsisPaths []string, concurrency int, exclude *regexp.Regexp, include *regexp.Regexp) (chan *Issue, chan error) {
errch := make(chan error, len(linters)*(len(paths)+len(ellipsisPaths)))
concurrencych := make(chan bool, config.Concurrency)
incomingIssues := make(chan *Issue, 1000000)
directives := newDirectiveParser(paths)
processedIssues := filterIssuesViaDirectives(directives, maybeSortIssues(maybeAggregateIssues(incomingIssues)))
wg := &sync.WaitGroup{}
for _, linter := range linters {
// Recreated in each loop because it is mutated by executeLinter().
vars := Vars{
"duplthreshold": fmt.Sprintf("%d", config.DuplThreshold),
"mincyclo": fmt.Sprintf("%d", config.Cyclo),
"maxlinelength": fmt.Sprintf("%d", config.LineLength),
"min_confidence": fmt.Sprintf("%f", config.MinConfidence),
"min_occurrences": fmt.Sprintf("%d", config.MinOccurrences),
"min_const_length": fmt.Sprintf("%d", config.MinConstLength),
"tests": "",
}
if config.Test {
vars["tests"] = "-t"
}
linterPaths := paths
// Most linters don't exclude vendor paths when recursing, so we don't use ... paths.
if acceptsEllipsis[linter.Name] && !config.Vendor && len(ellipsisPaths) > 0 {
linterPaths = ellipsisPaths
}
for _, path := range linterPaths {
wg.Add(1)
deadline := time.After(config.Deadline)
state := &linterState{
Linter: linter,
issues: incomingIssues,
path: path,
vars: vars.Copy(),
exclude: exclude,
include: include,
deadline: deadline,
}
go func() {
concurrencych <- true
err := executeLinter(state)
if err != nil {
errch <- err
}
<-concurrencych
wg.Done()
}()
}
}
go func() {
wg.Wait()
close(incomingIssues)
close(errch)
}()
return processedIssues, errch
}
// nolint: gocyclo
func expandPaths(paths, skip []string) []string {
func resolvePaths(paths, skip []string) []string {
if len(paths) == 0 {
paths = []string{"."}
return []string{"."}
}
skipMap := map[string]bool{}
for _, name := range skip {
skipMap[name] = true
}
dirs := map[string]bool{}
skipPath := newPathFilter(skip)
dirs := newStringSet()
for _, path := range paths {
if strings.HasSuffix(path, "/...") {
root := filepath.Dir(path)
@ -490,24 +289,22 @@ func expandPaths(paths, skip []string) []string {
return err
}
base := filepath.Base(p)
skip := skipMap[base] || skipMap[p] || (strings.ContainsAny(base[0:1], "_.") && base != "." && base != "..")
if i.IsDir() {
if skip {
return filepath.SkipDir
}
} else if !skip && strings.HasSuffix(p, ".go") {
dirs[filepath.Clean(filepath.Dir(p))] = true
skip := skipPath(p)
switch {
case i.IsDir() && skip:
return filepath.SkipDir
case !i.IsDir() && !skip && strings.HasSuffix(p, ".go"):
dirs.add(filepath.Clean(filepath.Dir(p)))
}
return nil
})
} else {
dirs[filepath.Clean(path)] = true
dirs.add(filepath.Clean(path))
}
}
out := make([]string, 0, len(dirs))
for d := range dirs {
out = append(out, d)
out := make([]string, 0, dirs.size())
for _, d := range dirs.asSlice() {
out = append(out, relativePackagePath(d))
}
sort.Strings(out)
for _, d := range out {
@ -516,313 +313,82 @@ func expandPaths(paths, skip []string) []string {
return out
}
func makeInstallCommand(linters ...string) []string {
cmd := []string{"get"}
if config.VendoredLinters {
cmd = []string{"install"}
} else {
if config.Update {
cmd = append(cmd, "-u")
func newPathFilter(skip []string) func(string) bool {
filter := map[string]bool{}
for _, name := range skip {
filter[name] = true
}
return func(path string) bool {
base := filepath.Base(path)
if filter[base] || filter[path] {
return true
}
if config.Force {
cmd = append(cmd, "-f")
}
if config.DownloadOnly {
cmd = append(cmd, "-d")
}
}
if config.Debug {
cmd = append(cmd, "-v")
}
cmd = append(cmd, linters...)
return cmd
}
func installLintersWithOneCommand(targets []string) error {
cmd := makeInstallCommand(targets...)
debug("go %s", strings.Join(cmd, " "))
c := exec.Command("go", cmd...) // nolint: gas
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return c.Run()
}
func installLintersIndividually(targets []string) {
failed := []string{}
for _, target := range targets {
cmd := makeInstallCommand(target)
debug("go %s", strings.Join(cmd, " "))
c := exec.Command("go", cmd...) // nolint: gas
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if err := c.Run(); err != nil {
warning("failed to install %s: %s", target, err)
failed = append(failed, target)
}
}
if len(failed) > 0 {
kingpin.Fatalf("failed to install the following linters: %s", strings.Join(failed, ", "))
return base != "." && base != ".." && strings.ContainsAny(base[0:1], "_.")
}
}
func installLinters() {
names := make([]string, 0, len(installMap))
targets := make([]string, 0, len(installMap))
for name, target := range installMap {
names = append(names, name)
targets = append(targets, target)
func relativePackagePath(dir string) string {
if filepath.IsAbs(dir) || strings.HasPrefix(dir, ".") {
return dir
}
namesStr := strings.Join(names, "\n ")
if config.DownloadOnly {
fmt.Printf("Downloading:\n %s\n", namesStr)
} else {
fmt.Printf("Installing:\n %s\n", namesStr)
}
err := installLintersWithOneCommand(targets)
if err == nil {
return
}
warning("failed to install one or more linters: %s (installing individually)", err)
installLintersIndividually(targets)
// package names must start with a ./
return "./" + dir
}
func maybeAggregateIssues(issues chan *Issue) chan *Issue {
if !config.Aggregate {
return issues
}
return aggregateIssues(issues)
}
func maybeSortIssues(issues chan *Issue) chan *Issue {
if reflect.DeepEqual([]string{"none"}, config.Sort) {
return issues
}
out := make(chan *Issue, 1000000)
sorted := &sortedIssues{
issues: []*Issue{},
order: config.Sort,
}
go func() {
for issue := range issues {
sorted.issues = append(sorted.issues, issue)
}
sort.Sort(sorted)
for _, issue := range sorted.issues {
out <- issue
}
close(out)
}()
return out
}
type linterState struct {
*Linter
path string
issues chan *Issue
vars Vars
exclude *regexp.Regexp
include *regexp.Regexp
deadline <-chan time.Time
}
func (l *linterState) InterpolatedCommand() string {
vars := l.vars.Copy()
if l.ShouldChdir() {
vars["path"] = "."
} else {
vars["path"] = l.path
}
return vars.Replace(l.Command)
}
func (l *linterState) ShouldChdir() bool {
return config.Vendor || !strings.HasSuffix(l.path, "/...") || !strings.Contains(l.Command, "{path}")
}
func parseCommand(dir, command string) (string, []string, error) {
args, err := shlex.Split(command)
if err != nil {
return "", nil, err
}
if len(args) == 0 {
return "", nil, fmt.Errorf("invalid command %q", command)
}
exe, err := exec.LookPath(args[0])
if err != nil {
return "", nil, err
}
out := []string{}
for _, arg := range args[1:] {
if strings.Contains(arg, "*") {
pattern := filepath.Join(dir, arg)
globbed, err := filepath.Glob(pattern)
if err != nil {
return "", nil, err
}
for i, g := range globbed {
if strings.HasPrefix(g, dir+string(filepath.Separator)) {
globbed[i] = g[len(dir)+1:]
}
}
out = append(out, globbed...)
} else {
out = append(out, arg)
}
}
return exe, out, nil
}
func executeLinter(state *linterState) error {
debug("linting with %s: %s (on %s)", state.Name, state.Command, state.path)
start := time.Now()
command := state.InterpolatedCommand()
exe, args, err := parseCommand(state.path, command)
if err != nil {
return err
}
debug("executing %s %q", exe, args)
buf := bytes.NewBuffer(nil)
cmd := exec.Command(exe, args...) // nolint: gas
if state.ShouldChdir() {
cmd.Dir = state.path
}
cmd.Stdout = buf
cmd.Stderr = buf
err = cmd.Start()
if err != nil {
return fmt.Errorf("failed to execute linter %s: %s", command, err)
}
done := make(chan bool)
go func() {
err = cmd.Wait()
done <- true
}()
// Wait for process to complete or deadline to expire.
select {
case <-done:
case <-state.deadline:
err = fmt.Errorf("deadline exceeded by linter %s on %s (try increasing --deadline)",
state.Name, state.path)
kerr := cmd.Process.Kill()
if kerr != nil {
warning("failed to kill %s: %s", state.Name, kerr)
}
return err
}
if err != nil {
debug("warning: %s returned %s", command, err)
}
processOutput(state, buf.Bytes())
elapsed := time.Since(start)
debug("%s linter took %s", state.Name, elapsed)
return nil
}
func (l *linterState) fixPath(path string) string {
lpath := strings.TrimSuffix(l.path, "...")
labspath, _ := filepath.Abs(lpath)
if !l.ShouldChdir() {
path = strings.TrimPrefix(path, lpath)
}
if !filepath.IsAbs(path) {
path, _ = filepath.Abs(filepath.Join(labspath, path))
}
if strings.HasPrefix(path, labspath) {
return filepath.Join(lpath, strings.TrimPrefix(path, labspath))
}
return path
}
func lintersFromFlags() map[string]*Linter {
func lintersFromConfig(config *Config) map[string]*Linter {
out := map[string]*Linter{}
for _, linter := range config.Enable {
out[linter] = LinterFromName(linter)
config.Enable = replaceWithMegacheck(config.Enable, config.EnableAll)
for _, name := range config.Enable {
linter := getLinterByName(name, config.Linters[name])
if config.Fast && !linter.IsFast {
continue
}
out[name] = linter
}
for _, linter := range config.Disable {
delete(out, linter)
}
if config.Fast {
for _, linter := range slowLinters {
delete(out, linter)
}
}
return out
}
// nolint: gocyclo
func processOutput(state *linterState, out []byte) {
re := state.regex
all := re.FindAllSubmatchIndex(out, -1)
debug("%s hits %d: %s", state.Name, len(all), state.Pattern)
for _, indices := range all {
group := [][]byte{}
for i := 0; i < len(indices); i += 2 {
var fragment []byte
if indices[i] != -1 {
fragment = out[indices[i]:indices[i+1]]
}
group = append(group, fragment)
// replaceWithMegacheck checks enabled linters if they duplicate megacheck and
// returns a either a revised list removing those and adding megacheck or an
// unchanged slice. Emits a warning if linters were removed and swapped with
// megacheck.
func replaceWithMegacheck(enabled []string, enableAll bool) []string {
var (
staticcheck,
gosimple,
unused bool
revised []string
)
for _, linter := range enabled {
switch linter {
case "staticcheck":
staticcheck = true
case "gosimple":
gosimple = true
case "unused":
unused = true
case "megacheck":
// Don't add to revised slice, we'll add it later
default:
revised = append(revised, linter)
}
issue := &Issue{Line: 1}
issue.Linter = LinterFromName(state.Name)
for i, name := range re.SubexpNames() {
if group[i] == nil {
continue
}
part := string(group[i])
if name != "" {
state.vars[name] = part
}
switch name {
case "path":
issue.Path = state.fixPath(part)
case "line":
n, err := strconv.ParseInt(part, 10, 32)
kingpin.FatalIfError(err, "line matched invalid integer")
issue.Line = int(n)
case "col":
n, err := strconv.ParseInt(part, 10, 32)
kingpin.FatalIfError(err, "col matched invalid integer")
issue.Col = int(n)
case "message":
issue.Message = part
case "":
}
}
if m, ok := config.MessageOverride[state.Name]; ok {
issue.Message = state.vars.Replace(m)
}
if sev, ok := config.Severity[state.Name]; ok {
issue.Severity = Severity(sev)
} else {
issue.Severity = "warning"
}
if state.exclude != nil && state.exclude.MatchString(issue.String()) {
continue
}
if state.include != nil && !state.include.MatchString(issue.String()) {
continue
}
state.issues <- issue
}
return
if staticcheck && gosimple && unused {
if !enableAll {
warning("staticcheck, gosimple and unused are all set, using megacheck instead")
}
return append(revised, "megacheck")
}
return enabled
}
func findVendoredLinters() string {
gopaths := strings.Split(getGoPath(), string(os.PathListSeparator))
gopaths := getGoPathList()
for _, home := range vendoredSearchPaths {
for _, p := range gopaths {
joined := append([]string{p, "src"}, home...)
@ -833,7 +399,6 @@ func findVendoredLinters() string {
}
}
return ""
}
// Go 1.8 compatible GOPATH.
@ -847,62 +412,70 @@ func getGoPath() string {
return path
}
// addPath appends p to paths and returns it if:
// 1. p is not a blank string
// 2. p doesn't already exist in paths
// Otherwise paths is returned unchanged.
func addPath(p string, paths []string) []string {
if p == "" {
return paths
}
for _, path := range paths {
if p == path {
func getGoPathList() []string {
return strings.Split(getGoPath(), string(os.PathListSeparator))
}
// addPath appends path to paths if path does not already exist in paths. Returns
// the new paths.
func addPath(paths []string, path string) []string {
for _, existingpath := range paths {
if path == existingpath {
return paths
}
}
return append(paths, p)
return append(paths, path)
}
// Ensure all "bin" directories from GOPATH exists in PATH, as well as GOBIN if set.
// configureEnvironment adds all `bin/` directories from $GOPATH to $PATH
func configureEnvironment() {
gopaths := strings.Split(getGoPath(), string(os.PathListSeparator))
paths := addGoBinsToPath(getGoPathList())
setEnv("PATH", strings.Join(paths, string(os.PathListSeparator)))
debugPrintEnv()
}
func addGoBinsToPath(gopaths []string) []string {
paths := strings.Split(os.Getenv("PATH"), string(os.PathListSeparator))
gobin := os.Getenv("GOBIN")
if config.VendoredLinters && config.Install {
vendorRoot := findVendoredLinters()
if vendorRoot == "" {
kingpin.Fatalf("could not find vendored linters in GOPATH=%q", getGoPath())
}
debug("found vendored linters at %s, updating environment", vendorRoot)
if gobin == "" {
gobin = filepath.Join(gopaths[0], "bin")
}
// "go install" panics when one GOPATH element is beneath another, so we just set
// our vendor root instead.
gopaths = []string{vendorRoot}
}
for _, p := range gopaths {
paths = addPath(filepath.Join(p, "bin"), paths)
paths = addPath(paths, filepath.Join(p, "bin"))
}
paths = addPath(gobin, paths)
path := strings.Join(paths, string(os.PathListSeparator))
gopath := strings.Join(gopaths, string(os.PathListSeparator))
if err := os.Setenv("PATH", path); err != nil {
warning("setenv PATH: %s", err)
gobin := os.Getenv("GOBIN")
if gobin != "" {
paths = addPath(paths, gobin)
}
return paths
}
// configureEnvironmentForInstall sets GOPATH and GOBIN so that vendored linters
// can be installed
func configureEnvironmentForInstall() {
gopaths := getGoPathList()
vendorRoot := findVendoredLinters()
if vendorRoot == "" {
kingpin.Fatalf("could not find vendored linters in GOPATH=%q", getGoPath())
}
debug("found vendored linters at %s, updating environment", vendorRoot)
gobin := os.Getenv("GOBIN")
if gobin == "" {
gobin = filepath.Join(gopaths[0], "bin")
}
setEnv("GOBIN", gobin)
// "go install" panics when one GOPATH element is beneath another, so set
// GOPATH to the vendor root
setEnv("GOPATH", vendorRoot)
debugPrintEnv()
}
func setEnv(key string, value string) {
if err := os.Setenv(key, value); err != nil {
warning("setenv %s: %s", key, err)
}
}
func debugPrintEnv() {
debug("PATH=%s", os.Getenv("PATH"))
if err := os.Setenv("GOPATH", gopath); err != nil {
warning("setenv GOPATH: %s", err)
}
debug("GOPATH=%s", os.Getenv("GOPATH"))
if err := os.Setenv("GOBIN", gobin); err != nil {
warning("setenv GOBIN: %s", err)
}
debug("GOBIN=%s", os.Getenv("GOBIN"))
}

View File

@ -0,0 +1,131 @@
package main
import (
"fmt"
"path/filepath"
)
// MaxCommandBytes is the maximum number of bytes used when executing a command
const MaxCommandBytes = 32000
type partitionStrategy func([]string, []string) ([][]string, error)
func pathsToFileGlobs(paths []string) ([]string, error) {
filePaths := []string{}
for _, dir := range paths {
paths, err := filepath.Glob(filepath.Join(dir, "*.go"))
if err != nil {
return nil, err
}
filePaths = append(filePaths, paths...)
}
return filePaths, nil
}
func partitionToMaxArgSize(cmdArgs []string, paths []string) ([][]string, error) {
return partitionToMaxSize(cmdArgs, paths, MaxCommandBytes), nil
}
func partitionToMaxSize(cmdArgs []string, paths []string, maxSize int) [][]string {
partitions := newSizePartitioner(cmdArgs, maxSize)
for _, path := range paths {
partitions.add(path)
}
return partitions.end()
}
type sizePartitioner struct {
base []string
parts [][]string
current []string
size int
max int
}
func newSizePartitioner(base []string, max int) *sizePartitioner {
p := &sizePartitioner{base: base, max: max}
p.new()
return p
}
func (p *sizePartitioner) add(arg string) {
if p.size+len(arg)+1 > p.max {
p.new()
}
p.current = append(p.current, arg)
p.size += len(arg) + 1
}
func (p *sizePartitioner) new() {
p.end()
p.size = 0
p.current = []string{}
for _, arg := range p.base {
p.add(arg)
}
}
func (p *sizePartitioner) end() [][]string {
if len(p.current) > 0 {
p.parts = append(p.parts, p.current)
}
return p.parts
}
func partitionToMaxArgSizeWithFileGlobs(cmdArgs []string, paths []string) ([][]string, error) {
filePaths, err := pathsToFileGlobs(paths)
if err != nil || len(filePaths) == 0 {
return nil, err
}
return partitionToMaxArgSize(cmdArgs, filePaths)
}
func partitionToPackageFileGlobs(cmdArgs []string, paths []string) ([][]string, error) {
parts := [][]string{}
for _, path := range paths {
filePaths, err := pathsToFileGlobs([]string{path})
if err != nil {
return nil, err
}
if len(filePaths) == 0 {
continue
}
parts = append(parts, append(cmdArgs, filePaths...))
}
return parts, nil
}
func partitionToMaxArgSizeWithPackagePaths(cmdArgs []string, paths []string) ([][]string, error) {
packagePaths, err := pathsToPackagePaths(paths)
if err != nil || len(packagePaths) == 0 {
return nil, err
}
return partitionToMaxArgSize(cmdArgs, packagePaths)
}
func pathsToPackagePaths(paths []string) ([]string, error) {
packages := []string{}
for _, path := range paths {
pkg, err := packageNameFromPath(path)
if err != nil {
return nil, err
}
packages = append(packages, pkg)
}
return packages, nil
}
func packageNameFromPath(path string) (string, error) {
if !filepath.IsAbs(path) {
return path, nil
}
for _, gopath := range getGoPathList() {
rel, err := filepath.Rel(filepath.Join(gopath, "src"), path)
if err != nil {
continue
}
return rel, nil
}
return "", fmt.Errorf("%s not in GOPATH", path)
}

View File

@ -0,0 +1,29 @@
package main
type stringSet struct {
items map[string]struct{}
}
func newStringSet(items ...string) *stringSet {
setItems := make(map[string]struct{}, len(items))
for _, item := range items {
setItems[item] = struct{}{}
}
return &stringSet{items: setItems}
}
func (s *stringSet) add(item string) {
s.items[item] = struct{}{}
}
func (s *stringSet) asSlice() []string {
items := []string{}
for item := range s.items {
items = append(items, item)
}
return items
}
func (s *stringSet) size() int {
return len(s.items)
}

View File

@ -1,2 +1,4 @@
# ineffassign
Detect ineffectual assignments in Go code.
This tool misses some cases because does not consider any type information in its analysis. (For example, assignments to struct fields are never marked as ineffectual.) It should, however, never give any false positives.

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"os"
"strconv"
@ -16,7 +17,7 @@ const usageDoc = `goconst: find repeated strings that could be replaced by a con
Usage:
goconst ARGS <directory>
goconst ARGS <directory> [<directory>...]
Flags:
@ -26,8 +27,8 @@ Flags:
-min-length only report strings with the minimum given length (default: 3)
-match-constant look for existing constants matching the strings
-numbers search also for duplicated numbers
-min minimum value, only works with -numbers
-max maximum value, only works with -numbers
-min minimum value, only works with -numbers
-max maximum value, only works with -numbers
-output output formatting (text or json)
Examples:
@ -52,17 +53,25 @@ var (
func main() {
flag.Usage = func() {
fmt.Fprint(os.Stderr, usage)
usage(os.Stderr)
}
flag.Parse()
log.SetPrefix("goconst: ")
args := flag.Args()
if len(args) != 1 {
usage()
if len(args) < 1 {
usage(os.Stderr)
os.Exit(1)
}
path := args[0]
for _, path := range args {
if err := run(path); err != nil {
log.Println(err)
os.Exit(1)
}
}
}
func run(path string) error {
gco := goconst.New(
path,
*flagIgnore,
@ -73,19 +82,17 @@ func main() {
)
strs, consts, err := gco.ParseTree()
if err != nil {
log.Println(err)
os.Exit(1)
return err
}
printOutput(strs, consts, *flagOutput, *flagMinOccurrences, *flagMin, *flagMax)
return printOutput(strs, consts, *flagOutput, *flagMinOccurrences, *flagMin, *flagMax)
}
func usage() {
fmt.Fprintf(os.Stderr, usageDoc)
os.Exit(1)
func usage(out io.Writer) {
fmt.Fprintf(out, usageDoc)
}
func printOutput(strs goconst.Strings, consts goconst.Constants, output string, minOccurrences, min, max int) {
func printOutput(strs goconst.Strings, consts goconst.Constants, output string, minOccurrences, min, max int) error {
for str, item := range strs {
// Filter out items whose occurrences don't match the min value
if len(item) < minOccurrences {
@ -113,7 +120,7 @@ func printOutput(strs goconst.Strings, consts goconst.Constants, output string,
strs, consts,
})
if err != nil {
log.Fatal(err)
return err
}
case "text":
for str, item := range strs {
@ -140,8 +147,9 @@ func printOutput(strs goconst.Strings, consts goconst.Constants, output string,
}
}
default:
fmt.Printf(`Unsupported output format: %s`, output)
return fmt.Errorf(`Unsupported output format: %s`, output)
}
return nil
}
func occurrences(item []goconst.ExtendedPos, current goconst.ExtendedPos) string {

View File

@ -6,6 +6,7 @@
package main
import (
"bytes"
_ "crypto/sha512"
"encoding/json"
"errors"
@ -43,7 +44,7 @@ func (a *Flags) Set(value string) error {
var (
extraFlags Flags
pkg = flag.String("package", "", "Go package")
verbose = flag.Bool("v", false, "Pass '-v' argument to 'go test'")
verbose = flag.Bool("v", false, "Pass '-v' argument to 'go test' and output to stdout")
debug = flag.Bool("debug", false, "Enable debug output")
coverprof = flag.String("coverprofile", "", "If supplied, use a go cover profile (comma separated)")
covermode = flag.String("covermode", "count", "sent as covermode argument to go test")
@ -127,19 +128,25 @@ func getCoverage() ([]*SourceFile, error) {
return nil, err
}
f.Close()
cmd := exec.Command("go")
outBuf := new(bytes.Buffer)
cmd.Stdout = outBuf
cmd.Stderr = outBuf
args := []string{"go", "test", "-covermode", *covermode, "-coverprofile", f.Name(), coverpkg}
if *verbose {
args = append(args, "-v")
cmd.Stdout = os.Stdout
}
args = append(args, extraFlags...)
args = append(args, line)
cmd.Args = args
b, err := cmd.CombinedOutput()
err = cmd.Run()
if err != nil {
return nil, fmt.Errorf("%v: %v", err, string(b))
return nil, fmt.Errorf("%v: %v", err, outBuf.String())
}
pfs, err := cover.ParseProfiles(f.Name())
if err != nil {
return nil, err

View File

@ -11,11 +11,18 @@ import (
"github.com/mvdan/unparam/check"
)
var tests = flag.Bool("tests", true, "include tests")
var (
tests = flag.Bool("tests", true, "include tests")
debug = flag.Bool("debug", false, "debug prints")
)
func main() {
flag.Usage = func() {
fmt.Fprintln(os.Stderr, "usage: unparam [flags] [package ...]")
flag.PrintDefaults()
}
flag.Parse()
warns, err := check.UnusedParams(*tests, flag.Args()...)
warns, err := check.UnusedParams(*tests, *debug, flag.Args()...)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)

View File

@ -5,9 +5,9 @@ adding missing ones and removing unreferenced ones.
$ go get golang.org/x/tools/cmd/goimports
It's a drop-in replacement for your editor's gofmt-on-save hook.
It has the same command-line interface as gofmt and formats
your code in the same way.
In addition to fixing imports, goimports also formats
your code in the same style as gofmt so it can be used
as a replacement for your editor's gofmt-on-save hook.
For emacs, make sure you have the latest go-mode.el:
https://github.com/dominikh/go-mode.el

View File

@ -144,12 +144,19 @@ func processFile(filename string, in io.Reader, out io.Writer, argType argumentT
fmt.Fprintln(out, filename)
}
if *write {
if argType == fromStdin {
// filename is "<standard input>"
return errors.New("can't use -w on stdin")
}
err = ioutil.WriteFile(filename, res, 0)
if err != nil {
return err
}
}
if *doDiff {
if argType == fromStdin {
filename = "stdin.go" // because <standard input>.orig looks silly
}
data, err := diff(src, res, filename)
if err != nil {
return fmt.Errorf("computing diff: %s", err)

View File

@ -68,6 +68,7 @@ constructs:
| S1027 | `return` as the final statement of a func body with no return values | Functions that don't return anything don't need a final return statement |
| S1028 | `errors.New(fmt.Sprintf(...))` | `fmt.Errorf(...)` |
| S1029 | `for _, r := range []rune(s)` | `for _, r := range s` |
| S1030 | `string(buf.Bytes())` or `[]byte(buf.String())` | Use the appropriate method of `bytes.Buffer` instead |
## gofmt -r

View File

@ -0,0 +1,18 @@
# megacheck
_megacheck_ runs staticcheck, gosimple and unused at once. Because it
is able to reuse work, it will be faster than running each tool
separately.
## Installation
go get honnef.co/go/tools/cmd/megacheck
## Usage
The basic operation of megacheck is just like that of the other tools.
The flags of the individual tools are prefixed by the tools' names.
Tools can be disabled by setting `-<tool>.enabled=false`.
For explanations of the individual tools, see their respective
readmes.

View File

@ -0,0 +1,123 @@
// megacheck runs staticcheck, gosimple and unused.
package main // import "honnef.co/go/tools/cmd/megacheck"
import (
"os"
"honnef.co/go/tools/lint"
"honnef.co/go/tools/lint/lintutil"
"honnef.co/go/tools/simple"
"honnef.co/go/tools/staticcheck"
"honnef.co/go/tools/unused"
)
type Checker struct {
Checkers []lint.Checker
}
func (c *Checker) Init(prog *lint.Program) {
for _, cc := range c.Checkers {
cc.Init(prog)
}
}
func (c *Checker) Funcs() map[string]lint.Func {
fns := map[string]lint.Func{}
for _, cc := range c.Checkers {
for k, v := range cc.Funcs() {
fns[k] = v
}
}
return fns
}
func main() {
var flags struct {
staticcheck struct {
enabled bool
generated bool
}
gosimple struct {
enabled bool
generated bool
}
unused struct {
enabled bool
constants bool
fields bool
functions bool
types bool
variables bool
debug string
wholeProgram bool
reflection bool
}
}
fs := lintutil.FlagSet("megacheck")
fs.BoolVar(&flags.gosimple.enabled,
"simple.enabled", true, "Run gosimple")
fs.BoolVar(&flags.gosimple.generated,
"simple.generated", false, "Check generated code")
fs.BoolVar(&flags.staticcheck.enabled,
"staticcheck.enabled", true, "Run staticcheck")
fs.BoolVar(&flags.staticcheck.generated,
"staticcheck.generated", false, "Check generated code (only applies to a subset of checks)")
fs.BoolVar(&flags.unused.enabled,
"unused.enabled", true, "Run unused")
fs.BoolVar(&flags.unused.constants,
"unused.consts", true, "Report unused constants")
fs.BoolVar(&flags.unused.fields,
"unused.fields", true, "Report unused fields")
fs.BoolVar(&flags.unused.functions,
"unused.funcs", true, "Report unused functions and methods")
fs.BoolVar(&flags.unused.types,
"unused.types", true, "Report unused types")
fs.BoolVar(&flags.unused.variables,
"unused.vars", true, "Report unused variables")
fs.BoolVar(&flags.unused.wholeProgram,
"unused.exported", false, "Treat arguments as a program and report unused exported identifiers")
fs.BoolVar(&flags.unused.reflection, "unused.reflect", true, "Consider identifiers as used when it's likely they'll be accessed via reflection")
fs.Parse(os.Args[1:])
c := &Checker{}
if flags.staticcheck.enabled {
sac := staticcheck.NewChecker()
sac.CheckGenerated = flags.staticcheck.generated
c.Checkers = append(c.Checkers, sac)
}
if flags.gosimple.enabled {
sc := simple.NewChecker()
sc.CheckGenerated = flags.gosimple.generated
c.Checkers = append(c.Checkers, sc)
}
if flags.unused.enabled {
var mode unused.CheckMode
if flags.unused.constants {
mode |= unused.CheckConstants
}
if flags.unused.fields {
mode |= unused.CheckFields
}
if flags.unused.functions {
mode |= unused.CheckFunctions
}
if flags.unused.types {
mode |= unused.CheckTypes
}
if flags.unused.variables {
mode |= unused.CheckVariables
}
uc := unused.NewChecker(mode)
uc.WholeProgram = flags.unused.wholeProgram
uc.ConsiderReflection = flags.unused.reflection
c.Checkers = append(c.Checkers, unused.NewLintChecker(uc))
}
lintutil.ProcessFlagSet(c, fs)
}

View File

@ -9,290 +9,8 @@ Staticcheck requires Go 1.6 or later.
go get honnef.co/go/tools/cmd/staticcheck
## Usage
## Documentation
Invoke `staticcheck` with one or more filenames, a directory, or a package named
by its import path. Staticcheck uses the same
[import path syntax](https://golang.org/cmd/go/#hdr-Import_path_syntax) as
the `go` command and therefore
also supports relative import paths like `./...`. Additionally the `...`
wildcard can be used as suffix on relative and absolute file paths to recurse
into them.
Detailed documentation can be found on
[staticcheck.io](https://staticcheck.io/docs/staticcheck).
The output of this tool is a list of suggestions in Vim quickfix format,
which is accepted by lots of different editors.
## Purpose
The main purpose of staticcheck is editor integration, or workflow
integration in general. For example, by running staticcheck when
saving a file, one can quickly catch simple bugs without having to run
the whole test suite or the program itself.
The tool shouldn't report any errors unless there are legitimate
bugs - or very dubious constructs - in the code.
It is similar in nature to `go vet`, but has more checks that catch
bugs that would also be caught easily at runtime, to reduce the number
of edit, compile and debug cycles.
## Checks
The following things are currently checked by staticcheck:
|Check|Description|
|---|---|
|**SA1???**|**Various misuses of the standard library**|
|SA1000|Invalid regular expression|
|SA1001|Invalid template|
|SA1002|Invalid format in time.Parse|
|SA1003|Unsupported argument to functions in encoding/binary|
|SA1004|Suspiciously small untyped constant in time.Sleep|
|[SA1005](#SA1005)|Invalid first argument to exec.Command|
|[SA1006](#SA1006)|Printf with dynamic first argument and no further arguments|
|SA1007|Invalid URL in net/url.Parse|
|SA1008|Non-canonical key in http.Header map|
|SA1010|`(*regexp.Regexp).FindAll` called with n == 0, which will always return zero results|
|SA1011|Various methods in the `strings` package expect valid UTF-8, but invalid input is provided|
|SA1012|A nil `context.Context` is being passed to a function, consider using context.TODO instead|
|SA1013|`io.Seeker.Seek` is being called with the `whence` constant as the first argument, but it should be the second|
|SA1014|Non-pointer value passed to Unmarshal or Decode|
|SA1015|Using `time.Tick` in a way that will leak. Consider using `time.NewTicker`, and only use `time.Tick` in tests, commands and endless functions|
|SA1016|Trapping a signal that cannot be trapped|
|SA1017|Channels used with signal.Notify should be buffered|
|SA1018|`strings.Replace` called with n == 0, which does nothing|
|SA1019|Using a deprecated function, variable, constant or field|
|SA1020|Using an invalid `host:port` pair with a `net.Listen`-related function|
|[SA1021](#SA1021)|Using bytes.Equal to compare two net.IP|
|[SA1022](#SA1022)|Calling os.Exit in a function assigned to flag.Usage|
|SA1023|Modifying the buffer in an io.Writer implementation|
|SA1024|A string cutset contains duplicate characters, suggesting TrimPrefix or TrimSuffix should be used instead of TrimLeft or TrimRight|
|||
|**SA2???**|**Concurrency issues**|
|SA2000|`sync.WaitGroup.Add` called inside the goroutine, leading to a race condition|
|SA2001|Empty critical section, did you mean to `defer` the unlock?|
|SA2002|Called testing.T.FailNow or SkipNow in a goroutine, which isn't allowed|
|SA2003|Deferred Lock right after locking, likely meant to defer Unlock instead|
|||
|**SA3???**|**Testing issues**|
|SA3000|TestMain doesn't call os.Exit, hiding test failures|
|SA3001|Assigning to `b.N` in benchmarks distorts the results|
|||
|**SA4???**|**Code that isn't really doing anything**|
|SA4000|Boolean expression has identical expressions on both sides|
|SA4001|`&*x` gets simplified to `x`, it does not copy `x`|
|SA4002|Comparing strings with known different sizes has predictable results|
|SA4003|Comparing unsigned values against negative values is pointless|
|SA4004|The loop exits unconditionally after one iteration|
|SA4005|Field assignment that will never be observed. Did you mean to use a pointer receiver?|
|SA4006|A value assigned to a variable is never read before being overwritten. Forgotten error check or dead code?|
|SA4008|The variable in the loop condition never changes, are you incrementing the wrong variable?|
|SA4009|A function argument is overwritten before its first use|
|SA4010|The result of `append` will never be observed anywhere|
|SA4011|Break statement with no effect. Did you mean to break out of an outer loop?|
|SA4012|Comparing a value against NaN even though no value is equal to NaN|
|SA4013|Negating a boolean twice (`!!b`) is the same as writing `b`. This is either redundant, or a typo.|
|SA4014|An if/else if chain has repeated conditions and no side-effects; if the condition didn't match the first time, it won't match the second time, either|
|SA4015|Calling functions like math.Ceil on floats converted from integers doesn't do anything useful|
|SA4016|Certain bitwise operations, such as `x ^ 0`, do not do anything useful|
|SA4017|A pure function's return value is discarded, making the call pointless|
|||
|**SA5???**|**Correctness issues**|
|SA5000|Assignment to nil map|
|SA5001|Defering `Close` before checking for a possible error|
|SA5002|The empty `for` loop (`for {}`) spins and can block the scheduler|
|SA5003|Defers in infinite loops will never execute|
|SA5004|`for { select { ...` with an empty default branch spins|
|[SA5005](#SA5005)|The finalizer references the finalized object, preventing garbage collection|
|SA5006|Slice index out of bounds|
|[SA5007](#SA5007)|Infinite recursive call|
|||
|**SA6???**|**Performance issues**|
|SA6000|Using `regexp.Match` or related in a loop, should use `regexp.Compile`|
|[SA6001](#SA6001)|Missing an optimization opportunity when indexing maps by byte slices|
|[SA6002](#SA6002)|Storing non-pointer values in sync.Pool allocates memory|
|[SA6003](#SA6003)|Converting a string to a slice of runes before ranging over it|
|||
|**SA9???**|**Dubious code constructs that have a high probability of being wrong**|
|SA9001|`defer`s in `for range` loops may not run when you expect them to|
|SA9002|Using a non-octal `os.FileMode` that looks like it was meant to be in octal.|
|SA9003|Empty body in an if or else branch|
|||
### <a id="SA1005">SA1005 Invalid first argument to exec.Command
`os/exec` runs programs directly (using variants of the
[fork](https://en.wikipedia.org/wiki/Fork_(system_call)) and
[exec](https://en.wikipedia.org/wiki/Exec_(system_call)) system calls
on Unix systems). This shouldn't be confused with running a command in
a shell. The shell will allow for features such as input redirection,
pipes, and general scripting. The
shell is also responsible for splitting the user's input into a
program name and its arguments. For example, the equivalent to `ls /
/tmp` would be `exec.Command("ls", "/", "/tmp")`.
If you want to run a command in a shell, consider using something like
the following but be aware that not all systems, particularly
Windows, will have a `/bin/sh` program:
```
exec.Command("/bin/sh", "-c", "ls | grep Awesome")
```
### <a id="SA1006">SA1006 Printf with dynamic first argument and no further arguments
Using `fmt.Printf` with a dynamic first argument can lead to
unexpected output. The first argument is a format string, where
certain character combinations have special meaning. If, for example,
a user were to enter a string such as `Interest rate: 5%` and you
printed it with `fmt.Printf(s)`, it would lead to the following
output: `Interest rate: 5%!(NOVERB)`.
Similarly, forming the first parameyer via string concatenation with
user input should be avoided for the same reason. When printing user
input, either use a variant of `fmt.Print`, or use the `%s` Printf
verb and pass the string as an argument.
### <a id="SA1021">SA1021 Using bytes.Equal to compare two net.IP
A `net.IP` stores an IPv4 or IPv6 address as a slice of bytes. The
length of the slice for an IPv4 address, however, can be either 4 or
16 bytes long, using different ways of representing IPv4 addresses. In
order to correctly compare two `net.IP`s, the `net.IP.Equal` method
should be used, as it takes both representations into account.
### <a id="SA1022">SA1022 Calling os.Exit in a function assigned to flag.Usage
The `flag` package has the notion of a `Usage` function, assigned to
`flag.Usage` or `flag.FlagSet.Usage`. The job of this function is to
print usage instructions for the program and it is called when invalid
flags were provided.
This function should not, however, terminate the program by calling
`os.Exit`. The `flag` package already has a mechanism for exiting on
incorrect flags, the `errorHandling` argument of `flag.NewFlagSet`.
Setting it to `flag.ExitOnError` instructs it to call `os.Exit(2)`.
There exist other values to react differently, which is why `Usage`
shouldn't call `os.Exit` on its own.
### <a id="SA5005">SA5005 The finalizer references the finalized object, preventing garbage collection
A finalizer is a function associated with an object that runs when the
garbage collector is ready to collect said object, that is when the
object is no longer referenced by anything.
If the finalizer references the object, however, it will always remain
as the final reference to that object, preventing the garbage
collector from collecting the object. The finalizer will never run,
and the object will never be collected, leading to a memory leak. That
is why the finalizer should instead use its first argument to operate
on the object. That way, the number of references can temporarily go
to zero before the object is being passed to the finalizer.
### <a id="SA5007">SA5007 Infinite recursive call
A function that calls itself recursively needs to have an exit
condition. Otherwise it will recurse forever, until the system runs
out of memory.
This issue can be caused by simple bugs such as forgetting adding an
exit condition. It can also happen "on purpose". Some languages have
[tail call optimization](https://en.wikipedia.org/wiki/Tail_call)
which makes certain infinite recursive calls safe to use. Go, however,
does not implement TCO, and as such a loop should be used instead.
### <a id="SA6001">SA6001 Missing an optimization opportunity when indexing maps by byte slices
Map keys must be comparable, which precludes the use of []byte. This
usually leads to using string keys and converting []bytes to
strings.
Normally, a conversion of []byte to string needs to copy the data and
causes allocations. The compiler, however, recognizes `m[string(b)]`
and uses the data of `b` directly, without copying it, because it
knows that the data can't change during the map lookup. This leads
to the counter-intuitive situation that
```
k := string(b)
println(m[k])
println(m[k])
```
will be less efficient than
```
println(m[string(b)])
println(m[string(b)])
```
because the first version needs to copy and allocate, while the second
one does not.
For some history on this optimization, check out commit
[f5f5a8b6209f84961687d993b93ea0d397f5d5bf](https://github.com/golang/go/commit/f5f5a8b6209f84961687d993b93ea0d397f5d5bf).
### <a id="SA6002">SA6002 Storing non-pointer values in sync.Pool allocates memory
A `sync.Pool` is used to avoid unnecessary allocations and reduce the
amount of work the garbage collector has to do.
When passing a value that is larger than a single word (8 bytes on a
64 bit machine) to a function that accepts an interface, the value
needs to be placed on the heap, which means an additional allocation.
Slices are a common thing to put in `sync.Pool`s, and they're 3 words
large (length, capacity, and a pointer to an array). In order to avoid
the extra allocation, one should store a pointer to the slice instead.
See the
[comments on a Go CL](https://go-review.googlesource.com/#/c/24371/)
that discuss this problem.
### <a id="SA6003">SA6003 Converting a string to a slice of runes before ranging over it
You may want to loop over the runes in a string. Instead of converting
the string to a slice of runes and looping over that, you can loop
over the string itself. That is,
```
for _, r := range s {}
```
and
```
for _, r := range []rune(s) {}
```
will yield the same values. The first version, however, will be faster
and avoid unnecessary memory allocations.
Do note that if you are interested in the indices, ranging over a
string and over a slice of runes will yield different indices. The
first one yields byte offsets, while the second one yields indices in
the slice of runes.
## Ignoring checks
staticcheck allows disabling some or all checks for certain files. The
`-ignore` flag takes a whitespace-separated list of
`glob:check1,check2,...` pairs. `glob` is a glob pattern matching
files in packages, and `check1,check2,...` are checks named by their
IDs.
For example, to ignore assignment to nil maps in all test files in the
`os/exec` package, you would write `-ignore
"os/exec/*_test.go:SA5000"`
Additionally, the check IDs support globbing, too. Using a pattern
such as `os/exec/*.gen.go:*` would disable all checks in all
auto-generated files in the os/exec package.
Any whitespace can be used to separate rules, including newlines. This
allows for a setup like the following:
```
$ cat stdlib.ignore
sync/*_test.go:SA2001
testing/benchmark.go:SA3001
runtime/string_test.go:SA4007
runtime/proc_test.go:SA5004
runtime/lfstack_test.go:SA4010
runtime/append_test.go:SA4010
errors/errors_test.go:SA4000
reflect/all_test.go:SA4000
$ staticcheck -ignore "$(cat stdlib.ignore)" std
```

View File

@ -39,10 +39,10 @@
"revisionTime": "2015-02-08T22:17:26Z"
},
{
"checksumSHA1": "zCnRL4ozOPngF6cn+bgZ9A4D+xE=",
"checksumSHA1": "dGfv3nwAfhSJ+20gR0yYgUwjPVY=",
"path": "github.com/alecthomas/gometalinter",
"revision": "fed116ef099c86fa41f60754555e85b7be704033",
"revisionTime": "2017-05-14T07:52:09Z"
"revision": "258ea75208e542b8a18a08e053ec10379a9f50f9",
"revisionTime": "2017-08-02T01:03:20Z"
},
{
"checksumSHA1": "fCc3grA7vIxfBru7R3SqjcW+oLI=",
@ -71,8 +71,8 @@
{
"checksumSHA1": "YdtKuQqs3pDJhMxOruVxLAD/JSo=",
"path": "github.com/client9/misspell/cmd/misspell",
"revision": "1d9ab7749ee27131547244ff2f9953d235b591fb",
"revisionTime": "2017-05-30T22:15:07Z"
"revision": "e1f24e3e0b6b2c8bc98584370d382ae095b13c94",
"revisionTime": "2017-06-21T02:16:58Z"
},
{
"checksumSHA1": "ULnk7ggN82JFO0ZdBCmSsQH3Vh8=",
@ -99,10 +99,10 @@
"revisionTime": "2015-01-27T13:39:51Z"
},
{
"checksumSHA1": "ODQ2p8mPWC3Ipa2dFhgjaruOwz0=",
"checksumSHA1": "F4OFLBCfPosjmNN+mpLtdbmlg2g=",
"path": "github.com/gordonklaus/ineffassign",
"revision": "f0c5cfc1817d6dd6011c7e8b8d272f94aeeb12cb",
"revisionTime": "2017-03-18T09:36:04Z"
"revision": "08cd83b3f513abe575f40d23acf2ae79088128bb",
"revisionTime": "2017-06-26T19:28:42Z"
},
{
"checksumSHA1": "DbSCKltce7IrgpDUF8+C7J+z+GU=",
@ -111,16 +111,16 @@
"revisionTime": "2016-05-14T19:25:19Z"
},
{
"checksumSHA1": "bW4FXd6+OJQ4w04u+vJiZTbmJfw=",
"checksumSHA1": "0tPXJ5Wul0FXiUDwVWsd/RA3tWg=",
"path": "github.com/jgautheron/goconst/cmd/goconst",
"revision": "6a7633b712b6fb1d6821d33851d086a1d545dacd",
"revisionTime": "2016-05-14T19:25:19Z"
"revision": "9740945f5dcb78c2faa8eedcce78c2a04aa6e1e9",
"revisionTime": "2017-07-03T17:01:52Z"
},
{
"checksumSHA1": "NKvKUGq0lp/GjLS7Ffp7BAjcoTg=",
"path": "github.com/kardianos/govendor",
"revision": "c86c10d612bf08e847456ce91d495eb69ad87087",
"revisionTime": "2017-05-06T05:20:04Z"
"revision": "b6d23590f46ec4816cb726e013bae44954d58972",
"revisionTime": "2017-07-28T15:58:28Z"
},
{
"checksumSHA1": "m24kWw3bFoAkKVvTjmxSLsywdHY=",
@ -213,10 +213,10 @@
"revisionTime": "2016-11-30T08:01:11Z"
},
{
"checksumSHA1": "GPZAI/PpSx3m5De6YxataiBWBvQ=",
"checksumSHA1": "18oHyXPLzfVDaXtMYIUyK23+slg=",
"path": "github.com/mattn/goveralls",
"revision": "a2cbbd7cdce4f5e051016fedf639c64bb05ef031",
"revisionTime": "2017-05-13T16:02:34Z"
"revision": "6efce81852ad1b7567c17ad71b03aeccc9dd9ae0",
"revisionTime": "2017-07-18T16:42:45Z"
},
{
"checksumSHA1": "90pFJb64MwgYvN5AmPVaWl87ZyU=",
@ -269,8 +269,8 @@
{
"checksumSHA1": "zD/VW+BRbOjxk1xq5bmdigi0cp8=",
"path": "github.com/mvdan/interfacer/cmd/interfacer",
"revision": "22c51662ff476dfd97944f74db1b263ed920ee83",
"revisionTime": "2017-04-06T16:05:15Z"
"revision": "0b82697b33395e082e4010e4122abb213eadfb5e",
"revisionTime": "2017-08-01T09:14:01Z"
},
{
"checksumSHA1": "18GDIJCo0vo+mmQDIYmyb2JSWqo=",
@ -279,10 +279,10 @@
"revisionTime": "2017-04-06T10:09:31Z"
},
{
"checksumSHA1": "5LiZtu67exUdRJ0/QQvU/epG9no=",
"checksumSHA1": "nV5rJzg1w+8RG7lgDtuMXvtgJ6g=",
"path": "github.com/mvdan/unparam",
"revision": "d647bb803b10a6777ee4c6a176416b91fa14713e",
"revisionTime": "2017-05-30T08:59:07Z"
"revision": "4f8ea7ae6525529da4e3c90bda033935b80d709a",
"revisionTime": "2017-08-02T23:35:07Z"
},
{
"checksumSHA1": "tuOLCrGa9DjfXheKkMXtHtQu3bs=",
@ -459,16 +459,16 @@
"revisionTime": "2017-05-18T06:42:59Z"
},
{
"checksumSHA1": "1MXx7WN0nA5A9v4hOFO8iLcONGo=",
"checksumSHA1": "V4M/6A62nVBzPFxPbN+EAatCrVs=",
"path": "golang.org/x/tools/cmd/goimports",
"revision": "92d42b9ff15f625347a13b6aeafd04a33537ce91",
"revisionTime": "2017-06-02T11:28:38Z"
"revision": "4e70a1b26a7875f00ca1916637a876b5ffaeec59",
"revisionTime": "2017-08-02T20:37:59Z"
},
{
"checksumSHA1": "V6/A1ZOZ2GUOZcRWcXegtci2FoU=",
"path": "golang.org/x/tools/cmd/gotype",
"revision": "92d42b9ff15f625347a13b6aeafd04a33537ce91",
"revisionTime": "2017-06-02T11:28:38Z"
"revision": "4e70a1b26a7875f00ca1916637a876b5ffaeec59",
"revisionTime": "2017-08-02T20:37:59Z"
},
{
"checksumSHA1": "nD89PLkMqA5CakR8SoDuj3iQz1M=",
@ -585,22 +585,28 @@
"revisionTime": "2017-05-22T19:09:05Z"
},
{
"checksumSHA1": "lHJgGIP4FjVWqWA6t44UGZCmIWU=",
"checksumSHA1": "tZUNdNOtkUpEB27J1ob00AFjsW4=",
"path": "honnef.co/go/tools/cmd/gosimple",
"revision": "e94d1c1a34c6b61d8d06c7793b8f22cd0dfcdd90",
"revisionTime": "2017-05-22T19:09:05Z"
"revision": "ae0caf6437a82200bc1c349a4f761b4106409f0b",
"revisionTime": "2017-07-31T15:06:36Z"
},
{
"checksumSHA1": "z3Hs/ZzkNK8nH655Rkdi5xv9pF8=",
"checksumSHA1": "pHczeoSy3ltbM5mEQzrG845aiAk=",
"path": "honnef.co/go/tools/cmd/megacheck",
"revision": "ae0caf6437a82200bc1c349a4f761b4106409f0b",
"revisionTime": "2017-07-31T15:06:36Z"
},
{
"checksumSHA1": "fCBb/czTIH/xy9Ot7sA95uRDPxE=",
"path": "honnef.co/go/tools/cmd/staticcheck",
"revision": "e94d1c1a34c6b61d8d06c7793b8f22cd0dfcdd90",
"revisionTime": "2017-05-22T19:09:05Z"
"revision": "ae0caf6437a82200bc1c349a4f761b4106409f0b",
"revisionTime": "2017-07-31T15:06:36Z"
},
{
"checksumSHA1": "TXSOAGnX/YpKr2hBk9EiE3OlODI=",
"path": "honnef.co/go/tools/cmd/unused",
"revision": "e94d1c1a34c6b61d8d06c7793b8f22cd0dfcdd90",
"revisionTime": "2017-05-22T19:09:05Z"
"revision": "ae0caf6437a82200bc1c349a4f761b4106409f0b",
"revisionTime": "2017-07-31T15:06:36Z"
},
{
"checksumSHA1": "3oKDy41034t1Hh/cEp2zvhMTU0w=",