diff --git a/.golangci.yml b/.golangci.yml index 5be886af83ba..670992d366c7 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -11,7 +11,7 @@ linters-settings: golint: min-confidence: 0 gocyclo: - min-complexity: 10 + min-complexity: 15 maligned: suggest-new: true dupl: @@ -86,7 +86,7 @@ linters: run: skip-dirs: - test/testdata_etc - - pkg/golinters/goanalysis/(checker|passes) + - internal/(cache|renameio|robustio) issues: exclude-rules: diff --git a/Makefile b/Makefile index 47cff0faee67..c41ed9c0414b 100644 --- a/Makefile +++ b/Makefile @@ -19,8 +19,8 @@ test: export GOLANGCI_LINT_INSTALLED = true test: build GL_TEST_RUN=1 time ./golangci-lint run -v - GL_TEST_RUN=1 time ./golangci-lint run --fast --no-config -v --skip-dirs 'test/testdata_etc,pkg/golinters/goanalysis/(checker|passes)' - GL_TEST_RUN=1 time ./golangci-lint run --no-config -v --skip-dirs 'test/testdata_etc,pkg/golinters/goanalysis/(checker|passes)' + GL_TEST_RUN=1 time ./golangci-lint run --fast --no-config -v --skip-dirs 'test/testdata_etc,internal/(cache|renameio|robustio)' + GL_TEST_RUN=1 time ./golangci-lint run --no-config -v --skip-dirs 'test/testdata_etc,internal/(cache|renameio|robustio)' GL_TEST_RUN=1 time go test -v ./... .PHONY: test diff --git a/README.md b/README.md index 4bf44de5e6a9..5e5b7e536bf9 100644 --- a/README.md +++ b/README.md @@ -878,7 +878,7 @@ linters-settings: golint: min-confidence: 0 gocyclo: - min-complexity: 10 + min-complexity: 15 maligned: suggest-new: true dupl: @@ -953,7 +953,7 @@ linters: run: skip-dirs: - test/testdata_etc - - pkg/golinters/goanalysis/(checker|passes) + - internal/(cache|renameio|robustio) issues: exclude-rules: diff --git a/go.mod b/go.mod index e9af5c534c5b..ac26c5750727 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,6 @@ require ( github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6 github.com/golangci/go-misc v0.0.0-20180628070357-927a3d87b613 - github.com/golangci/go-tools v0.0.0-20190318055746-e32c54105b7c github.com/golangci/goconst v0.0.0-20180610141641-041c5f2b40f3 github.com/golangci/gocyclo v0.0.0-20180528134321-2becd97e67ee github.com/golangci/gofmt v0.0.0-20181222123516-0b8337e80d98 @@ -45,10 +44,13 @@ require ( github.com/valyala/quicktemplate v1.2.0 golang.org/x/tools v0.0.0-20190912215617-3720d1ec3678 gopkg.in/yaml.v2 v2.2.2 + honnef.co/go/tools v0.0.1-2019.2.3 mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect mvdan.cc/unparam v0.0.0-20190720180237-d51796306d8f ) +// https://github.com/golang/tools/pull/162 // https://github.com/golang/tools/pull/160 -replace golang.org/x/tools => github.com/golangci/tools v0.0.0-20190914130248-e9260b99c8f1 +// https://github.com/golang/tools/pull/156 +replace golang.org/x/tools => github.com/golangci/tools v0.0.0-20190915081525-6aa350649b1c diff --git a/go.sum b/go.sum index 48e79c231144..1ae2c3ffe843 100644 --- a/go.sum +++ b/go.sum @@ -84,8 +84,6 @@ github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6 h1:YYWNAGTKWhKpc github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6/go.mod h1:DbHgvLiFKX1Sh2T1w8Q/h4NAI8MHIpzCdnBUDTXU3I0= github.com/golangci/go-misc v0.0.0-20180628070357-927a3d87b613 h1:9kfjN3AdxcbsZBf8NjltjWihK2QfBBBZuv91cMFfDHw= github.com/golangci/go-misc v0.0.0-20180628070357-927a3d87b613/go.mod h1:SyvUF2NxV+sN8upjjeVYr5W7tyxaT1JVtvhKhOn2ii8= -github.com/golangci/go-tools v0.0.0-20190318055746-e32c54105b7c h1:/7detzz5stiXWPzkTlPTzkBEIIE4WGpppBJYjKqBiPI= -github.com/golangci/go-tools v0.0.0-20190318055746-e32c54105b7c/go.mod h1:unzUULGw35sjyOYjUt0jMTXqHlZPpPc6e+xfO4cd6mM= github.com/golangci/goconst v0.0.0-20180610141641-041c5f2b40f3 h1:pe9JHs3cHHDQgOFXJJdYkK6fLz2PWyYtP4hthoCMvs8= github.com/golangci/goconst v0.0.0-20180610141641-041c5f2b40f3/go.mod h1:JXrF4TWy4tXYn62/9x8Wm/K/dm06p8tCKwFRDPZG/1o= github.com/golangci/gocyclo v0.0.0-20180528134321-2becd97e67ee h1:J2XAy40+7yz70uaOiMbNnluTg7gyQhtGqLQncQh+4J8= @@ -104,13 +102,14 @@ github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21 h1:leSNB7iYzLYSS github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21/go.mod h1:tf5+bzsHdTM0bsB7+8mt0GUMvjCgwLpTapNZHU8AajI= github.com/golangci/revgrep v0.0.0-20180526074752-d9c87f5ffaf0 h1:HVfrLniijszjS1aiNg8JbBMO2+E1WIQ+j/gL4SQqGPg= github.com/golangci/revgrep v0.0.0-20180526074752-d9c87f5ffaf0/go.mod h1:qOQCunEYvmd/TLamH+7LlVccLvUH5kZNhbCgTHoBbp4= -github.com/golangci/tools v0.0.0-20190914130248-e9260b99c8f1 h1:8eGJVbBRoAvCh/YZq6n11s9WJkIgWKa/iTNq+R0UrNw= -github.com/golangci/tools v0.0.0-20190914130248-e9260b99c8f1/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +github.com/golangci/tools v0.0.0-20190915081525-6aa350649b1c h1:JF7g2hV+1F/DwJ3CrSxOc9ZNVY6N8zYt5mB0Qve//fU= +github.com/golangci/tools v0.0.0-20190915081525-6aa350649b1c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4 h1:zwtduBRr5SSWhqsYNgcuWO2kFlpdOZbP0+yRjmvPGys= github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4/go.mod h1:Izgrg8RkN3rCIMLGE9CyYmU9pY2Jer6DgANEnZ/L/cQ= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gostaticanalysis/analysisutil v0.0.0-20190318220348-4088753ea4d3 h1:JVnpOZS+qxli+rgVl98ILOXVNbW+kb5wcxeGx8ShUIw= github.com/gostaticanalysis/analysisutil v0.0.0-20190318220348-4088753ea4d3/go.mod h1:eEOZF4jCKGi+aprrirO9e7WKB3beBRtWgqGunKl6pKE= @@ -188,6 +187,7 @@ github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40T github.com/quasilyte/go-consistent v0.0.0-20190521200055-c6f3937de18c/go.mod h1:5STLWrekHfjyYwxBRVRXNOSewLJ3PWfDJd1VyTS21fI= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/securego/gosec v0.0.0-20190912120752-140048b2a218 h1:O0yPHYL49quNL4Oj2wVq+zbGMu4dAM6iLoOQtm49TrQ= github.com/securego/gosec v0.0.0-20190912120752-140048b2a218/go.mod h1:q6oYAujd2qyeU4cJqIri4LBIgdHXGvxWHZ1E29HNFRE= @@ -250,9 +250,11 @@ go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -305,7 +307,10 @@ gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bl gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099 h1:XJP7lxbSxWLOMNdBE4B/STaqVy6L73o0knwj2vIlxnw= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM= +honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed h1:WX1yoOaKQfddO/mLzdV4wptyWgoH/6hwLs7QHTixo0I= mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed/go.mod h1:Xkxe497xwlCKkIaQYRfC7CSLworTXY9RMqwhhCm+8Nc= mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b h1:DxJ5nJdkhDlLok9K6qO+5290kphDJbHOQO1DFFFTeBo= diff --git a/internal/cache/cache.go b/internal/cache/cache.go new file mode 100644 index 000000000000..2157c9f27c1e --- /dev/null +++ b/internal/cache/cache.go @@ -0,0 +1,500 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package cache implements a build artifact cache. +// +// This package is a slightly modified fork of Go's +// cmd/go/internal/cache package. +package cache + +import ( + "bytes" + "crypto/sha256" + "encoding/hex" + "errors" + "fmt" + "io" + "io/ioutil" + "os" + "path/filepath" + "strconv" + "strings" + "time" + + "github.com/golangci/golangci-lint/internal/renameio" +) + +// An ActionID is a cache action key, the hash of a complete description of a +// repeatable computation (command line, environment variables, +// input file contents, executable contents). +type ActionID [HashSize]byte + +// An OutputID is a cache output key, the hash of an output of a computation. +type OutputID [HashSize]byte + +// A Cache is a package cache, backed by a file system directory tree. +type Cache struct { + dir string + now func() time.Time +} + +// Open opens and returns the cache in the given directory. +// +// It is safe for multiple processes on a single machine to use the +// same cache directory in a local file system simultaneously. +// They will coordinate using operating system file locks and may +// duplicate effort but will not corrupt the cache. +// +// However, it is NOT safe for multiple processes on different machines +// to share a cache directory (for example, if the directory were stored +// in a network file system). File locking is notoriously unreliable in +// network file systems and may not suffice to protect the cache. +// +func Open(dir string) (*Cache, error) { + info, err := os.Stat(dir) + if err != nil { + return nil, err + } + if !info.IsDir() { + return nil, &os.PathError{Op: "open", Path: dir, Err: fmt.Errorf("not a directory")} + } + for i := 0; i < 256; i++ { + name := filepath.Join(dir, fmt.Sprintf("%02x", i)) + if err := os.MkdirAll(name, 0777); err != nil { + return nil, err + } + } + c := &Cache{ + dir: dir, + now: time.Now, + } + return c, nil +} + +// fileName returns the name of the file corresponding to the given id. +func (c *Cache) fileName(id [HashSize]byte, key string) string { + return filepath.Join(c.dir, fmt.Sprintf("%02x", id[0]), fmt.Sprintf("%x", id)+"-"+key) +} + +var errMissing = errors.New("cache entry not found") + +func IsErrMissing(err error) bool { + return err == errMissing +} + +const ( + // action entry file is "v1 \n" + hexSize = HashSize * 2 + entrySize = 2 + 1 + hexSize + 1 + hexSize + 1 + 20 + 1 + 20 + 1 +) + +// verify controls whether to run the cache in verify mode. +// In verify mode, the cache always returns errMissing from Get +// but then double-checks in Put that the data being written +// exactly matches any existing entry. This provides an easy +// way to detect program behavior that would have been different +// had the cache entry been returned from Get. +// +// verify is enabled by setting the environment variable +// GODEBUG=gocacheverify=1. +var verify = false + +// DebugTest is set when GODEBUG=gocachetest=1 is in the environment. +var DebugTest = false + +func init() { initEnv() } + +func initEnv() { + verify = false + debugHash = false + debug := strings.Split(os.Getenv("GODEBUG"), ",") + for _, f := range debug { + if f == "gocacheverify=1" { + verify = true + } + if f == "gocachehash=1" { + debugHash = true + } + if f == "gocachetest=1" { + DebugTest = true + } + } +} + +// Get looks up the action ID in the cache, +// returning the corresponding output ID and file size, if any. +// Note that finding an output ID does not guarantee that the +// saved file for that output ID is still available. +func (c *Cache) Get(id ActionID) (Entry, error) { + if verify { + return Entry{}, errMissing + } + return c.get(id) +} + +type Entry struct { + OutputID OutputID + Size int64 + Time time.Time +} + +// get is Get but does not respect verify mode, so that Put can use it. +func (c *Cache) get(id ActionID) (Entry, error) { + missing := func() (Entry, error) { + return Entry{}, errMissing + } + f, err := os.Open(c.fileName(id, "a")) + if err != nil { + return missing() + } + defer f.Close() + entry := make([]byte, entrySize+1) // +1 to detect whether f is too long + if n, err := io.ReadFull(f, entry); n != entrySize || err != io.ErrUnexpectedEOF { + return missing() + } + if entry[0] != 'v' || entry[1] != '1' || entry[2] != ' ' || entry[3+hexSize] != ' ' || entry[3+hexSize+1+hexSize] != ' ' || entry[3+hexSize+1+hexSize+1+20] != ' ' || entry[entrySize-1] != '\n' { + return missing() + } + eid, entry := entry[3:3+hexSize], entry[3+hexSize:] + eout, entry := entry[1:1+hexSize], entry[1+hexSize:] + esize, entry := entry[1:1+20], entry[1+20:] + //lint:ignore SA4006 See https://github.com/dominikh/go-tools/issues/465 + etime, entry := entry[1:1+20], entry[1+20:] //nolint:staticcheck + var buf [HashSize]byte + if _, err := hex.Decode(buf[:], eid); err != nil || buf != id { + return missing() + } + if _, err := hex.Decode(buf[:], eout); err != nil { + return missing() + } + i := 0 + for i < len(esize) && esize[i] == ' ' { + i++ + } + size, err := strconv.ParseInt(string(esize[i:]), 10, 64) + if err != nil || size < 0 { + return missing() + } + i = 0 + for i < len(etime) && etime[i] == ' ' { + i++ + } + tm, err := strconv.ParseInt(string(etime[i:]), 10, 64) + if err != nil || tm < 0 { + return missing() + } + + c.used(c.fileName(id, "a")) + + return Entry{buf, size, time.Unix(0, tm)}, nil +} + +// GetFile looks up the action ID in the cache and returns +// the name of the corresponding data file. +func (c *Cache) GetFile(id ActionID) (file string, entry Entry, err error) { + entry, err = c.Get(id) + if err != nil { + return "", Entry{}, err + } + file = c.OutputFile(entry.OutputID) + info, err := os.Stat(file) + if err != nil || info.Size() != entry.Size { + return "", Entry{}, errMissing + } + return file, entry, nil +} + +// GetBytes looks up the action ID in the cache and returns +// the corresponding output bytes. +// GetBytes should only be used for data that can be expected to fit in memory. +func (c *Cache) GetBytes(id ActionID) ([]byte, Entry, error) { + entry, err := c.Get(id) + if err != nil { + return nil, entry, err + } + data, _ := ioutil.ReadFile(c.OutputFile(entry.OutputID)) + if sha256.Sum256(data) != entry.OutputID { + return nil, entry, errMissing + } + return data, entry, nil +} + +// OutputFile returns the name of the cache file storing output with the given OutputID. +func (c *Cache) OutputFile(out OutputID) string { + file := c.fileName(out, "d") + c.used(file) + return file +} + +// Time constants for cache expiration. +// +// We set the mtime on a cache file on each use, but at most one per mtimeInterval (1 hour), +// to avoid causing many unnecessary inode updates. The mtimes therefore +// roughly reflect "time of last use" but may in fact be older by at most an hour. +// +// We scan the cache for entries to delete at most once per trimInterval (1 day). +// +// When we do scan the cache, we delete entries that have not been used for +// at least trimLimit (5 days). Statistics gathered from a month of usage by +// Go developers found that essentially all reuse of cached entries happened +// within 5 days of the previous reuse. See golang.org/issue/22990. +const ( + mtimeInterval = 1 * time.Hour + trimInterval = 24 * time.Hour + trimLimit = 5 * 24 * time.Hour +) + +// used makes a best-effort attempt to update mtime on file, +// so that mtime reflects cache access time. +// +// Because the reflection only needs to be approximate, +// and to reduce the amount of disk activity caused by using +// cache entries, used only updates the mtime if the current +// mtime is more than an hour old. This heuristic eliminates +// nearly all of the mtime updates that would otherwise happen, +// while still keeping the mtimes useful for cache trimming. +func (c *Cache) used(file string) { + info, err := os.Stat(file) + if err == nil && c.now().Sub(info.ModTime()) < mtimeInterval { + return + } + os.Chtimes(file, c.now(), c.now()) +} + +// Trim removes old cache entries that are likely not to be reused. +func (c *Cache) Trim() { + now := c.now() + + // We maintain in dir/trim.txt the time of the last completed cache trim. + // If the cache has been trimmed recently enough, do nothing. + // This is the common case. + data, _ := renameio.ReadFile(filepath.Join(c.dir, "trim.txt")) + t, err := strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64) + if err == nil && now.Sub(time.Unix(t, 0)) < trimInterval { + return + } + + // Trim each of the 256 subdirectories. + // We subtract an additional mtimeInterval + // to account for the imprecision of our "last used" mtimes. + cutoff := now.Add(-trimLimit - mtimeInterval) + for i := 0; i < 256; i++ { + subdir := filepath.Join(c.dir, fmt.Sprintf("%02x", i)) + c.trimSubdir(subdir, cutoff) + } + + // Ignore errors from here: if we don't write the complete timestamp, the + // cache will appear older than it is, and we'll trim it again next time. + renameio.WriteFile(filepath.Join(c.dir, "trim.txt"), []byte(fmt.Sprintf("%d", now.Unix())), 0666) +} + +// trimSubdir trims a single cache subdirectory. +func (c *Cache) trimSubdir(subdir string, cutoff time.Time) { + // Read all directory entries from subdir before removing + // any files, in case removing files invalidates the file offset + // in the directory scan. Also, ignore error from f.Readdirnames, + // because we don't care about reporting the error and we still + // want to process any entries found before the error. + f, err := os.Open(subdir) + if err != nil { + return + } + names, _ := f.Readdirnames(-1) + f.Close() + + for _, name := range names { + // Remove only cache entries (xxxx-a and xxxx-d). + if !strings.HasSuffix(name, "-a") && !strings.HasSuffix(name, "-d") { + continue + } + entry := filepath.Join(subdir, name) + info, err := os.Stat(entry) + if err == nil && info.ModTime().Before(cutoff) { + os.Remove(entry) + } + } +} + +// putIndexEntry adds an entry to the cache recording that executing the action +// with the given id produces an output with the given output id (hash) and size. +func (c *Cache) putIndexEntry(id ActionID, out OutputID, size int64, allowVerify bool) error { + // Note: We expect that for one reason or another it may happen + // that repeating an action produces a different output hash + // (for example, if the output contains a time stamp or temp dir name). + // While not ideal, this is also not a correctness problem, so we + // don't make a big deal about it. In particular, we leave the action + // cache entries writable specifically so that they can be overwritten. + // + // Setting GODEBUG=gocacheverify=1 does make a big deal: + // in verify mode we are double-checking that the cache entries + // are entirely reproducible. As just noted, this may be unrealistic + // in some cases but the check is also useful for shaking out real bugs. + entry := fmt.Sprintf("v1 %x %x %20d %20d\n", id, out, size, time.Now().UnixNano()) + + if verify && allowVerify { + old, err := c.get(id) + if err == nil && (old.OutputID != out || old.Size != size) { + // panic to show stack trace, so we can see what code is generating this cache entry. + msg := fmt.Sprintf("go: internal cache error: cache verify failed: id=%x changed:<<<\n%s\n>>>\nold: %x %d\nnew: %x %d", id, reverseHash(id), out, size, old.OutputID, old.Size) + panic(msg) + } + } + file := c.fileName(id, "a") + + // Copy file to cache directory. + mode := os.O_WRONLY | os.O_CREATE + f, err := os.OpenFile(file, mode, 0666) + if err != nil { + return err + } + _, err = f.WriteString(entry) + if err == nil { + // Truncate the file only *after* writing it. + // (This should be a no-op, but truncate just in case of previous corruption.) + // + // This differs from ioutil.WriteFile, which truncates to 0 *before* writing + // via os.O_TRUNC. Truncating only after writing ensures that a second write + // of the same content to the same file is idempotent, and does not — even + // temporarily! — undo the effect of the first write. + err = f.Truncate(int64(len(entry))) + } + if closeErr := f.Close(); err == nil { + err = closeErr + } + if err != nil { + // TODO(bcmills): This Remove potentially races with another go command writing to file. + // Can we eliminate it? + os.Remove(file) + return err + } + os.Chtimes(file, c.now(), c.now()) // mainly for tests + + return nil +} + +// Put stores the given output in the cache as the output for the action ID. +// It may read file twice. The content of file must not change between the two passes. +func (c *Cache) Put(id ActionID, file io.ReadSeeker) (OutputID, int64, error) { + return c.put(id, file, true) +} + +// PutNoVerify is like Put but disables the verify check +// when GODEBUG=goverifycache=1 is set. +// It is meant for data that is OK to cache but that we expect to vary slightly from run to run, +// like test output containing times and the like. +func (c *Cache) PutNoVerify(id ActionID, file io.ReadSeeker) (OutputID, int64, error) { + return c.put(id, file, false) +} + +func (c *Cache) put(id ActionID, file io.ReadSeeker, allowVerify bool) (OutputID, int64, error) { + // Compute output ID. + h := sha256.New() + if _, err := file.Seek(0, 0); err != nil { + return OutputID{}, 0, err + } + size, err := io.Copy(h, file) + if err != nil { + return OutputID{}, 0, err + } + var out OutputID + h.Sum(out[:0]) + + // Copy to cached output file (if not already present). + if err := c.copyFile(file, out, size); err != nil { + return out, size, err + } + + // Add to cache index. + return out, size, c.putIndexEntry(id, out, size, allowVerify) +} + +// PutBytes stores the given bytes in the cache as the output for the action ID. +func (c *Cache) PutBytes(id ActionID, data []byte) error { + _, _, err := c.Put(id, bytes.NewReader(data)) + return err +} + +// copyFile copies file into the cache, expecting it to have the given +// output ID and size, if that file is not present already. +func (c *Cache) copyFile(file io.ReadSeeker, out OutputID, size int64) error { + name := c.fileName(out, "d") + info, err := os.Stat(name) + if err == nil && info.Size() == size { + // Check hash. + if f, err := os.Open(name); err == nil { + h := sha256.New() + io.Copy(h, f) + f.Close() + var out2 OutputID + h.Sum(out2[:0]) + if out == out2 { + return nil + } + } + // Hash did not match. Fall through and rewrite file. + } + + // Copy file to cache directory. + mode := os.O_RDWR | os.O_CREATE + if err == nil && info.Size() > size { // shouldn't happen but fix in case + mode |= os.O_TRUNC + } + f, err := os.OpenFile(name, mode, 0666) + if err != nil { + return err + } + defer f.Close() + if size == 0 { + // File now exists with correct size. + // Only one possible zero-length file, so contents are OK too. + // Early return here makes sure there's a "last byte" for code below. + return nil + } + + // From here on, if any of the I/O writing the file fails, + // we make a best-effort attempt to truncate the file f + // before returning, to avoid leaving bad bytes in the file. + + // Copy file to f, but also into h to double-check hash. + if _, err := file.Seek(0, 0); err != nil { + f.Truncate(0) + return err + } + h := sha256.New() + w := io.MultiWriter(f, h) + if _, err := io.CopyN(w, file, size-1); err != nil { + f.Truncate(0) + return err + } + // Check last byte before writing it; writing it will make the size match + // what other processes expect to find and might cause them to start + // using the file. + buf := make([]byte, 1) + if _, err := file.Read(buf); err != nil { + f.Truncate(0) + return err + } + h.Write(buf) + sum := h.Sum(nil) + if !bytes.Equal(sum, out[:]) { + f.Truncate(0) + return fmt.Errorf("file content changed underfoot") + } + + // Commit cache file entry. + if _, err := f.Write(buf); err != nil { + f.Truncate(0) + return err + } + if err := f.Close(); err != nil { + // Data might not have been written, + // but file may look like it is the right size. + // To be extra careful, remove cached file. + os.Remove(name) + return err + } + os.Chtimes(name, c.now(), c.now()) // mainly for tests + + return nil +} diff --git a/internal/cache/cache_test.go b/internal/cache/cache_test.go new file mode 100644 index 000000000000..7229bc4cec49 --- /dev/null +++ b/internal/cache/cache_test.go @@ -0,0 +1,270 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cache + +import ( + "bytes" + "encoding/binary" + "fmt" + "io/ioutil" + "os" + "path/filepath" + "testing" + "time" +) + +func init() { + verify = false // even if GODEBUG is set +} + +func TestBasic(t *testing.T) { + dir, err := ioutil.TempDir("", "cachetest-") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + _, err = Open(filepath.Join(dir, "notexist")) + if err == nil { + t.Fatal(`Open("tmp/notexist") succeeded, want failure`) + } + + cdir := filepath.Join(dir, "c1") + if err := os.Mkdir(cdir, 0777); err != nil { + t.Fatal(err) + } + + c1, err := Open(cdir) + if err != nil { + t.Fatalf("Open(c1) (create): %v", err) + } + if err := c1.putIndexEntry(dummyID(1), dummyID(12), 13, true); err != nil { + t.Fatalf("addIndexEntry: %v", err) + } + if err := c1.putIndexEntry(dummyID(1), dummyID(2), 3, true); err != nil { // overwrite entry + t.Fatalf("addIndexEntry: %v", err) + } + if entry, err := c1.Get(dummyID(1)); err != nil || entry.OutputID != dummyID(2) || entry.Size != 3 { + t.Fatalf("c1.Get(1) = %x, %v, %v, want %x, %v, nil", entry.OutputID, entry.Size, err, dummyID(2), 3) + } + + c2, err := Open(cdir) + if err != nil { + t.Fatalf("Open(c2) (reuse): %v", err) + } + if entry, err := c2.Get(dummyID(1)); err != nil || entry.OutputID != dummyID(2) || entry.Size != 3 { + t.Fatalf("c2.Get(1) = %x, %v, %v, want %x, %v, nil", entry.OutputID, entry.Size, err, dummyID(2), 3) + } + if err := c2.putIndexEntry(dummyID(2), dummyID(3), 4, true); err != nil { + t.Fatalf("addIndexEntry: %v", err) + } + if entry, err := c1.Get(dummyID(2)); err != nil || entry.OutputID != dummyID(3) || entry.Size != 4 { + t.Fatalf("c1.Get(2) = %x, %v, %v, want %x, %v, nil", entry.OutputID, entry.Size, err, dummyID(3), 4) + } +} + +func TestGrowth(t *testing.T) { + dir, err := ioutil.TempDir("", "cachetest-") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + + c, err := Open(dir) + if err != nil { + t.Fatalf("Open: %v", err) + } + + n := 10000 + if testing.Short() { + n = 1000 + } + + for i := 0; i < n; i++ { + if err := c.putIndexEntry(dummyID(i), dummyID(i*99), int64(i)*101, true); err != nil { + t.Fatalf("addIndexEntry: %v", err) + } + id := ActionID(dummyID(i)) + entry, err := c.Get(id) + if err != nil { + t.Fatalf("Get(%x): %v", id, err) + } + if entry.OutputID != dummyID(i*99) || entry.Size != int64(i)*101 { + t.Errorf("Get(%x) = %x, %d, want %x, %d", id, entry.OutputID, entry.Size, dummyID(i*99), int64(i)*101) + } + } + for i := 0; i < n; i++ { + id := ActionID(dummyID(i)) + entry, err := c.Get(id) + if err != nil { + t.Fatalf("Get2(%x): %v", id, err) + } + if entry.OutputID != dummyID(i*99) || entry.Size != int64(i)*101 { + t.Errorf("Get2(%x) = %x, %d, want %x, %d", id, entry.OutputID, entry.Size, dummyID(i*99), int64(i)*101) + } + } +} + +func TestVerifyPanic(t *testing.T) { + os.Setenv("GODEBUG", "gocacheverify=1") + initEnv() + defer func() { + os.Unsetenv("GODEBUG") + verify = false + }() + + if !verify { + t.Fatal("initEnv did not set verify") + } + + dir, err := ioutil.TempDir("", "cachetest-") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + + c, err := Open(dir) + if err != nil { + t.Fatalf("Open: %v", err) + } + + id := ActionID(dummyID(1)) + if err := c.PutBytes(id, []byte("abc")); err != nil { + t.Fatal(err) + } + + defer func() { + if err := recover(); err != nil { + t.Log(err) + return + } + }() + c.PutBytes(id, []byte("def")) + t.Fatal("mismatched Put did not panic in verify mode") +} + +func dummyID(x int) [HashSize]byte { + var out [HashSize]byte + binary.LittleEndian.PutUint64(out[:], uint64(x)) + return out +} + +func TestCacheTrim(t *testing.T) { + dir, err := ioutil.TempDir("", "cachetest-") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + + c, err := Open(dir) + if err != nil { + t.Fatalf("Open: %v", err) + } + const start = 1000000000 + now := int64(start) + c.now = func() time.Time { return time.Unix(now, 0) } + + checkTime := func(name string, mtime int64) { + t.Helper() + file := filepath.Join(c.dir, name[:2], name) + info, err := os.Stat(file) + if err != nil { + t.Fatal(err) + } + if info.ModTime().Unix() != mtime { + t.Fatalf("%s mtime = %d, want %d", name, info.ModTime().Unix(), mtime) + } + } + + id := ActionID(dummyID(1)) + c.PutBytes(id, []byte("abc")) + entry, _ := c.Get(id) + c.PutBytes(ActionID(dummyID(2)), []byte("def")) + mtime := now + checkTime(fmt.Sprintf("%x-a", id), mtime) + checkTime(fmt.Sprintf("%x-d", entry.OutputID), mtime) + + // Get should not change recent mtimes. + now = start + 10 + c.Get(id) + checkTime(fmt.Sprintf("%x-a", id), mtime) + checkTime(fmt.Sprintf("%x-d", entry.OutputID), mtime) + + // Get should change distant mtimes. + now = start + 5000 + mtime2 := now + if _, err := c.Get(id); err != nil { + t.Fatal(err) + } + c.OutputFile(entry.OutputID) + checkTime(fmt.Sprintf("%x-a", id), mtime2) + checkTime(fmt.Sprintf("%x-d", entry.OutputID), mtime2) + + // Trim should leave everything alone: it's all too new. + c.Trim() + if _, err := c.Get(id); err != nil { + t.Fatal(err) + } + c.OutputFile(entry.OutputID) + data, err := ioutil.ReadFile(filepath.Join(dir, "trim.txt")) + if err != nil { + t.Fatal(err) + } + checkTime(fmt.Sprintf("%x-a", dummyID(2)), start) + + // Trim less than a day later should not do any work at all. + now = start + 80000 + c.Trim() + if _, err := c.Get(id); err != nil { + t.Fatal(err) + } + c.OutputFile(entry.OutputID) + data2, err := ioutil.ReadFile(filepath.Join(dir, "trim.txt")) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(data, data2) { + t.Fatalf("second trim did work: %q -> %q", data, data2) + } + + // Fast forward and do another trim just before the 5 day cutoff. + // Note that because of usedQuantum the cutoff is actually 5 days + 1 hour. + // We used c.Get(id) just now, so 5 days later it should still be kept. + // On the other hand almost a full day has gone by since we wrote dummyID(2) + // and we haven't looked at it since, so 5 days later it should be gone. + now += 5 * 86400 + checkTime(fmt.Sprintf("%x-a", dummyID(2)), start) + c.Trim() + if _, err := c.Get(id); err != nil { + t.Fatal(err) + } + c.OutputFile(entry.OutputID) + mtime3 := now + if _, err := c.Get(dummyID(2)); err == nil { // haven't done a Get for this since original write above + t.Fatalf("Trim did not remove dummyID(2)") + } + + // The c.Get(id) refreshed id's mtime again. + // Check that another 5 days later it is still not gone, + // but check by using checkTime, which doesn't bring mtime forward. + now += 5 * 86400 + c.Trim() + checkTime(fmt.Sprintf("%x-a", id), mtime3) + checkTime(fmt.Sprintf("%x-d", entry.OutputID), mtime3) + + // Half a day later Trim should still be a no-op, because there was a Trim recently. + // Even though the entry for id is now old enough to be trimmed, + // it gets a reprieve until the time comes for a new Trim scan. + now += 86400 / 2 + c.Trim() + checkTime(fmt.Sprintf("%x-a", id), mtime3) + checkTime(fmt.Sprintf("%x-d", entry.OutputID), mtime3) + + // Another half a day later, Trim should actually run, and it should remove id. + now += 86400/2 + 1 + c.Trim() + if _, err := c.Get(dummyID(1)); err == nil { + t.Fatal("Trim did not remove dummyID(1)") + } +} diff --git a/internal/cache/default.go b/internal/cache/default.go new file mode 100644 index 000000000000..6b7d8b4f74b1 --- /dev/null +++ b/internal/cache/default.go @@ -0,0 +1,85 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cache + +import ( + "fmt" + "io/ioutil" + "log" + "os" + "path/filepath" + "sync" +) + +// Default returns the default cache to use. +func Default() (*Cache, error) { + defaultOnce.Do(initDefaultCache) + return defaultCache, defaultDirErr +} + +var ( + defaultOnce sync.Once + defaultCache *Cache +) + +// cacheREADME is a message stored in a README in the cache directory. +// Because the cache lives outside the normal Go trees, we leave the +// README as a courtesy to explain where it came from. +const cacheREADME = `This directory holds cached build artifacts from golangci-lint. +` + +// initDefaultCache does the work of finding the default cache +// the first time Default is called. +func initDefaultCache() { + dir := DefaultDir() + if err := os.MkdirAll(dir, 0777); err != nil { + log.Fatalf("failed to initialize build cache at %s: %s\n", dir, err) + } + if _, err := os.Stat(filepath.Join(dir, "README")); err != nil { + // Best effort. + ioutil.WriteFile(filepath.Join(dir, "README"), []byte(cacheREADME), 0666) + } + + c, err := Open(dir) + if err != nil { + log.Fatalf("failed to initialize build cache at %s: %s\n", dir, err) + } + defaultCache = c +} + +var ( + defaultDirOnce sync.Once + defaultDir string + defaultDirErr error +) + +// DefaultDir returns the effective GOLANGCI_LINT_CACHE setting. +func DefaultDir() string { + // Save the result of the first call to DefaultDir for later use in + // initDefaultCache. cmd/go/main.go explicitly sets GOCACHE so that + // subprocesses will inherit it, but that means initDefaultCache can't + // otherwise distinguish between an explicit "off" and a UserCacheDir error. + + defaultDirOnce.Do(func() { + defaultDir = os.Getenv("GOLANGCI_LINT_CACHE") + if filepath.IsAbs(defaultDir) { + return + } + if defaultDir != "" { + defaultDirErr = fmt.Errorf("GOLANGCI_LINT_CACHE is not an absolute path") + return + } + + // Compute default location. + dir, err := os.UserCacheDir() + if err != nil { + defaultDirErr = fmt.Errorf("GOLANGCI_LINT_CACHE is not defined and %v", err) + return + } + defaultDir = filepath.Join(dir, "golangci-lint") + }) + + return defaultDir +} diff --git a/internal/cache/hash.go b/internal/cache/hash.go new file mode 100644 index 000000000000..a42f149c7125 --- /dev/null +++ b/internal/cache/hash.go @@ -0,0 +1,176 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cache + +import ( + "bytes" + "crypto/sha256" + "fmt" + "hash" + "io" + "os" + "sync" +) + +var debugHash = false // set when GODEBUG=gocachehash=1 + +// HashSize is the number of bytes in a hash. +const HashSize = 32 + +// A Hash provides access to the canonical hash function used to index the cache. +// The current implementation uses salted SHA256, but clients must not assume this. +type Hash struct { + h hash.Hash + name string // for debugging + buf *bytes.Buffer // for verify +} + +// hashSalt is a salt string added to the beginning of every hash +// created by NewHash. Using the golangci-lint version makes sure that different +// versions of the command do not address the same cache +// entries, so that a bug in one version does not affect the execution +// of other versions. This salt will result in additional ActionID files +// in the cache, but not additional copies of the large output files, +// which are still addressed by unsalted SHA256. +var hashSalt []byte + +func SetSalt(b []byte) { + hashSalt = b +} + +// Subkey returns an action ID corresponding to mixing a parent +// action ID with a string description of the subkey. +func Subkey(parent ActionID, desc string) ActionID { + h := sha256.New() + h.Write([]byte("subkey:")) + h.Write(parent[:]) + h.Write([]byte(desc)) + var out ActionID + h.Sum(out[:0]) + if debugHash { + fmt.Fprintf(os.Stderr, "HASH subkey %x %q = %x\n", parent, desc, out) + } + if verify { + hashDebug.Lock() + hashDebug.m[out] = fmt.Sprintf("subkey %x %q", parent, desc) + hashDebug.Unlock() + } + return out +} + +// NewHash returns a new Hash. +// The caller is expected to Write data to it and then call Sum. +func NewHash(name string) *Hash { + h := &Hash{h: sha256.New(), name: name} + if debugHash { + fmt.Fprintf(os.Stderr, "HASH[%s]\n", h.name) + } + h.Write(hashSalt) + if verify { + h.buf = new(bytes.Buffer) + } + return h +} + +// Write writes data to the running hash. +func (h *Hash) Write(b []byte) (int, error) { + if debugHash { + fmt.Fprintf(os.Stderr, "HASH[%s]: %q\n", h.name, b) + } + if h.buf != nil { + h.buf.Write(b) + } + return h.h.Write(b) +} + +// Sum returns the hash of the data written previously. +func (h *Hash) Sum() [HashSize]byte { + var out [HashSize]byte + h.h.Sum(out[:0]) + if debugHash { + fmt.Fprintf(os.Stderr, "HASH[%s]: %x\n", h.name, out) + } + if h.buf != nil { + hashDebug.Lock() + if hashDebug.m == nil { + hashDebug.m = make(map[[HashSize]byte]string) + } + hashDebug.m[out] = h.buf.String() + hashDebug.Unlock() + } + return out +} + +// In GODEBUG=gocacheverify=1 mode, +// hashDebug holds the input to every computed hash ID, +// so that we can work backward from the ID involved in a +// cache entry mismatch to a description of what should be there. +var hashDebug struct { + sync.Mutex + m map[[HashSize]byte]string +} + +// reverseHash returns the input used to compute the hash id. +func reverseHash(id [HashSize]byte) string { + hashDebug.Lock() + s := hashDebug.m[id] + hashDebug.Unlock() + return s +} + +var hashFileCache struct { + sync.Mutex + m map[string][HashSize]byte +} + +// FileHash returns the hash of the named file. +// It caches repeated lookups for a given file, +// and the cache entry for a file can be initialized +// using SetFileHash. +// The hash used by FileHash is not the same as +// the hash used by NewHash. +func FileHash(file string) ([HashSize]byte, error) { + hashFileCache.Lock() + out, ok := hashFileCache.m[file] + hashFileCache.Unlock() + + if ok { + return out, nil + } + + h := sha256.New() + f, err := os.Open(file) + if err != nil { + if debugHash { + fmt.Fprintf(os.Stderr, "HASH %s: %v\n", file, err) + } + return [HashSize]byte{}, err + } + _, err = io.Copy(h, f) + f.Close() + if err != nil { + if debugHash { + fmt.Fprintf(os.Stderr, "HASH %s: %v\n", file, err) + } + return [HashSize]byte{}, err + } + h.Sum(out[:0]) + if debugHash { + fmt.Fprintf(os.Stderr, "HASH %s: %x\n", file, out) + } + + SetFileHash(file, out) + return out, nil +} + +// SetFileHash sets the hash returned by FileHash for file. +func SetFileHash(file string, sum [HashSize]byte) { + hashFileCache.Lock() + if hashFileCache.m == nil { + hashFileCache.m = make(map[string][HashSize]byte) + } + hashFileCache.m[file] = sum + hashFileCache.Unlock() +} diff --git a/internal/cache/hash_test.go b/internal/cache/hash_test.go new file mode 100644 index 000000000000..3bf7143039ca --- /dev/null +++ b/internal/cache/hash_test.go @@ -0,0 +1,52 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cache + +import ( + "fmt" + "io/ioutil" + "os" + "testing" +) + +func TestHash(t *testing.T) { + oldSalt := hashSalt + hashSalt = nil + defer func() { + hashSalt = oldSalt + }() + + h := NewHash("alice") + h.Write([]byte("hello world")) + sum := fmt.Sprintf("%x", h.Sum()) + want := "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9" + if sum != want { + t.Errorf("hash(hello world) = %v, want %v", sum, want) + } +} + +func TestHashFile(t *testing.T) { + f, err := ioutil.TempFile("", "cmd-go-test-") + if err != nil { + t.Fatal(err) + } + name := f.Name() + fmt.Fprintf(f, "hello world") + defer os.Remove(name) + if err := f.Close(); err != nil { + t.Fatal(err) + } + + var h ActionID // make sure hash result is assignable to ActionID + h, err = FileHash(name) + if err != nil { + t.Fatal(err) + } + sum := fmt.Sprintf("%x", h) + want := "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9" + if sum != want { + t.Errorf("hash(hello world) = %v, want %v", sum, want) + } +} diff --git a/internal/errorutil/errors.go b/internal/errorutil/errors.go new file mode 100644 index 000000000000..693620e93dd7 --- /dev/null +++ b/internal/errorutil/errors.go @@ -0,0 +1,21 @@ +package errorutil + +import "fmt" + +// PanicError can be used to not print stacktrace twice +type PanicError struct { + recovered interface{} + stack []byte +} + +func NewPanicError(recovered interface{}, stack []byte) *PanicError { + return &PanicError{recovered: recovered, stack: stack} +} + +func (e PanicError) Error() string { + return fmt.Sprint(e.recovered) +} + +func (e PanicError) Stack() []byte { + return e.stack +} diff --git a/internal/pkgcache/pkgcache.go b/internal/pkgcache/pkgcache.go new file mode 100644 index 000000000000..b2c481b58d24 --- /dev/null +++ b/internal/pkgcache/pkgcache.go @@ -0,0 +1,179 @@ +package pkgcache + +import ( + "bytes" + "encoding/gob" + "encoding/hex" + "fmt" + "runtime" + "sort" + "sync" + + "github.com/golangci/golangci-lint/pkg/logutils" + + "github.com/golangci/golangci-lint/pkg/timeutils" + + "github.com/pkg/errors" + "golang.org/x/tools/go/packages" + + "github.com/golangci/golangci-lint/internal/cache" +) + +// Cache is a per-package data cache. A cached data is invalidated when +// package or it's dependencies change. +type Cache struct { + lowLevelCache *cache.Cache + pkgHashes sync.Map + sw *timeutils.Stopwatch + log logutils.Log // not used now, but may be needed for future debugging purposes + ioSem chan struct{} // semaphore limiting parallel IO +} + +func NewCache(sw *timeutils.Stopwatch, log logutils.Log) (*Cache, error) { + c, err := cache.Default() + if err != nil { + return nil, err + } + return &Cache{ + lowLevelCache: c, + sw: sw, + log: log, + ioSem: make(chan struct{}, runtime.GOMAXPROCS(-1)), + }, nil +} + +func (c *Cache) Trim() { + c.sw.TrackStage("trim", func() { + c.lowLevelCache.Trim() + }) +} + +func (c *Cache) Put(pkg *packages.Package, key string, data interface{}) error { + var err error + buf := &bytes.Buffer{} + c.sw.TrackStage("gob", func() { + err = gob.NewEncoder(buf).Encode(data) + }) + if err != nil { + return errors.Wrap(err, "failed to gob encode") + } + + var aID cache.ActionID + + c.sw.TrackStage("key build", func() { + aID, err = c.pkgActionID(pkg) + if err == nil { + aID = cache.Subkey(aID, key) + } + }) + if err != nil { + return errors.Wrapf(err, "failed to calculate package %s action id", pkg.Name) + } + c.ioSem <- struct{}{} + c.sw.TrackStage("cache io", func() { + err = c.lowLevelCache.PutBytes(aID, buf.Bytes()) + }) + <-c.ioSem + if err != nil { + return errors.Wrapf(err, "failed to save data to low-level cache by key %s for package %s", key, pkg.Name) + } + + return nil +} + +var ErrMissing = errors.New("missing data") + +func (c *Cache) Get(pkg *packages.Package, key string, data interface{}) error { + var aID cache.ActionID + var err error + c.sw.TrackStage("key build", func() { + aID, err = c.pkgActionID(pkg) + if err == nil { + aID = cache.Subkey(aID, key) + } + }) + if err != nil { + return errors.Wrapf(err, "failed to calculate package %s action id", pkg.Name) + } + + var b []byte + c.ioSem <- struct{}{} + c.sw.TrackStage("cache io", func() { + b, _, err = c.lowLevelCache.GetBytes(aID) + }) + <-c.ioSem + if err != nil { + if cache.IsErrMissing(err) { + return ErrMissing + } + return errors.Wrapf(err, "failed to get data from low-level cache by key %s for package %s", key, pkg.Name) + } + + c.sw.TrackStage("gob", func() { + err = gob.NewDecoder(bytes.NewReader(b)).Decode(data) + }) + if err != nil { + return errors.Wrap(err, "failed to gob decode") + } + + return nil +} + +func (c *Cache) pkgActionID(pkg *packages.Package) (cache.ActionID, error) { + hash, err := c.packageHash(pkg) + if err != nil { + return cache.ActionID{}, errors.Wrap(err, "failed to get package hash") + } + + key := cache.NewHash("action ID") + fmt.Fprintf(key, "pkgpath %s\n", pkg.PkgPath) + fmt.Fprintf(key, "pkghash %s\n", hash) + + return key.Sum(), nil +} + +// packageHash computes a package's hash. The hash is based on all Go +// files that make up the package, as well as the hashes of imported +// packages. +func (c *Cache) packageHash(pkg *packages.Package) (string, error) { + cachedHash, ok := c.pkgHashes.Load(pkg) + if ok { + return cachedHash.(string), nil + } + + key := cache.NewHash("package hash") + fmt.Fprintf(key, "pkgpath %s\n", pkg.PkgPath) + for _, f := range pkg.CompiledGoFiles { + c.ioSem <- struct{}{} + h, err := cache.FileHash(f) + <-c.ioSem + if err != nil { + return "", errors.Wrapf(err, "failed to calculate file %s hash", f) + } + fmt.Fprintf(key, "file %s %x\n", f, h) + } + + imps := make([]*packages.Package, 0, len(pkg.Imports)) + for _, imp := range pkg.Imports { + imps = append(imps, imp) + } + sort.Slice(imps, func(i, j int) bool { + return imps[i].PkgPath < imps[j].PkgPath + }) + for _, dep := range imps { + if dep.PkgPath == "unsafe" { + continue + } + + depHash, err := c.packageHash(dep) + if err != nil { + return "", errors.Wrapf(err, "failed to calculate hash for dependency %s", dep.Name) + } + + fmt.Fprintf(key, "import %s %s\n", dep.PkgPath, depHash) + } + h := key.Sum() + ret := hex.EncodeToString(h[:]) + c.pkgHashes.Store(pkg, ret) + return ret, nil +} diff --git a/internal/renameio/renameio.go b/internal/renameio/renameio.go new file mode 100644 index 000000000000..fa9d93bf7ad7 --- /dev/null +++ b/internal/renameio/renameio.go @@ -0,0 +1,93 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package renameio writes files atomically by renaming temporary files. +package renameio + +import ( + "bytes" + "io" + "math/rand" + "os" + "path/filepath" + "strconv" + + "github.com/golangci/golangci-lint/internal/robustio" +) + +const patternSuffix = ".tmp" + +// Pattern returns a glob pattern that matches the unrenamed temporary files +// created when writing to filename. +func Pattern(filename string) string { + return filepath.Join(filepath.Dir(filename), filepath.Base(filename)+patternSuffix) +} + +// WriteFile is like ioutil.WriteFile, but first writes data to an arbitrary +// file in the same directory as filename, then renames it atomically to the +// final name. +// +// That ensures that the final location, if it exists, is always a complete file. +func WriteFile(filename string, data []byte, perm os.FileMode) (err error) { + return WriteToFile(filename, bytes.NewReader(data), perm) +} + +// WriteToFile is a variant of WriteFile that accepts the data as an io.Reader +// instead of a slice. +func WriteToFile(filename string, data io.Reader, perm os.FileMode) (err error) { + f, err := tempFile(filepath.Dir(filename), filepath.Base(filename), perm) + if err != nil { + return err + } + defer func() { + // Only call os.Remove on f.Name() if we failed to rename it: otherwise, + // some other process may have created a new file with the same name after + // that. + if err != nil { + f.Close() + os.Remove(f.Name()) + } + }() + + if _, err := io.Copy(f, data); err != nil { + return err + } + // Sync the file before renaming it: otherwise, after a crash the reader may + // observe a 0-length file instead of the actual contents. + // See https://golang.org/issue/22397#issuecomment-380831736. + if err := f.Sync(); err != nil { + return err + } + if err := f.Close(); err != nil { + return err + } + + return robustio.Rename(f.Name(), filename) +} + +// tempFile creates a new temporary file with given permission bits. +func tempFile(dir, prefix string, perm os.FileMode) (f *os.File, err error) { + for i := 0; i < 10000; i++ { + name := filepath.Join(dir, prefix+strconv.Itoa(rand.Intn(1000000000))+patternSuffix) + f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, perm) + if os.IsExist(err) { + continue + } + break + } + return +} + +// ReadFile is like ioutil.ReadFile, but on Windows retries spurious errors that +// may occur if the file is concurrently replaced. +// +// Errors are classified heuristically and retries are bounded, so even this +// function may occasionally return a spurious error on Windows. +// If so, the error will likely wrap one of: +// - syscall.ERROR_ACCESS_DENIED +// - syscall.ERROR_FILE_NOT_FOUND +// - internal/syscall/windows.ERROR_SHARING_VIOLATION +func ReadFile(filename string) ([]byte, error) { + return robustio.ReadFile(filename) +} diff --git a/internal/renameio/renameio_test.go b/internal/renameio/renameio_test.go new file mode 100644 index 000000000000..01ec9cee80ab --- /dev/null +++ b/internal/renameio/renameio_test.go @@ -0,0 +1,142 @@ +// Copyright 2019 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//+build !plan9 + +package renameio + +import ( + "encoding/binary" + "io/ioutil" + "math/rand" + "os" + "path/filepath" + "runtime" + "sync" + "sync/atomic" + "syscall" + "testing" + "time" + + "github.com/golangci/golangci-lint/internal/robustio" +) + +func TestConcurrentReadsAndWrites(t *testing.T) { + dir, err := ioutil.TempDir("", "renameio") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + path := filepath.Join(dir, "blob.bin") + + const chunkWords = 8 << 10 + buf := make([]byte, 2*chunkWords*8) + for i := uint64(0); i < 2*chunkWords; i++ { + binary.LittleEndian.PutUint64(buf[i*8:], i) + } + + var attempts int64 = 128 + if !testing.Short() { + attempts *= 16 + } + const parallel = 32 + + var sem = make(chan bool, parallel) + + var ( + writeSuccesses, readSuccesses int64 // atomic + writeErrnoSeen, readErrnoSeen sync.Map + ) + + for n := attempts; n > 0; n-- { + sem <- true + go func() { + defer func() { <-sem }() + + time.Sleep(time.Duration(rand.Intn(100)) * time.Microsecond) + offset := rand.Intn(chunkWords) + chunk := buf[offset*8 : (offset+chunkWords)*8] + if err := WriteFile(path, chunk, 0666); err == nil { + atomic.AddInt64(&writeSuccesses, 1) + } else if robustio.IsEphemeralError(err) { + var ( + dup bool + ) + if errno, ok := err.(syscall.Errno); ok { + _, dup = writeErrnoSeen.LoadOrStore(errno, true) + } + if !dup { + t.Logf("ephemeral error: %v", err) + } + } else { + t.Errorf("unexpected error: %v", err) + } + + time.Sleep(time.Duration(rand.Intn(100)) * time.Microsecond) + data, err := ReadFile(path) + if err == nil { + atomic.AddInt64(&readSuccesses, 1) + } else if robustio.IsEphemeralError(err) { + var ( + dup bool + ) + if errno, ok := err.(syscall.Errno); ok { + _, dup = readErrnoSeen.LoadOrStore(errno, true) + } + if !dup { + t.Logf("ephemeral error: %v", err) + } + return + } else { + t.Errorf("unexpected error: %v", err) + return + } + + if len(data) != 8*chunkWords { + t.Errorf("read %d bytes, but each write is a %d-byte file", len(data), 8*chunkWords) + return + } + + u := binary.LittleEndian.Uint64(data) + for i := 1; i < chunkWords; i++ { + next := binary.LittleEndian.Uint64(data[i*8:]) + if next != u+1 { + t.Errorf("wrote sequential integers, but read integer out of sequence at offset %d", i) + return + } + u = next + } + }() + } + + for n := parallel; n > 0; n-- { + sem <- true + } + + var minWriteSuccesses int64 = attempts + if runtime.GOOS == "windows" { + // Windows produces frequent "Access is denied" errors under heavy rename load. + // As long as those are the only errors and *some* of the writes succeed, we're happy. + minWriteSuccesses = attempts / 4 + } + + if writeSuccesses < minWriteSuccesses { + t.Errorf("%d (of %d) writes succeeded; want ≥ %d", writeSuccesses, attempts, minWriteSuccesses) + } else { + t.Logf("%d (of %d) writes succeeded (ok: ≥ %d)", writeSuccesses, attempts, minWriteSuccesses) + } + + var minReadSuccesses int64 = attempts + if runtime.GOOS == "windows" { + // Windows produces frequent "Access is denied" errors under heavy rename load. + // As long as those are the only errors and *some* of the writes succeed, we're happy. + minReadSuccesses = attempts / 4 + } + + if readSuccesses < minReadSuccesses { + t.Errorf("%d (of %d) reads succeeded; want ≥ %d", readSuccesses, attempts, minReadSuccesses) + } else { + t.Logf("%d (of %d) reads succeeded (ok: ≥ %d)", readSuccesses, attempts, minReadSuccesses) + } +} diff --git a/internal/renameio/umask_test.go b/internal/renameio/umask_test.go new file mode 100644 index 000000000000..1a471c9e4e64 --- /dev/null +++ b/internal/renameio/umask_test.go @@ -0,0 +1,42 @@ +// Copyright 2019 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//+build !nacl,!plan9,!windows,!js + +package renameio + +import ( + "io/ioutil" + "os" + "path/filepath" + "syscall" + "testing" +) + +func TestWriteFileModeAppliesUmask(t *testing.T) { + dir, err := ioutil.TempDir("", "renameio") + if err != nil { + t.Fatalf("Failed to create temporary directory: %v", err) + } + + const mode = 0644 + const umask = 0007 + defer syscall.Umask(syscall.Umask(umask)) + + file := filepath.Join(dir, "testWrite") + err = WriteFile(file, []byte("go-build"), mode) + if err != nil { + t.Fatalf("Failed to write file: %v", err) + } + defer os.RemoveAll(dir) + + fi, err := os.Stat(file) + if err != nil { + t.Fatalf("Stat %q (looking for mode %#o): %s", file, mode, err) + } + + if fi.Mode()&os.ModePerm != 0640 { + t.Errorf("Stat %q: mode %#o want %#o", file, fi.Mode()&os.ModePerm, 0640) + } +} diff --git a/internal/robustio/robustio.go b/internal/robustio/robustio.go new file mode 100644 index 000000000000..76e47ad1ffad --- /dev/null +++ b/internal/robustio/robustio.go @@ -0,0 +1,53 @@ +// Copyright 2019 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package robustio wraps I/O functions that are prone to failure on Windows, +// transparently retrying errors up to an arbitrary timeout. +// +// Errors are classified heuristically and retries are bounded, so the functions +// in this package do not completely eliminate spurious errors. However, they do +// significantly reduce the rate of failure in practice. +// +// If so, the error will likely wrap one of: +// The functions in this package do not completely eliminate spurious errors, +// but substantially reduce their rate of occurrence in practice. +package robustio + +// Rename is like os.Rename, but on Windows retries errors that may occur if the +// file is concurrently read or overwritten. +// +// (See golang.org/issue/31247 and golang.org/issue/32188.) +func Rename(oldpath, newpath string) error { + return rename(oldpath, newpath) +} + +// ReadFile is like ioutil.ReadFile, but on Windows retries errors that may +// occur if the file is concurrently replaced. +// +// (See golang.org/issue/31247 and golang.org/issue/32188.) +func ReadFile(filename string) ([]byte, error) { + return readFile(filename) +} + +// RemoveAll is like os.RemoveAll, but on Windows retries errors that may occur +// if an executable file in the directory has recently been executed. +// +// (See golang.org/issue/19491.) +func RemoveAll(path string) error { + return removeAll(path) +} + +// IsEphemeralError reports whether err is one of the errors that the functions +// in this package attempt to mitigate. +// +// Errors considered ephemeral include: +// - syscall.ERROR_ACCESS_DENIED +// - syscall.ERROR_FILE_NOT_FOUND +// - internal/syscall/windows.ERROR_SHARING_VIOLATION +// +// This set may be expanded in the future; programs must not rely on the +// non-ephemerality of any given error. +func IsEphemeralError(err error) bool { + return isEphemeralError(err) +} diff --git a/internal/robustio/robustio_other.go b/internal/robustio/robustio_other.go new file mode 100644 index 000000000000..91ca56cb82ca --- /dev/null +++ b/internal/robustio/robustio_other.go @@ -0,0 +1,28 @@ +// Copyright 2019 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//+build !windows + +package robustio + +import ( + "io/ioutil" + "os" +) + +func rename(oldpath, newpath string) error { + return os.Rename(oldpath, newpath) +} + +func readFile(filename string) ([]byte, error) { + return ioutil.ReadFile(filename) +} + +func removeAll(path string) error { + return os.RemoveAll(path) +} + +func isEphemeralError(err error) bool { + return false +} diff --git a/internal/robustio/robustio_windows.go b/internal/robustio/robustio_windows.go new file mode 100644 index 000000000000..02f1e8c1ce6e --- /dev/null +++ b/internal/robustio/robustio_windows.go @@ -0,0 +1,104 @@ +// Copyright 2019 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package robustio + +import ( + "io/ioutil" + "math/rand" + "os" + "syscall" + "time" +) + +const arbitraryTimeout = 500 * time.Millisecond + +const ERROR_SHARING_VIOLATION = 32 + +// retry retries ephemeral errors from f up to an arbitrary timeout +// to work around spurious filesystem errors on Windows +func retry(f func() (err error, mayRetry bool)) error { + var ( + bestErr error + lowestErrno syscall.Errno + start time.Time + nextSleep time.Duration = 1 * time.Millisecond + ) + for { + err, mayRetry := f() + if err == nil || !mayRetry { + return err + } + + if errno, ok := err.(syscall.Errno); ok && (lowestErrno == 0 || errno < lowestErrno) { + bestErr = err + lowestErrno = errno + } else if bestErr == nil { + bestErr = err + } + + if start.IsZero() { + start = time.Now() + } else if d := time.Since(start) + nextSleep; d >= arbitraryTimeout { + break + } + time.Sleep(nextSleep) + nextSleep += time.Duration(rand.Int63n(int64(nextSleep))) + } + + return bestErr +} + +// rename is like os.Rename, but retries ephemeral errors. +// +// It wraps os.Rename, which (as of 2019-06-04) uses MoveFileEx with +// MOVEFILE_REPLACE_EXISTING. +// +// Windows also provides a different system call, ReplaceFile, +// that provides similar semantics, but perhaps preserves more metadata. (The +// documentation on the differences between the two is very sparse.) +// +// Empirical error rates with MoveFileEx are lower under modest concurrency, so +// for now we're sticking with what the os package already provides. +func rename(oldpath, newpath string) (err error) { + return retry(func() (err error, mayRetry bool) { + err = os.Rename(oldpath, newpath) + return err, isEphemeralError(err) + }) +} + +// readFile is like ioutil.ReadFile, but retries ephemeral errors. +func readFile(filename string) ([]byte, error) { + var b []byte + err := retry(func() (err error, mayRetry bool) { + b, err = ioutil.ReadFile(filename) + + // Unlike in rename, we do not retry ERROR_FILE_NOT_FOUND here: it can occur + // as a spurious error, but the file may also genuinely not exist, so the + // increase in robustness is probably not worth the extra latency. + + return err, isEphemeralError(err) && err != syscall.ERROR_FILE_NOT_FOUND + }) + return b, err +} + +func removeAll(path string) error { + return retry(func() (err error, mayRetry bool) { + err = os.RemoveAll(path) + return err, isEphemeralError(err) + }) +} + +// isEphemeralError returns true if err may be resolved by waiting. +func isEphemeralError(err error) bool { + if errno, ok := err.(syscall.Errno); ok { + switch errno { + case syscall.ERROR_ACCESS_DENIED, + syscall.ERROR_FILE_NOT_FOUND, + ERROR_SHARING_VIOLATION: + return true + } + } + return false +} diff --git a/pkg/commands/executor.go b/pkg/commands/executor.go index bfe6476cf9de..0fad8f766120 100644 --- a/pkg/commands/executor.go +++ b/pkg/commands/executor.go @@ -5,6 +5,11 @@ import ( "github.com/spf13/cobra" "github.com/spf13/pflag" + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis/load" + + "github.com/golangci/golangci-lint/internal/pkgcache" + "github.com/golangci/golangci-lint/pkg/timeutils" + "github.com/golangci/golangci-lint/pkg/fsutils" "github.com/golangci/golangci-lint/pkg/config" @@ -31,7 +36,11 @@ type Executor struct { goenv *goutil.Env fileCache *fsutils.FileCache lineCache *fsutils.LineCache + pkgCache *pkgcache.Cache debugf logutils.DebugFunc + sw *timeutils.Stopwatch + + loadGuard *load.Guard } func NewExecutor(version, commit, date string) *Executor { @@ -82,7 +91,7 @@ func NewExecutor(version, commit, date string) *Executor { // is found in command-line: it's ok, command-line has higher priority. r := config.NewFileReader(e.cfg, commandLineCfg, e.log.Child("config_reader")) - if err := r.Read(); err != nil { + if err = r.Read(); err != nil { e.log.Fatalf("Can't read config: %s", err) } @@ -90,7 +99,7 @@ func NewExecutor(version, commit, date string) *Executor { e.DBManager = lintersdb.NewManager(e.cfg) e.cfg.LintersSettings.Gocritic.InferEnabledChecks(e.log) - if err := e.cfg.LintersSettings.Gocritic.Validate(e.log); err != nil { + if err = e.cfg.LintersSettings.Gocritic.Validate(e.log); err != nil { e.log.Fatalf("Invalid gocritic settings: %s", err) } @@ -102,13 +111,19 @@ func NewExecutor(version, commit, date string) *Executor { e.goenv = goutil.NewEnv(e.log.Child("goenv")) e.fileCache = fsutils.NewFileCache() e.lineCache = fsutils.NewLineCache(e.fileCache) - e.contextLoader = lint.NewContextLoader(e.cfg, e.log.Child("loader"), e.goenv, e.lineCache, e.fileCache) + + e.sw = timeutils.NewStopwatch("pkgcache", e.log.Child("stopwatch")) + e.pkgCache, err = pkgcache.NewCache(e.sw, e.log.Child("pkgcache")) + if err != nil { + e.log.Fatalf("Failed to build packages cache: %s", err) + } + e.loadGuard = load.NewGuard() + e.contextLoader = lint.NewContextLoader(e.cfg, e.log.Child("loader"), e.goenv, + e.lineCache, e.fileCache, e.pkgCache, e.loadGuard) e.debugf("Initialized executor") return e } func (e *Executor) Execute() error { - err := e.rootCmd.Execute() - e.debugf("Finished execution") - return err + return e.rootCmd.Execute() } diff --git a/pkg/commands/help.go b/pkg/commands/help.go index 4e5ff6e0fcbb..16fef33ebc2c 100644 --- a/pkg/commands/help.go +++ b/pkg/commands/help.go @@ -46,7 +46,7 @@ func printLinterConfigs(lcs []*linter.Config) { altNamesStr = fmt.Sprintf(" (%s)", strings.Join(lc.AlternativeNames, ", ")) } fmt.Fprintf(logutils.StdOut, "%s%s: %s [fast: %t, auto-fix: %t]\n", color.YellowString(lc.Name()), - altNamesStr, lc.Linter.Desc(), !lc.NeedsDepsTypeInfo, lc.CanAutoFix) + altNamesStr, lc.Linter.Desc(), !lc.IsSlowLinter(), lc.CanAutoFix) } } diff --git a/pkg/golinters/goanalysis/checker/checker.go b/pkg/golinters/goanalysis/checker/checker.go deleted file mode 100644 index 4173c9b08e5f..000000000000 --- a/pkg/golinters/goanalysis/checker/checker.go +++ /dev/null @@ -1,586 +0,0 @@ -// checker is a partial copy of https://github.com/golang/tools/blob/master/go/analysis/internal/checker -// Copyright 2018 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package checker defines the implementation of the checker commands. -// The same code drives the multi-analysis driver, the single-analysis -// driver that is conventionally provided for convenience along with -// each analysis package, and the test driver. -package checker - -import ( - "bytes" - "encoding/gob" - "fmt" - "go/token" - "go/types" - "log" - "os" - "reflect" - "runtime" - "runtime/debug" - "runtime/pprof" - "runtime/trace" - "sort" - "strings" - "sync" - "time" - - "github.com/pkg/errors" - - "golang.org/x/tools/go/analysis" - "golang.org/x/tools/go/packages" -) - -var ( - // Debug is a set of single-letter flags: - // - // f show [f]acts as they are created - // p disable [p]arallel execution of analyzers - // s do additional [s]anity checks on fact types and serialization - // t show [t]iming info (NB: use 'p' flag to avoid GC/scheduler noise) - // v show [v]erbose logging - // - Debug = os.Getenv("GL_DEBUG_GO_ANALYSIS") - - // Log files for optional performance tracing. - CPUProfile, MemProfile, Trace string -) - -type Diagnostic struct { - analysis.Diagnostic - AnalyzerName string - Position token.Position -} - -// Run loads the packages specified by args using go/packages, -// then applies the specified analyzers to them. -// Analysis flags must already have been set. -// It provides most of the logic for the main functions of both the -// singlechecker and the multi-analysis commands. -// It returns the appropriate exit code. -//nolint:gocyclo -func Run(analyzers []*analysis.Analyzer, initialPackages []*packages.Package) ([]Diagnostic, []error) { - if CPUProfile != "" { - f, err := os.Create(CPUProfile) - if err != nil { - log.Fatal(err) - } - if err := pprof.StartCPUProfile(f); err != nil { - log.Fatal(err) - } - // NB: profile won't be written in case of error. - defer pprof.StopCPUProfile() - } - - if Trace != "" { - f, err := os.Create(Trace) - if err != nil { - log.Fatal(err) - } - if err := trace.Start(f); err != nil { - log.Fatal(err) - } - // NB: trace log won't be written in case of error. - defer func() { - trace.Stop() - log.Printf("To view the trace, run:\n$ go tool trace view %s", Trace) - }() - } - - if MemProfile != "" { - f, err := os.Create(MemProfile) - if err != nil { - log.Fatal(err) - } - // NB: memprofile won't be written in case of error. - defer func() { - runtime.GC() // get up-to-date statistics - if err := pprof.WriteHeapProfile(f); err != nil { - log.Fatalf("Writing memory profile: %v", err) - } - f.Close() - }() - } - - // Load the packages. - if dbg('v') { - log.SetPrefix("") - log.SetFlags(log.Lmicroseconds) // display timing - log.Printf("load %d packages", len(initialPackages)) - } - - // Print the results. - roots := analyze(initialPackages, analyzers) - - return extractDiagnostics(roots) -} - -func analyze(pkgs []*packages.Package, analyzers []*analysis.Analyzer) []*action { - // Construct the action graph. - if dbg('v') { - log.Printf("building graph of analysis passes") - } - - // Each graph node (action) is one unit of analysis. - // Edges express package-to-package (vertical) dependencies, - // and analysis-to-analysis (horizontal) dependencies. - type key struct { - *analysis.Analyzer - *packages.Package - } - actions := make(map[key]*action) - - var mkAction func(a *analysis.Analyzer, pkg *packages.Package) *action - mkAction = func(a *analysis.Analyzer, pkg *packages.Package) *action { - k := key{a, pkg} - act, ok := actions[k] - if !ok { - act = &action{a: a, pkg: pkg} - - // Add a dependency on each required analyzers. - for _, req := range a.Requires { - act.deps = append(act.deps, mkAction(req, pkg)) - } - - // An analysis that consumes/produces facts - // must run on the package's dependencies too. - if len(a.FactTypes) > 0 { - paths := make([]string, 0, len(pkg.Imports)) - for path := range pkg.Imports { - paths = append(paths, path) - } - sort.Strings(paths) // for determinism - for _, path := range paths { - dep := mkAction(a, pkg.Imports[path]) - act.deps = append(act.deps, dep) - } - } - - actions[k] = act - } - return act - } - - // Build nodes for initial packages. - var roots []*action - for _, a := range analyzers { - for _, pkg := range pkgs { - root := mkAction(a, pkg) - root.isroot = true - roots = append(roots, root) - } - } - - // Execute the graph in parallel. - execAll(roots) - - return roots -} - -func extractDiagnostics(roots []*action) (retDiags []Diagnostic, retErrors []error) { - extracted := make(map[*action]bool) - var extract func(*action) - var visitAll func(actions []*action) - visitAll = func(actions []*action) { - for _, act := range actions { - if !extracted[act] { - extracted[act] = true - visitAll(act.deps) - extract(act) - } - } - } - - // De-duplicate diagnostics by position (not token.Pos) to - // avoid double-reporting in source files that belong to - // multiple packages, such as foo and foo.test. - type key struct { - token.Position - *analysis.Analyzer - message string - } - seen := make(map[key]bool) - - extract = func(act *action) { - if act.err != nil { - retErrors = append(retErrors, errors.Wrap(act.err, act.a.Name)) - return - } - - if act.isroot { - for _, diag := range act.diagnostics { - // We don't display a.Name/f.Category - // as most users don't care. - - posn := act.pkg.Fset.Position(diag.Pos) - k := key{posn, act.a, diag.Message} - if seen[k] { - continue // duplicate - } - seen[k] = true - - retDiags = append(retDiags, Diagnostic{Diagnostic: diag, AnalyzerName: act.a.Name, Position: posn}) - } - } - } - visitAll(roots) - return -} - -// NeedFacts reports whether any analysis required by the specified set -// needs facts. If so, we must load the entire program from source. -func NeedFacts(analyzers []*analysis.Analyzer) bool { - seen := make(map[*analysis.Analyzer]bool) - var q []*analysis.Analyzer // for BFS - q = append(q, analyzers...) - for len(q) > 0 { - a := q[0] - q = q[1:] - if !seen[a] { - seen[a] = true - if len(a.FactTypes) > 0 { - return true - } - q = append(q, a.Requires...) - } - } - return false -} - -// An action represents one unit of analysis work: the application of -// one analysis to one package. Actions form a DAG, both within a -// package (as different analyzers are applied, either in sequence or -// parallel), and across packages (as dependencies are analyzed). -type action struct { - once sync.Once - a *analysis.Analyzer - pkg *packages.Package - pass *analysis.Pass - isroot bool - deps []*action - objectFacts map[objectFactKey]analysis.Fact - packageFacts map[packageFactKey]analysis.Fact - inputs map[*analysis.Analyzer]interface{} - result interface{} - diagnostics []analysis.Diagnostic - err error - duration time.Duration -} - -type objectFactKey struct { - obj types.Object - typ reflect.Type -} - -type packageFactKey struct { - pkg *types.Package - typ reflect.Type -} - -func (act *action) String() string { - return fmt.Sprintf("%s@%s", act.a, act.pkg) -} - -func execAll(actions []*action) { - sequential := dbg('p') - var wg sync.WaitGroup - panics := make([]interface{}, len(actions)) - for i, act := range actions { - wg.Add(1) - work := func(act *action) { - defer func() { - wg.Done() - if p := recover(); p != nil { - panics[i] = fmt.Errorf("%s: %s", p, debug.Stack()) - } - }() - act.exec() - } - if sequential { - work(act) - } else { - go work(act) - } - } - wg.Wait() - for _, p := range panics { - if p != nil { - panic(p) - } - } -} - -func (act *action) exec() { act.once.Do(act.execOnce) } - -func (act *action) execOnce() { - // Analyze dependencies. - execAll(act.deps) - - // TODO(adonovan): uncomment this during profiling. - // It won't build pre-go1.11 but conditional compilation - // using build tags isn't warranted. - // - // ctx, task := trace.NewTask(context.Background(), "exec") - // trace.Log(ctx, "pass", act.String()) - // defer task.End() - - // Record time spent in this node but not its dependencies. - // In parallel mode, due to GC/scheduler contention, the - // time is 5x higher than in sequential mode, even with a - // semaphore limiting the number of threads here. - // So use -debug=tp. - if dbg('t') { - t0 := time.Now() - defer func() { act.duration = time.Since(t0) }() - } - - // Report an error if any dependency failed. - var failed []string - for _, dep := range act.deps { - if dep.err != nil { - failed = append(failed, dep.String()) - } - } - if failed != nil { - sort.Strings(failed) - act.err = fmt.Errorf("failed prerequisites: %s", strings.Join(failed, ", ")) - return - } - - // Plumb the output values of the dependencies - // into the inputs of this action. Also facts. - inputs := make(map[*analysis.Analyzer]interface{}) - act.objectFacts = make(map[objectFactKey]analysis.Fact) - act.packageFacts = make(map[packageFactKey]analysis.Fact) - for _, dep := range act.deps { - if dep.pkg == act.pkg { - // Same package, different analysis (horizontal edge): - // in-memory outputs of prerequisite analyzers - // become inputs to this analysis pass. - inputs[dep.a] = dep.result - } else if dep.a == act.a { // (always true) - // Same analysis, different package (vertical edge): - // serialized facts produced by prerequisite analysis - // become available to this analysis pass. - inheritFacts(act, dep) - } - } - - // Run the analysis. - pass := &analysis.Pass{ - Analyzer: act.a, - Fset: act.pkg.Fset, - Files: act.pkg.Syntax, - OtherFiles: act.pkg.OtherFiles, - Pkg: act.pkg.Types, - TypesInfo: act.pkg.TypesInfo, - TypesSizes: act.pkg.TypesSizes, - ResultOf: inputs, - Report: func(d analysis.Diagnostic) { act.diagnostics = append(act.diagnostics, d) }, - ImportObjectFact: act.importObjectFact, - ExportObjectFact: act.exportObjectFact, - ImportPackageFact: act.importPackageFact, - ExportPackageFact: act.exportPackageFact, - } - act.pass = pass - - var err error - if act.pkg.IllTyped && !pass.Analyzer.RunDespiteErrors { - err = fmt.Errorf("analysis skipped due to errors in package") - } else { - act.result, err = pass.Analyzer.Run(pass) - if err == nil { - if got, want := reflect.TypeOf(act.result), pass.Analyzer.ResultType; got != want { - err = fmt.Errorf( - "internal error: on package %s, analyzer %s returned a result of type %v, but declared ResultType %v", - pass.Pkg.Path(), pass.Analyzer, got, want) - } - } - } - act.err = err - - // disallow calls after Run - pass.ExportObjectFact = nil - pass.ExportPackageFact = nil -} - -// inheritFacts populates act.facts with -// those it obtains from its dependency, dep. -func inheritFacts(act, dep *action) { - serialize := dbg('s') - - for key, fact := range dep.objectFacts { - // Filter out facts related to objects - // that are irrelevant downstream - // (equivalently: not in the compiler export data). - if !exportedFrom(key.obj, dep.pkg.Types) { - if false { - log.Printf("%v: discarding %T fact from %s for %s: %s", act, fact, dep, key.obj, fact) - } - continue - } - - // Optionally serialize/deserialize fact - // to verify that it works across address spaces. - if serialize { - var err error - fact, err = codeFact(fact) - if err != nil { - log.Panicf("internal error: encoding of %T fact failed in %v", fact, act) - } - } - - if false { - log.Printf("%v: inherited %T fact for %s: %s", act, fact, key.obj, fact) - } - act.objectFacts[key] = fact - } - - for key, fact := range dep.packageFacts { - // TODO: filter out facts that belong to - // packages not mentioned in the export data - // to prevent side channels. - - // Optionally serialize/deserialize fact - // to verify that it works across address spaces - // and is deterministic. - if serialize { - var err error - fact, err = codeFact(fact) - if err != nil { - log.Panicf("internal error: encoding of %T fact failed in %v", fact, act) - } - } - - if false { - log.Printf("%v: inherited %T fact for %s: %s", act, fact, key.pkg.Path(), fact) - } - act.packageFacts[key] = fact - } -} - -// codeFact encodes then decodes a fact, -// just to exercise that logic. -func codeFact(fact analysis.Fact) (analysis.Fact, error) { - // We encode facts one at a time. - // A real modular driver would emit all facts - // into one encoder to improve gob efficiency. - var buf bytes.Buffer - if err := gob.NewEncoder(&buf).Encode(fact); err != nil { - return nil, err - } - - // Encode it twice and assert that we get the same bits. - // This helps detect nondeterministic Gob encoding (e.g. of maps). - var buf2 bytes.Buffer - if err := gob.NewEncoder(&buf2).Encode(fact); err != nil { - return nil, err - } - if !bytes.Equal(buf.Bytes(), buf2.Bytes()) { - return nil, fmt.Errorf("encoding of %T fact is nondeterministic", fact) - } - - new := reflect.New(reflect.TypeOf(fact).Elem()).Interface().(analysis.Fact) - if err := gob.NewDecoder(&buf).Decode(new); err != nil { - return nil, err - } - return new, nil -} - -// exportedFrom reports whether obj may be visible to a package that imports pkg. -// This includes not just the exported members of pkg, but also unexported -// constants, types, fields, and methods, perhaps belonging to oether packages, -// that find there way into the API. -// This is an overapproximation of the more accurate approach used by -// gc export data, which walks the type graph, but it's much simpler. -// -// TODO(adonovan): do more accurate filtering by walking the type graph. -func exportedFrom(obj types.Object, pkg *types.Package) bool { - switch obj := obj.(type) { - case *types.Func: - return obj.Exported() && obj.Pkg() == pkg || - obj.Type().(*types.Signature).Recv() != nil - case *types.Var: - return obj.Exported() && obj.Pkg() == pkg || - obj.IsField() - case *types.TypeName, *types.Const: - return true - } - return false // Nil, Builtin, Label, or PkgName -} - -// importObjectFact implements Pass.ImportObjectFact. -// Given a non-nil pointer ptr of type *T, where *T satisfies Fact, -// importObjectFact copies the fact value to *ptr. -func (act *action) importObjectFact(obj types.Object, ptr analysis.Fact) bool { - if obj == nil { - panic("nil object") - } - key := objectFactKey{obj, factType(ptr)} - if v, ok := act.objectFacts[key]; ok { - reflect.ValueOf(ptr).Elem().Set(reflect.ValueOf(v).Elem()) - return true - } - return false -} - -// exportObjectFact implements Pass.ExportObjectFact. -func (act *action) exportObjectFact(obj types.Object, fact analysis.Fact) { - if act.pass.ExportObjectFact == nil { - log.Panicf("%s: Pass.ExportObjectFact(%s, %T) called after Run", act, obj, fact) - } - - if obj.Pkg() != act.pkg.Types { - log.Panicf("internal error: in analysis %s of package %s: Fact.Set(%s, %T): can't set facts on objects belonging another package", - act.a, act.pkg, obj, fact) - } - - key := objectFactKey{obj, factType(fact)} - act.objectFacts[key] = fact // clobber any existing entry - if dbg('f') { - objstr := types.ObjectString(obj, (*types.Package).Name) - fmt.Fprintf(os.Stderr, "%s: object %s has fact %s\n", - act.pkg.Fset.Position(obj.Pos()), objstr, fact) - } -} - -// importPackageFact implements Pass.ImportPackageFact. -// Given a non-nil pointer ptr of type *T, where *T satisfies Fact, -// fact copies the fact value to *ptr. -func (act *action) importPackageFact(pkg *types.Package, ptr analysis.Fact) bool { - if pkg == nil { - panic("nil package") - } - key := packageFactKey{pkg, factType(ptr)} - if v, ok := act.packageFacts[key]; ok { - reflect.ValueOf(ptr).Elem().Set(reflect.ValueOf(v).Elem()) - return true - } - return false -} - -// exportPackageFact implements Pass.ExportPackageFact. -func (act *action) exportPackageFact(fact analysis.Fact) { - if act.pass.ExportPackageFact == nil { - log.Panicf("%s: Pass.ExportPackageFact(%T) called after Run", act, fact) - } - - key := packageFactKey{act.pass.Pkg, factType(fact)} - act.packageFacts[key] = fact // clobber any existing entry - if dbg('f') { - fmt.Fprintf(os.Stderr, "%s: package %s has fact %s\n", - act.pkg.Fset.Position(act.pass.Files[0].Pos()), act.pass.Pkg.Path(), fact) - } -} - -func factType(fact analysis.Fact) reflect.Type { - t := reflect.TypeOf(fact) - if t.Kind() != reflect.Ptr { - log.Fatalf("invalid Fact type: got %T, want pointer", t) - } - return t -} - -func dbg(b byte) bool { return strings.IndexByte(Debug, b) >= 0 } diff --git a/pkg/golinters/goanalysis/interface.go b/pkg/golinters/goanalysis/interface.go new file mode 100644 index 000000000000..afe6219d72d1 --- /dev/null +++ b/pkg/golinters/goanalysis/interface.go @@ -0,0 +1,11 @@ +package goanalysis + +import ( + "golang.org/x/tools/go/analysis" +) + +type SupportedLinter interface { + Analyzers() []*analysis.Analyzer + Cfg() map[string]map[string]interface{} + AnalyzerToLinterNameMapping() map[*analysis.Analyzer]string +} diff --git a/pkg/golinters/goanalysis/linter.go b/pkg/golinters/goanalysis/linter.go index fc36c2961c3f..a9487bc0cb26 100644 --- a/pkg/golinters/goanalysis/linter.go +++ b/pkg/golinters/goanalysis/linter.go @@ -9,7 +9,6 @@ import ( "github.com/pkg/errors" "golang.org/x/tools/go/analysis" - "github.com/golangci/golangci-lint/pkg/golinters/goanalysis/checker" "github.com/golangci/golangci-lint/pkg/lint/linter" "github.com/golangci/golangci-lint/pkg/result" ) @@ -115,7 +114,9 @@ func (lnt Linter) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Is return nil, errors.Wrap(err, "failed to configure analyzers") } - diags, errs := checker.Run(lnt.analyzers, lintCtx.Packages) + runner := newRunner(lnt.name, lintCtx.Log.Child("goanalysis"), lintCtx.PkgCache, lintCtx.LoadGuard) + + diags, errs := runner.run(lnt.analyzers, lintCtx.Packages) for i := 1; i < len(errs); i++ { lintCtx.Log.Warnf("%s error: %s", lnt.Name(), errs[i]) } @@ -128,10 +129,26 @@ func (lnt Linter) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Is diag := &diags[i] issues = append(issues, result.Issue{ FromLinter: lnt.Name(), - Text: fmt.Sprintf("%s: %s", diag.AnalyzerName, diag.Message), + Text: fmt.Sprintf("%s: %s", diag.Analyzer.Name, diag.Message), Pos: diag.Position, }) } return issues, nil } + +func (lnt Linter) Analyzers() []*analysis.Analyzer { + return lnt.analyzers +} + +func (lnt Linter) Cfg() map[string]map[string]interface{} { + return lnt.cfg +} + +func (lnt Linter) AnalyzerToLinterNameMapping() map[*analysis.Analyzer]string { + ret := map[*analysis.Analyzer]string{} + for _, a := range lnt.analyzers { + ret[a] = lnt.Name() + } + return ret +} diff --git a/pkg/golinters/goanalysis/load/guard.go b/pkg/golinters/goanalysis/load/guard.go new file mode 100644 index 000000000000..5695f717eb9b --- /dev/null +++ b/pkg/golinters/goanalysis/load/guard.go @@ -0,0 +1,35 @@ +package load + +import ( + "sync" + + "golang.org/x/tools/go/packages" +) + +type Guard struct { + loadMutexes map[*packages.Package]*sync.Mutex + mutexForExportData sync.Mutex + mutex sync.Mutex +} + +func NewGuard() *Guard { + return &Guard{ + loadMutexes: map[*packages.Package]*sync.Mutex{}, + } +} + +func (g *Guard) AddMutexForPkg(pkg *packages.Package) { + g.loadMutexes[pkg] = &sync.Mutex{} +} + +func (g *Guard) MutexForPkg(pkg *packages.Package) *sync.Mutex { + return g.loadMutexes[pkg] +} + +func (g *Guard) MutexForExportData() *sync.Mutex { + return &g.mutexForExportData +} + +func (g *Guard) Mutex() *sync.Mutex { + return &g.mutex +} diff --git a/pkg/golinters/goanalysis/metalinter.go b/pkg/golinters/goanalysis/metalinter.go new file mode 100644 index 000000000000..85430f328f56 --- /dev/null +++ b/pkg/golinters/goanalysis/metalinter.go @@ -0,0 +1,70 @@ +package goanalysis + +import ( + "context" + "fmt" + + "github.com/pkg/errors" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/lint/linter" + "github.com/golangci/golangci-lint/pkg/result" +) + +type MetaLinter struct { + linters []*Linter + analyzerToLinterName map[*analysis.Analyzer]string +} + +func NewMetaLinter(linters []*Linter, analyzerToLinterName map[*analysis.Analyzer]string) *MetaLinter { + return &MetaLinter{linters: linters, analyzerToLinterName: analyzerToLinterName} +} + +func (ml MetaLinter) Name() string { + return "goanalysis_metalinter" +} + +func (ml MetaLinter) Desc() string { + return "" +} + +func (ml MetaLinter) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) { + for _, linter := range ml.linters { + if err := analysis.Validate(linter.analyzers); err != nil { + return nil, errors.Wrapf(err, "failed to validate analyzers of %s", linter.Name()) + } + } + + for _, linter := range ml.linters { + if err := linter.configure(); err != nil { + return nil, errors.Wrapf(err, "failed to configure analyzers of %s", linter.Name()) + } + } + + var allAnalyzers []*analysis.Analyzer + for _, linter := range ml.linters { + allAnalyzers = append(allAnalyzers, linter.analyzers...) + } + + runner := newRunner("metalinter", lintCtx.Log.Child("goanalysis"), lintCtx.PkgCache, lintCtx.LoadGuard) + + diags, errs := runner.run(allAnalyzers, lintCtx.Packages) + for i := 1; i < len(errs); i++ { + lintCtx.Log.Warnf("go/analysis metalinter error: %s", errs[i]) + } + if len(errs) != 0 { + return nil, errs[0] + } + + var issues []result.Issue + for i := range diags { + diag := &diags[i] + issues = append(issues, result.Issue{ + FromLinter: ml.analyzerToLinterName[diag.Analyzer], + Text: fmt.Sprintf("%s: %s", diag.Analyzer, diag.Message), + Pos: diag.Position, + }) + } + + return issues, nil +} diff --git a/pkg/golinters/goanalysis/passes/nilness/nilness.go b/pkg/golinters/goanalysis/passes/nilness/nilness.go deleted file mode 100644 index 7c1967b46a08..000000000000 --- a/pkg/golinters/goanalysis/passes/nilness/nilness.go +++ /dev/null @@ -1,275 +0,0 @@ -// Copyright 2018 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package nilness inspects the control-flow graph of an SSA function -// and reports errors such as nil pointer dereferences and degenerate -// nil pointer comparisons. - -// This is a copy of https://github.com/golang/tools/blob/master/go/analysis/passes/nilness/nilness.go -// from the commit f0bfdbff1f9c986484a9f02fc198b1efcfe76ebe. -// Can't use the original one because of https://github.com/golang/go/issues/29612 -package nilness - -import ( - "fmt" - "go/token" - "go/types" - - "golang.org/x/tools/go/analysis" - "golang.org/x/tools/go/analysis/passes/buildssa" - "golang.org/x/tools/go/ssa" -) - -const Doc = `check for redundant or impossible nil comparisons - -The nilness checker inspects the control-flow graph of each function in -a package and reports nil pointer dereferences and degenerate nil -pointers. A degenerate comparison is of the form x==nil or x!=nil where x -is statically known to be nil or non-nil. These are often a mistake, -especially in control flow related to errors. - -This check reports conditions such as: - - if f == nil { // impossible condition (f is a function) - } - -and: - - p := &v - ... - if p != nil { // tautological condition - } - -and: - - if p == nil { - print(*p) // nil dereference - } -` - -var Analyzer = &analysis.Analyzer{ - Name: "nilness", - Doc: Doc, - Run: run, - Requires: []*analysis.Analyzer{buildssa.Analyzer}, -} - -func run(pass *analysis.Pass) (interface{}, error) { - ssainput := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA) - for _, fn := range ssainput.SrcFuncs { - runFunc(pass, fn) - } - return nil, nil -} - -func runFunc(pass *analysis.Pass, fn *ssa.Function) { - reportf := func(category string, pos token.Pos, format string, args ...interface{}) { - pass.Report(analysis.Diagnostic{ - Pos: pos, - Category: category, - Message: fmt.Sprintf(format, args...), - }) - } - - // notNil reports an error if v is provably nil. - notNil := func(stack []fact, instr ssa.Instruction, v ssa.Value, descr string) { - if nilnessOf(stack, v) == isnil { - reportf("nilderef", instr.Pos(), "nil dereference in "+descr) - } - } - - // visit visits reachable blocks of the CFG in dominance order, - // maintaining a stack of dominating nilness facts. - // - // By traversing the dom tree, we can pop facts off the stack as - // soon as we've visited a subtree. Had we traversed the CFG, - // we would need to retain the set of facts for each block. - seen := make([]bool, len(fn.Blocks)) // seen[i] means visit should ignore block i - var visit func(b *ssa.BasicBlock, stack []fact) - visit = func(b *ssa.BasicBlock, stack []fact) { - if seen[b.Index] { - return - } - seen[b.Index] = true - - // Report nil dereferences. - for _, instr := range b.Instrs { - switch instr := instr.(type) { - case ssa.CallInstruction: - notNil(stack, instr, instr.Common().Value, - instr.Common().Description()) - case *ssa.FieldAddr: - notNil(stack, instr, instr.X, "field selection") - case *ssa.IndexAddr: - notNil(stack, instr, instr.X, "index operation") - case *ssa.MapUpdate: - notNil(stack, instr, instr.Map, "map update") - case *ssa.Slice: - // A nilcheck occurs in ptr[:] iff ptr is a pointer to an array. - if _, ok := instr.X.Type().Underlying().(*types.Pointer); ok { - notNil(stack, instr, instr.X, "slice operation") - } - case *ssa.Store: - notNil(stack, instr, instr.Addr, "store") - case *ssa.TypeAssert: - notNil(stack, instr, instr.X, "type assertion") - case *ssa.UnOp: - if instr.Op == token.MUL { // *X - notNil(stack, instr, instr.X, "load") - } - } - } - - // For nil comparison blocks, report an error if the condition - // is degenerate, and push a nilness fact on the stack when - // visiting its true and false successor blocks. - if binop, tsucc, fsucc := eq(b); binop != nil { - xnil := nilnessOf(stack, binop.X) - ynil := nilnessOf(stack, binop.Y) - - if ynil != unknown && xnil != unknown && (xnil == isnil || ynil == isnil) { - // Degenerate condition: - // the nilness of both operands is known, - // and at least one of them is nil. - var adj string - if (xnil == ynil) == (binop.Op == token.EQL) { - adj = "tautological" - } else { - adj = "impossible" - } - reportf("cond", binop.Pos(), "%s condition: %s %s %s", adj, xnil, binop.Op, ynil) - - // If tsucc's or fsucc's sole incoming edge is impossible, - // it is unreachable. Prune traversal of it and - // all the blocks it dominates. - // (We could be more precise with full dataflow - // analysis of control-flow joins.) - var skip *ssa.BasicBlock - if xnil == ynil { - skip = fsucc - } else { - skip = tsucc - } - for _, d := range b.Dominees() { - if d == skip && len(d.Preds) == 1 { - continue - } - visit(d, stack) - } - return - } - - // "if x == nil" or "if nil == y" condition; x, y are unknown. - if xnil == isnil || ynil == isnil { - var f fact - if xnil == isnil { - // x is nil, y is unknown: - // t successor learns y is nil. - f = fact{binop.Y, isnil} - } else { - // x is nil, y is unknown: - // t successor learns x is nil. - f = fact{binop.X, isnil} - } - - for _, d := range b.Dominees() { - // Successor blocks learn a fact - // only at non-critical edges. - // (We could do be more precise with full dataflow - // analysis of control-flow joins.) - s := stack - if len(d.Preds) == 1 { - if d == tsucc { - s = append(s, f) - } else if d == fsucc { - s = append(s, f.negate()) - } - } - visit(d, s) - } - return - } - } - - for _, d := range b.Dominees() { - visit(d, stack) - } - } - - // Visit the entry block. No need to visit fn.Recover. - if fn.Blocks != nil { - visit(fn.Blocks[0], make([]fact, 0, 20)) // 20 is plenty - } -} - -// A fact records that a block is dominated -// by the condition v == nil or v != nil. -type fact struct { - value ssa.Value - nilness nilness -} - -func (f fact) negate() fact { return fact{f.value, -f.nilness} } - -type nilness int - -const ( - isnonnil = -1 - unknown nilness = 0 - isnil = 1 -) - -var nilnessStrings = []string{"non-nil", "unknown", "nil"} - -func (n nilness) String() string { return nilnessStrings[n+1] } - -// nilnessOf reports whether v is definitely nil, definitely not nil, -// or unknown given the dominating stack of facts. -func nilnessOf(stack []fact, v ssa.Value) nilness { - // Is value intrinsically nil or non-nil? - switch v := v.(type) { - case *ssa.Alloc, - *ssa.FieldAddr, - *ssa.FreeVar, - *ssa.Function, - *ssa.Global, - *ssa.IndexAddr, - *ssa.MakeChan, - *ssa.MakeClosure, - *ssa.MakeInterface, - *ssa.MakeMap, - *ssa.MakeSlice: - return isnonnil - case *ssa.Const: - if v.IsNil() { - return isnil - } else { - return isnonnil - } - } - - // Search dominating control-flow facts. - for _, f := range stack { - if f.value == v { - return f.nilness - } - } - return unknown -} - -// If b ends with an equality comparison, eq returns the operation and -// its true (equal) and false (not equal) successors. -func eq(b *ssa.BasicBlock) (op *ssa.BinOp, tsucc, fsucc *ssa.BasicBlock) { - if If, ok := b.Instrs[len(b.Instrs)-1].(*ssa.If); ok { - if binop, ok := If.Cond.(*ssa.BinOp); ok { - switch binop.Op { - case token.EQL: - return binop, b.Succs[0], b.Succs[1] - case token.NEQ: - return binop, b.Succs[1], b.Succs[0] - } - } - } - return nil, nil, nil -} diff --git a/pkg/golinters/goanalysis/runner.go b/pkg/golinters/goanalysis/runner.go new file mode 100644 index 000000000000..096591e64eec --- /dev/null +++ b/pkg/golinters/goanalysis/runner.go @@ -0,0 +1,1122 @@ +// checker is a partial copy of https://github.com/golang/tools/blob/master/go/analysis/internal/checker +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package checker defines the implementation of the checker commands. +// The same code drives the multi-analysis driver, the single-analysis +// driver that is conventionally provided for convenience along with +// each analysis package, and the test driver. +package goanalysis + +import ( + "bytes" + "encoding/gob" + "fmt" + "go/ast" + "go/parser" + "go/scanner" + "go/token" + "go/types" + "log" + "os" + "reflect" + "runtime" + "runtime/debug" + "sort" + "strings" + "sync" + "time" + + "golang.org/x/tools/go/types/objectpath" + + "golang.org/x/tools/go/gcexportdata" + + "github.com/golangci/golangci-lint/internal/errorutil" + "github.com/golangci/golangci-lint/internal/pkgcache" + + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis/load" + "github.com/golangci/golangci-lint/pkg/logutils" + + "github.com/pkg/errors" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/packages" +) + +var ( + // Debug is a set of single-letter flags: + // + // f show [f]acts as they are created + // p disable [p]arallel execution of analyzers + // s do additional [s]anity checks on fact types and serialization + // t show [t]iming info (NB: use 'p' flag to avoid GC/scheduler noise) + // v show [v]erbose logging + // + + debugf = logutils.Debug("goanalysis") + isDebug = logutils.HaveDebugTag("goanalysis") + + factsDebugf = logutils.Debug("goanalysis/facts") + isFactsDebug = logutils.HaveDebugTag("goanalysis/facts") + + factsCacheDebugf = logutils.Debug("goanalysis/facts/cache") + analyzeDebugf = logutils.Debug("goanalysis/analyze") + + Debug = os.Getenv("GL_GOANALYSIS_DEBUG") + + unsafePkgName = "unsafe" +) + +type Diagnostic struct { + analysis.Diagnostic + Analyzer *analysis.Analyzer + Position token.Position +} + +type runner struct { + log logutils.Log + prefix string // ensure unique analyzer names + pkgCache *pkgcache.Cache + loadGuard *load.Guard +} + +func newRunner(prefix string, logger logutils.Log, pkgCache *pkgcache.Cache, loadGuard *load.Guard) *runner { + return &runner{ + prefix: prefix, + log: logger, + pkgCache: pkgCache, + loadGuard: loadGuard, + } +} + +// Run loads the packages specified by args using go/packages, +// then applies the specified analyzers to them. +// Analysis flags must already have been set. +// It provides most of the logic for the main functions of both the +// singlechecker and the multi-analysis commands. +// It returns the appropriate exit code. +//nolint:gocyclo +func (r *runner) run(analyzers []*analysis.Analyzer, initialPackages []*packages.Package) ([]Diagnostic, []error) { + defer r.pkgCache.Trim() + + roots, err := r.analyze(initialPackages, analyzers) + if err != nil { + return nil, []error{err} + } + + return extractDiagnostics(roots) +} + +func (r *runner) analyze(pkgs []*packages.Package, analyzers []*analysis.Analyzer) ([]*action, error) { + // Construct the action graph. + + // Each graph node (action) is one unit of analysis. + // Edges express package-to-package (vertical) dependencies, + // and analysis-to-analysis (horizontal) dependencies. + type key struct { + *analysis.Analyzer + *packages.Package + } + actions := make(map[key]*action) + + initialPkgs := map[*packages.Package]bool{} + for _, pkg := range pkgs { + initialPkgs[pkg] = true + } + + var mkAction func(a *analysis.Analyzer, pkg *packages.Package) *action + mkAction = func(a *analysis.Analyzer, pkg *packages.Package) *action { + k := key{a, pkg} + act, ok := actions[k] + if !ok { + act = &action{ + a: a, + pkg: pkg, + log: r.log, + prefix: r.prefix, + pkgCache: r.pkgCache, + isInitialPkg: initialPkgs[pkg], + needAnalyzeSource: initialPkgs[pkg], + analysisDoneCh: make(chan struct{}), + objectFacts: make(map[objectFactKey]analysis.Fact), + packageFacts: make(map[packageFactKey]analysis.Fact), + } + + // Add a dependency on each required analyzers. + for _, req := range a.Requires { + act.deps = append(act.deps, mkAction(req, pkg)) + } + + // An analysis that consumes/produces facts + // must run on the package's dependencies too. + if len(a.FactTypes) > 0 { + paths := make([]string, 0, len(pkg.Imports)) + for path := range pkg.Imports { + paths = append(paths, path) + } + sort.Strings(paths) // for determinism + for _, path := range paths { + dep := mkAction(a, pkg.Imports[path]) + act.deps = append(act.deps, dep) + } + + // Need to register fact types for pkgcache proper gob encoding. + for _, f := range a.FactTypes { + gob.Register(f) + } + } + + actions[k] = act + } + return act + } + + // Build nodes for initial packages. + var roots []*action + for _, a := range analyzers { + for _, pkg := range pkgs { + root := mkAction(a, pkg) + root.isroot = true + roots = append(roots, root) + } + } + + allActions := make([]*action, 0, len(actions)) + for _, act := range actions { + allActions = append(allActions, act) + } + + if err := r.loadPackagesAndFacts(allActions, initialPkgs); err != nil { + return nil, errors.Wrap(err, "failed to load packages") + } + + r.runActionsAnalysis(allActions) + + return roots, nil +} + +func (r *runner) loadPackagesAndFacts(actions []*action, initialPkgs map[*packages.Package]bool) error { + defer func(from time.Time) { + debugf("Loading packages and facts took %s", time.Since(from)) + }(time.Now()) + + actionPerPkg := map[*packages.Package][]*action{} + for _, act := range actions { + actionPerPkg[act.pkg] = append(actionPerPkg[act.pkg], act) + } + + // Fill Imports field. + loadingPackages := map[*packages.Package]*loadingPackage{} + var dfs func(pkg *packages.Package) + dfs = func(pkg *packages.Package) { + if loadingPackages[pkg] != nil { + return + } + + imports := map[string]*loadingPackage{} + for impPath, imp := range pkg.Imports { + dfs(imp) + imports[impPath] = loadingPackages[imp] + } + + loadingPackages[pkg] = &loadingPackage{ + pkg: pkg, + imports: imports, + isInitial: initialPkgs[pkg], + doneCh: make(chan struct{}), + log: r.log, + actions: actionPerPkg[pkg], + loadGuard: r.loadGuard, + } + } + for _, act := range actions { + dfs(act.pkg) + } + + // Limit IO. + loadSem := make(chan struct{}, runtime.GOMAXPROCS(-1)) + + var wg sync.WaitGroup + wg.Add(len(loadingPackages)) + errCh := make(chan error, len(loadingPackages)) + for _, lp := range loadingPackages { + go func(lp *loadingPackage) { + defer wg.Done() + + lp.waitUntilImportsLoaded() + loadSem <- struct{}{} + + if err := lp.loadWithFacts(); err != nil { + errCh <- errors.Wrapf(err, "failed to load package %s", lp.pkg.Name) + } + <-loadSem + }(lp) + } + wg.Wait() + + close(errCh) + for err := range errCh { + return err + } + + return nil +} + +func (r *runner) runActionsAnalysis(actions []*action) { + // Execute the graph in parallel. + debugf("Running %d actions in parallel", len(actions)) + var wg sync.WaitGroup + wg.Add(len(actions)) + panicsCh := make(chan error, len(actions)) + for _, act := range actions { + go func(act *action) { + defer func() { + if p := recover(); p != nil { + panicsCh <- errorutil.NewPanicError(fmt.Sprintf("%s: package %q (isInitialPkg: %t, needAnalyzeSource: %t): %s", + act.a.Name, act.pkg.Name, act.isInitialPkg, act.needAnalyzeSource, p), debug.Stack()) + } + wg.Done() + }() + act.analyze() + }(act) + } + wg.Wait() + close(panicsCh) + + for p := range panicsCh { + panic(p) + } +} + +//nolint:nakedret +func extractDiagnostics(roots []*action) (retDiags []Diagnostic, retErrors []error) { + extracted := make(map[*action]bool) + var extract func(*action) + var visitAll func(actions []*action) + visitAll = func(actions []*action) { + for _, act := range actions { + if !extracted[act] { + extracted[act] = true + visitAll(act.deps) + extract(act) + } + } + } + + // De-duplicate diagnostics by position (not token.Pos) to + // avoid double-reporting in source files that belong to + // multiple packages, such as foo and foo.test. + type key struct { + token.Position + *analysis.Analyzer + message string + } + seen := make(map[key]bool) + + extract = func(act *action) { + if act.err != nil { + retErrors = append(retErrors, errors.Wrap(act.err, act.a.Name)) + return + } + + if act.isroot { + for _, diag := range act.diagnostics { + // We don't display a.Name/f.Category + // as most users don't care. + + posn := act.pkg.Fset.Position(diag.Pos) + k := key{posn, act.a, diag.Message} + if seen[k] { + continue // duplicate + } + seen[k] = true + + retDiags = append(retDiags, Diagnostic{Diagnostic: diag, Analyzer: act.a, Position: posn}) + } + } + } + visitAll(roots) + return +} + +// NeedFacts reports whether any analysis required by the specified set +// needs facts. If so, we must load the entire program from source. +func NeedFacts(analyzers []*analysis.Analyzer) bool { + seen := make(map[*analysis.Analyzer]bool) + var q []*analysis.Analyzer // for BFS + q = append(q, analyzers...) + for len(q) > 0 { + a := q[0] + q = q[1:] + if !seen[a] { + seen[a] = true + if len(a.FactTypes) > 0 { + return true + } + q = append(q, a.Requires...) + } + } + return false +} + +// An action represents one unit of analysis work: the application of +// one analysis to one package. Actions form a DAG, both within a +// package (as different analyzers are applied, either in sequence or +// parallel), and across packages (as dependencies are analyzed). +type action struct { + a *analysis.Analyzer + pkg *packages.Package + pass *analysis.Pass + isroot bool + isInitialPkg bool + needAnalyzeSource bool + deps []*action + objectFacts map[objectFactKey]analysis.Fact + packageFacts map[packageFactKey]analysis.Fact + result interface{} + diagnostics []analysis.Diagnostic + err error + duration time.Duration + log logutils.Log + prefix string + pkgCache *pkgcache.Cache + analysisDoneCh chan struct{} + loadCachedFactsDone bool + loadCachedFactsOk bool +} + +type objectFactKey struct { + obj types.Object + typ reflect.Type +} + +type packageFactKey struct { + pkg *types.Package + typ reflect.Type +} + +func (act *action) String() string { + return fmt.Sprintf("%s@%s", act.a, act.pkg) +} + +func (act *action) loadCachedFacts() bool { + if act.loadCachedFactsDone { // can't be set in parallel + return act.loadCachedFactsOk + } + + res := func() bool { + if act.isInitialPkg { + return true // load cached facts only for non-initial packages + } + + if len(act.a.FactTypes) == 0 { + return true // no need to load facts + } + + return act.loadPersistedFacts() + }() + act.loadCachedFactsDone = true + act.loadCachedFactsOk = res + return res +} + +func (act *action) analyze() { + defer close(act.analysisDoneCh) // unblock actions depending from this action + + if !act.needAnalyzeSource { + return + } + + // Analyze dependencies. + for _, dep := range act.deps { + <-dep.analysisDoneCh + } + + // TODO(adonovan): uncomment this during profiling. + // It won't build pre-go1.11 but conditional compilation + // using build tags isn't warranted. + // + // ctx, task := trace.NewTask(context.Background(), "exec") + // trace.Log(ctx, "pass", act.String()) + // defer task.End() + + // Record time spent in this node but not its dependencies. + // In parallel mode, due to GC/scheduler contention, the + // time is 5x higher than in sequential mode, even with a + // semaphore limiting the number of threads here. + // So use -debug=tp. + if isDebug { + t0 := time.Now() + defer func() { act.duration = time.Since(t0) }() + } + defer func(now time.Time) { + analyzeDebugf("go/analysis: %s: %s: analyzed package %q in %s", act.prefix, act.a.Name, act.pkg.Name, time.Since(now)) + }(time.Now()) + + // Report an error if any dependency failed. + var failed []string + for _, dep := range act.deps { + if dep.err != nil { + failed = append(failed, dep.String()) + } + } + if failed != nil { + sort.Strings(failed) + act.err = fmt.Errorf("failed prerequisites: %s", strings.Join(failed, ", ")) + return + } + + // Plumb the output values of the dependencies + // into the inputs of this action. Also facts. + inputs := make(map[*analysis.Analyzer]interface{}) + for _, dep := range act.deps { + if dep.pkg == act.pkg { + // Same package, different analysis (horizontal edge): + // in-memory outputs of prerequisite analyzers + // become inputs to this analysis pass. + inputs[dep.a] = dep.result + } else if dep.a == act.a { // (always true) + // Same analysis, different package (vertical edge): + // serialized facts produced by prerequisite analysis + // become available to this analysis pass. + inheritFacts(act, dep) + } + } + + // Run the analysis. + pass := &analysis.Pass{ + Analyzer: act.a, + Fset: act.pkg.Fset, + Files: act.pkg.Syntax, + OtherFiles: act.pkg.OtherFiles, + Pkg: act.pkg.Types, + TypesInfo: act.pkg.TypesInfo, + TypesSizes: act.pkg.TypesSizes, + ResultOf: inputs, + Report: func(d analysis.Diagnostic) { act.diagnostics = append(act.diagnostics, d) }, + ImportObjectFact: act.importObjectFact, + ExportObjectFact: act.exportObjectFact, + ImportPackageFact: act.importPackageFact, + ExportPackageFact: act.exportPackageFact, + AllObjectFacts: act.allObjectFacts, + AllPackageFacts: act.allPackageFacts, + } + act.pass = pass + + var err error + if act.pkg.IllTyped && !pass.Analyzer.RunDespiteErrors { + err = fmt.Errorf("analysis skipped due to errors in package") + } else { + act.result, err = pass.Analyzer.Run(pass) + if err == nil { + if got, want := reflect.TypeOf(act.result), pass.Analyzer.ResultType; got != want { + err = fmt.Errorf( + "internal error: on package %s, analyzer %s returned a result of type %v, but declared ResultType %v", + pass.Pkg.Path(), pass.Analyzer, got, want) + } + } + } + act.err = err + + // disallow calls after Run + pass.ExportObjectFact = nil + pass.ExportPackageFact = nil + + if err := act.persistFactsToCache(); err != nil { + act.log.Warnf("Failed to persist facts to cache: %s", err) + } +} + +// inheritFacts populates act.facts with +// those it obtains from its dependency, dep. +func inheritFacts(act, dep *action) { + serialize := false + + for key, fact := range dep.objectFacts { + // Filter out facts related to objects + // that are irrelevant downstream + // (equivalently: not in the compiler export data). + if !exportedFrom(key.obj, dep.pkg.Types) { + factsDebugf("%v: discarding %T fact from %s for %s: %s", act, fact, dep, key.obj, fact) + continue + } + + // Optionally serialize/deserialize fact + // to verify that it works across address spaces. + if serialize { + var err error + fact, err = codeFact(fact) + if err != nil { + log.Panicf("internal error: encoding of %T fact failed in %v", fact, act) + } + } + + factsDebugf("%v: inherited %T fact for %s: %s", act, fact, key.obj, fact) + act.objectFacts[key] = fact + } + + for key, fact := range dep.packageFacts { + // TODO: filter out facts that belong to + // packages not mentioned in the export data + // to prevent side channels. + + // Optionally serialize/deserialize fact + // to verify that it works across address spaces + // and is deterministic. + if serialize { + var err error + fact, err = codeFact(fact) + if err != nil { + log.Panicf("internal error: encoding of %T fact failed in %v", fact, act) + } + } + + factsDebugf("%v: inherited %T fact for %s: %s", act, fact, key.pkg.Path(), fact) + act.packageFacts[key] = fact + } +} + +// codeFact encodes then decodes a fact, +// just to exercise that logic. +func codeFact(fact analysis.Fact) (analysis.Fact, error) { + // We encode facts one at a time. + // A real modular driver would emit all facts + // into one encoder to improve gob efficiency. + var buf bytes.Buffer + if err := gob.NewEncoder(&buf).Encode(fact); err != nil { + return nil, err + } + + // Encode it twice and assert that we get the same bits. + // This helps detect nondeterministic Gob encoding (e.g. of maps). + var buf2 bytes.Buffer + if err := gob.NewEncoder(&buf2).Encode(fact); err != nil { + return nil, err + } + if !bytes.Equal(buf.Bytes(), buf2.Bytes()) { + return nil, fmt.Errorf("encoding of %T fact is nondeterministic", fact) + } + + newFact := reflect.New(reflect.TypeOf(fact).Elem()).Interface().(analysis.Fact) + if err := gob.NewDecoder(&buf).Decode(newFact); err != nil { + return nil, err + } + return newFact, nil +} + +// exportedFrom reports whether obj may be visible to a package that imports pkg. +// This includes not just the exported members of pkg, but also unexported +// constants, types, fields, and methods, perhaps belonging to oether packages, +// that find there way into the API. +// This is an overapproximation of the more accurate approach used by +// gc export data, which walks the type graph, but it's much simpler. +// +// TODO(adonovan): do more accurate filtering by walking the type graph. +func exportedFrom(obj types.Object, pkg *types.Package) bool { + switch obj := obj.(type) { + case *types.Func: + return obj.Exported() && obj.Pkg() == pkg || + obj.Type().(*types.Signature).Recv() != nil + case *types.Var: + return obj.Exported() && obj.Pkg() == pkg || + obj.IsField() + case *types.TypeName, *types.Const: + return true + } + return false // Nil, Builtin, Label, or PkgName +} + +// importObjectFact implements Pass.ImportObjectFact. +// Given a non-nil pointer ptr of type *T, where *T satisfies Fact, +// importObjectFact copies the fact value to *ptr. +func (act *action) importObjectFact(obj types.Object, ptr analysis.Fact) bool { + if obj == nil { + panic("nil object") + } + key := objectFactKey{obj, factType(ptr)} + if v, ok := act.objectFacts[key]; ok { + reflect.ValueOf(ptr).Elem().Set(reflect.ValueOf(v).Elem()) + return true + } + return false +} + +// exportObjectFact implements Pass.ExportObjectFact. +func (act *action) exportObjectFact(obj types.Object, fact analysis.Fact) { + if act.pass.ExportObjectFact == nil { + log.Panicf("%s: Pass.ExportObjectFact(%s, %T) called after Run", act, obj, fact) + } + + if obj.Pkg() != act.pkg.Types { + log.Panicf("internal error: in analysis %s of package %s: Fact.Set(%s, %T): can't set facts on objects belonging another package", + act.a, act.pkg, obj, fact) + } + + key := objectFactKey{obj, factType(fact)} + act.objectFacts[key] = fact // clobber any existing entry + if isFactsDebug { + objstr := types.ObjectString(obj, (*types.Package).Name) + factsDebugf("%s: object %s has fact %s\n", + act.pkg.Fset.Position(obj.Pos()), objstr, fact) + } +} + +func (act *action) allObjectFacts() []analysis.ObjectFact { + out := make([]analysis.ObjectFact, 0, len(act.objectFacts)) + for key, fact := range act.objectFacts { + out = append(out, analysis.ObjectFact{ + Object: key.obj, + Fact: fact, + }) + } + return out +} + +// importPackageFact implements Pass.ImportPackageFact. +// Given a non-nil pointer ptr of type *T, where *T satisfies Fact, +// fact copies the fact value to *ptr. +func (act *action) importPackageFact(pkg *types.Package, ptr analysis.Fact) bool { + if pkg == nil { + panic("nil package") + } + key := packageFactKey{pkg, factType(ptr)} + if v, ok := act.packageFacts[key]; ok { + reflect.ValueOf(ptr).Elem().Set(reflect.ValueOf(v).Elem()) + return true + } + return false +} + +// exportPackageFact implements Pass.ExportPackageFact. +func (act *action) exportPackageFact(fact analysis.Fact) { + if act.pass.ExportPackageFact == nil { + log.Panicf("%s: Pass.ExportPackageFact(%T) called after Run", act, fact) + } + + key := packageFactKey{act.pass.Pkg, factType(fact)} + act.packageFacts[key] = fact // clobber any existing entry + factsDebugf("%s: package %s has fact %s\n", + act.pkg.Fset.Position(act.pass.Files[0].Pos()), act.pass.Pkg.Path(), fact) +} + +func (act *action) allPackageFacts() []analysis.PackageFact { + out := make([]analysis.PackageFact, 0, len(act.packageFacts)) + for key, fact := range act.packageFacts { + out = append(out, analysis.PackageFact{ + Package: key.pkg, + Fact: fact, + }) + } + return out +} + +func factType(fact analysis.Fact) reflect.Type { + t := reflect.TypeOf(fact) + if t.Kind() != reflect.Ptr { + log.Fatalf("invalid Fact type: got %T, want pointer", t) + } + return t +} + +type Fact struct { + Path string // non-empty only for object facts + Fact analysis.Fact +} + +func (act *action) persistFactsToCache() error { + analyzer := act.a + if len(analyzer.FactTypes) == 0 { + return nil + } + + // Merge new facts into the package and persist them. + var facts []Fact + for key, fact := range act.packageFacts { + if key.pkg != act.pkg.Types { + // The fact is from inherited facts from another package + continue + } + facts = append(facts, Fact{ + Path: "", + Fact: fact, + }) + } + for key, fact := range act.objectFacts { + obj := key.obj + if obj.Pkg() != act.pkg.Types { + // The fact is from inherited facts from another package + continue + } + + path, err := objectpath.For(obj) + if err != nil { + // The object is not globally addressable + continue + } + + facts = append(facts, Fact{ + Path: string(path), + Fact: fact, + }) + } + + factsCacheDebugf("Caching %d facts for package %q and analyzer %s", len(facts), act.pkg.Name, act.a.Name) + + key := fmt.Sprintf("%s/facts", analyzer.Name) + return act.pkgCache.Put(act.pkg, key, facts) +} + +func (act *action) loadPersistedFacts() bool { + var facts []Fact + key := fmt.Sprintf("%s/facts", act.a.Name) + if err := act.pkgCache.Get(act.pkg, key, &facts); err != nil { + if err != pkgcache.ErrMissing { + act.log.Warnf("Failed to get persisted facts: %s", err) + } + + factsCacheDebugf("No cached facts for package %q and analyzer %s", act.pkg.Name, act.a.Name) + return false + } + + factsCacheDebugf("Loaded %d cached facts for package %q and analyzer %s", len(facts), act.pkg.Name, act.a.Name) + + for _, f := range facts { + if f.Path == "" { // this is a package fact + key := packageFactKey{act.pkg.Types, factType(f.Fact)} + act.packageFacts[key] = f.Fact + continue + } + obj, err := objectpath.Object(act.pkg.Types, objectpath.Path(f.Path)) + if err != nil { + // Be lenient about these errors. For example, when + // analyzing io/ioutil from source, we may get a fact + // for methods on the devNull type, and objectpath + // will happily create a path for them. However, when + // we later load io/ioutil from export data, the path + // no longer resolves. + // + // If an exported type embeds the unexported type, + // then (part of) the unexported type will become part + // of the type information and our path will resolve + // again. + continue + } + factKey := objectFactKey{obj, factType(f.Fact)} + act.objectFacts[factKey] = f.Fact + } + + return true +} + +type loadingPackage struct { + pkg *packages.Package + imports map[string]*loadingPackage + isInitial bool + doneCh chan struct{} + log logutils.Log + actions []*action // all actions with this package + wasLoaded bool + loadGuard *load.Guard +} + +func (lp *loadingPackage) loadFromSource() error { + pkg := lp.pkg + + // Call NewPackage directly with explicit name. + // This avoids skew between golist and go/types when the files' + // package declarations are inconsistent. + // Subtle: we populate all Types fields with an empty Package + // before loading export data so that export data processing + // never has to create a types.Package for an indirect dependency, + // which would then require that such created packages be explicitly + // inserted back into the Import graph as a final step after export data loading. + pkg.Types = types.NewPackage(pkg.PkgPath, pkg.Name) + + pkg.IllTyped = true + + // Many packages have few files, much fewer than there + // are CPU cores. Additionally, parsing each individual file is + // very fast. A naive parallel implementation of this loop won't + // be faster, and tends to be slower due to extra scheduling, + // bookkeeping and potentially false sharing of cache lines. + pkg.Syntax = make([]*ast.File, len(pkg.CompiledGoFiles)) + for i, file := range pkg.CompiledGoFiles { + f, err := parser.ParseFile(pkg.Fset, file, nil, parser.ParseComments) + if err != nil { + pkg.Errors = append(pkg.Errors, lp.convertError(err)...) + return err + } + pkg.Syntax[i] = f + } + pkg.TypesInfo = &types.Info{ + Types: make(map[ast.Expr]types.TypeAndValue), + Defs: make(map[*ast.Ident]types.Object), + Uses: make(map[*ast.Ident]types.Object), + Implicits: make(map[ast.Node]types.Object), + Scopes: make(map[ast.Node]*types.Scope), + Selections: make(map[*ast.SelectorExpr]*types.Selection), + } + + importer := func(path string) (*types.Package, error) { + if path == unsafePkgName { + return types.Unsafe, nil + } + if path == "C" { + // go/packages doesn't tell us that cgo preprocessing + // failed. When we subsequently try to parse the package, + // we'll encounter the raw C import. + return nil, errors.New("cgo preprocessing failed") + } + imp := pkg.Imports[path] + if imp == nil { + return nil, nil + } + if len(imp.Errors) > 0 { + return nil, imp.Errors[0] + } + return imp.Types, nil + } + tc := &types.Config{ + Importer: importerFunc(importer), + Error: func(err error) { + pkg.Errors = append(pkg.Errors, lp.convertError(err)...) + }, + } + err := types.NewChecker(tc, pkg.Fset, pkg.Types, pkg.TypesInfo).Files(pkg.Syntax) + if err != nil { + return err + } + pkg.IllTyped = false + return nil +} + +func (lp *loadingPackage) loadFromExportData() error { + // Because gcexportdata.Read has the potential to create or + // modify the types.Package for each node in the transitive + // closure of dependencies of lpkg, all exportdata operations + // must be sequential. (Finer-grained locking would require + // changes to the gcexportdata API.) + // + // The exportMu lock guards the Package.Pkg field and the + // types.Package it points to, for each Package in the graph. + // + // Not all accesses to Package.Pkg need to be protected by this mutex: + // graph ordering ensures that direct dependencies of source + // packages are fully loaded before the importer reads their Pkg field. + mu := lp.loadGuard.MutexForExportData() + mu.Lock() + defer mu.Unlock() + + pkg := lp.pkg + + // Call NewPackage directly with explicit name. + // This avoids skew between golist and go/types when the files' + // package declarations are inconsistent. + // Subtle: we populate all Types fields with an empty Package + // before loading export data so that export data processing + // never has to create a types.Package for an indirect dependency, + // which would then require that such created packages be explicitly + // inserted back into the Import graph as a final step after export data loading. + pkg.Types = types.NewPackage(pkg.PkgPath, pkg.Name) + + pkg.IllTyped = true + for path, pkg := range pkg.Imports { + if pkg.Types == nil { + return fmt.Errorf("dependency %q hasn't been loaded yet", path) + } + } + if pkg.ExportFile == "" { + return fmt.Errorf("no export data for %q", pkg.ID) + } + f, err := os.Open(pkg.ExportFile) + if err != nil { + return err + } + defer f.Close() + + r, err := gcexportdata.NewReader(f) + if err != nil { + return err + } + + view := make(map[string]*types.Package) // view seen by gcexportdata + seen := make(map[*packages.Package]bool) // all visited packages + var visit func(pkgs map[string]*packages.Package) + visit = func(pkgs map[string]*packages.Package) { + for _, pkg := range pkgs { + if !seen[pkg] { + seen[pkg] = true + view[pkg.PkgPath] = pkg.Types + visit(pkg.Imports) + } + } + } + visit(pkg.Imports) + tpkg, err := gcexportdata.Read(r, pkg.Fset, view, pkg.PkgPath) + if err != nil { + return err + } + pkg.Types = tpkg + pkg.IllTyped = false + return nil +} + +func (lp *loadingPackage) waitUntilImportsLoaded() { + // Imports must be loaded before loading the package. + for _, imp := range lp.imports { + <-imp.doneCh + } +} + +func (lp *loadingPackage) loadWithFacts() error { + defer close(lp.doneCh) + defer func() { + lp.wasLoaded = true + }() + + pkg := lp.pkg + + if pkg.PkgPath == unsafePkgName { + // Fill in the blanks to avoid surprises. + pkg.Types = types.Unsafe + pkg.Syntax = []*ast.File{} + pkg.TypesInfo = new(types.Info) + return nil + } + + markDepsForAnalyzingSource := func(act *action) { + // Horizontal deps (analyzer.Requires) must be loaded from source and analyzed before analyzing + // this action. + for _, dep := range act.deps { + if dep.pkg == act.pkg { + // Analyze source only for horizontal dependencies, e.g. from "buildssa". + dep.needAnalyzeSource = true // can't be set in parallel + } + } + } + + if pkg.TypesInfo != nil { + // Already loaded package, e.g. because another not go/analysis linter required types for deps. + // Try load cached facts for it. + + if !lp.wasLoaded { // wasLoaded can't be set in parallel + for _, act := range lp.actions { + if !act.loadCachedFacts() { + // Cached facts loading failed: analyze later the action from source. + act.needAnalyzeSource = true + markDepsForAnalyzingSource(act) + } + } + } + return nil + } + + if lp.isInitial { + // No need to load cached facts: the package will be analyzed from source + // because it's the initial. + return lp.loadFromSource() + } + + // Load package from export data + if err := lp.loadFromExportData(); err != nil { + // We asked Go to give us up to date export data, yet + // we can't load it. There must be something wrong. + // + // Attempt loading from source. This should fail (because + // otherwise there would be export data); we just want to + // get the compile errors. If loading from source succeeds + // we discard the result, anyway. Otherwise we'll fail + // when trying to reload from export data later. + + // Otherwise it panics because uses already existing (from exported data) types. + pkg.Types = types.NewPackage(pkg.PkgPath, pkg.Name) + if srcErr := lp.loadFromSource(); srcErr != nil { + return srcErr + } + // Make sure this package can't be imported successfully + pkg.Errors = append(pkg.Errors, packages.Error{ + Pos: "-", + Msg: fmt.Sprintf("could not load export data: %s", err), + Kind: packages.ParseError, + }) + return errors.Wrap(err, "could not load export data") + } + + needLoadFromSource := false + for _, act := range lp.actions { + if act.loadCachedFacts() { + continue + } + + // Cached facts loading failed: analyze later the action from source. + factsCacheDebugf("Loading of facts for %s:%s failed, analyze it from source later", act.a.Name, pkg.Name) + act.needAnalyzeSource = true // can't be set in parallel + needLoadFromSource = true + + markDepsForAnalyzingSource(act) + } + + if needLoadFromSource { + // Cached facts loading failed: analyze later the action from source. To perform + // the analysis we need to load the package from source code. + + // Otherwise it panics because uses already existing (from exported data) types. + pkg.Types = types.NewPackage(pkg.PkgPath, pkg.Name) + return lp.loadFromSource() + } + + return nil +} + +func (lp *loadingPackage) convertError(err error) []packages.Error { + var errs []packages.Error + // taken from go/packages + switch err := err.(type) { + case packages.Error: + // from driver + errs = append(errs, err) + + case *os.PathError: + // from parser + errs = append(errs, packages.Error{ + Pos: err.Path + ":1", + Msg: err.Err.Error(), + Kind: packages.ParseError, + }) + + case scanner.ErrorList: + // from parser + for _, err := range err { + errs = append(errs, packages.Error{ + Pos: err.Pos.String(), + Msg: err.Msg, + Kind: packages.ParseError, + }) + } + + case types.Error: + // from type checker + errs = append(errs, packages.Error{ + Pos: err.Fset.Position(err.Pos).String(), + Msg: err.Msg, + Kind: packages.TypeError, + }) + + default: + // unexpected impoverished error from parser? + errs = append(errs, packages.Error{ + Pos: "-", + Msg: err.Error(), + Kind: packages.UnknownError, + }) + + // If you see this error message, please file a bug. + lp.log.Warnf("Internal error: error %q (%T) without position", err, err) + } + return errs +} + +type importerFunc func(path string) (*types.Package, error) + +func (f importerFunc) Import(path string) (*types.Package, error) { return f(path) } diff --git a/pkg/golinters/megacheck.go b/pkg/golinters/megacheck.go index 937e10cfd149..9b1b74ba31ef 100644 --- a/pkg/golinters/megacheck.go +++ b/pkg/golinters/megacheck.go @@ -3,21 +3,19 @@ package golinters import ( "context" "fmt" - "strings" - "time" - "github.com/pkg/errors" + "github.com/golangci/golangci-lint/pkg/logutils" - "github.com/golangci/go-tools/config" - "github.com/golangci/go-tools/stylecheck" + "honnef.co/go/tools/unused" - "github.com/golangci/go-tools/lint" - "github.com/golangci/go-tools/lint/lintutil" - "github.com/golangci/go-tools/simple" - "github.com/golangci/go-tools/staticcheck" - "github.com/golangci/go-tools/unused" - "golang.org/x/tools/go/packages" + "honnef.co/go/tools/lint" + "golang.org/x/tools/go/analysis" + "honnef.co/go/tools/simple" + "honnef.co/go/tools/staticcheck" + "honnef.co/go/tools/stylecheck" + + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" "github.com/golangci/golangci-lint/pkg/lint/linter" "github.com/golangci/golangci-lint/pkg/result" ) @@ -30,6 +28,8 @@ const ( MegacheckStylecheckName = "stylecheck" ) +var debugf = logutils.Debug("megacheck") + type Staticcheck struct { megacheck } @@ -143,18 +143,22 @@ func (MegacheckMetalinter) BuildLinterConfig(enabledChildren []string) (*linter. } // TODO: merge linter.Config and linter.Linter or refactor it in another way - return &linter.Config{ - Linter: m, - EnabledByDefault: false, - NeedsTypeInfo: true, - NeedsDepsTypeInfo: true, - NeedsSSARepr: true, - InPresets: []string{linter.PresetStyle, linter.PresetBugs, linter.PresetUnused}, - Speed: 1, - AlternativeNames: nil, - OriginalURL: "", - ParentLinterName: "", - }, nil + lc := &linter.Config{ + Linter: m, + EnabledByDefault: false, + NeedsSSARepr: false, + InPresets: []string{linter.PresetStyle, linter.PresetBugs, linter.PresetUnused}, + Speed: 1, + AlternativeNames: nil, + OriginalURL: "", + ParentLinterName: "", + } + if m.unusedEnabled { + lc = lc.WithLoadDepsTypeInfo() + } else { + lc = lc.WithLoadForGoAnalysis() + } + return lc, nil } func (MegacheckMetalinter) DefaultChildLinterNames() []string { @@ -167,138 +171,130 @@ func (m MegacheckMetalinter) AllChildLinterNames() []string { return append(m.DefaultChildLinterNames(), MegacheckStylecheckName) } -func (m MegacheckMetalinter) isValidChild(name string) bool { - for _, child := range m.AllChildLinterNames() { - if child == name { - return true - } - } - - return false -} - func (m megacheck) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) { // Use OriginalPackages not Packages because `unused` doesn't work properly // when we deduplicate normal and test packages. - issues, err := m.runMegacheck(lintCtx.OriginalPackages, lintCtx.Settings().Unused.CheckExported) - if err != nil { - return nil, errors.Wrap(err, "failed to run megacheck") - } + return m.runMegacheck(ctx, lintCtx) +} - if len(issues) == 0 { - return nil, nil +func getAnalyzers(m map[string]*analysis.Analyzer) []*analysis.Analyzer { + var ret []*analysis.Analyzer + for _, v := range m { + ret = append(ret, v) } + return ret +} - res := make([]result.Issue, 0, len(issues)) - meta := MegacheckMetalinter{} - for _, i := range issues { - if !meta.isValidChild(i.Checker) { - lintCtx.Log.Warnf("Bad megacheck checker name %q", i.Checker) - continue +func setGoVersion(analyzers []*analysis.Analyzer) { + const goVersion = 13 // TODO + for _, a := range analyzers { + if v := a.Flags.Lookup("go"); v != nil { + if err := v.Value.Set(fmt.Sprintf("1.%d", goVersion)); err != nil { + debugf("Failed to set go version: %s", err) + } } - - res = append(res, result.Issue{ - Pos: i.Position, - // TODO: use severity - Text: fmt.Sprintf("%s: %s", i.Check, i.Text), - FromLinter: i.Checker, - }) } - return res, nil } -func (m megacheck) runMegacheck(workingPkgs []*packages.Package, checkExportedUnused bool) ([]lint.Problem, error) { - var checkers []lint.Checker +func (m megacheck) runMegacheck(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) { + var linters []linter.Linter if m.gosimpleEnabled { - checkers = append(checkers, simple.NewChecker()) + analyzers := getAnalyzers(simple.Analyzers) + setGoVersion(analyzers) + lnt := goanalysis.NewLinter(MegacheckGosimpleName, "", analyzers, nil) + linters = append(linters, lnt) } if m.staticcheckEnabled { - checkers = append(checkers, staticcheck.NewChecker()) + analyzers := getAnalyzers(staticcheck.Analyzers) + setGoVersion(analyzers) + lnt := goanalysis.NewLinter(MegacheckStaticcheckName, "", analyzers, nil) + linters = append(linters, lnt) } if m.stylecheckEnabled { - checkers = append(checkers, stylecheck.NewChecker()) + analyzers := getAnalyzers(stylecheck.Analyzers) + setGoVersion(analyzers) + lnt := goanalysis.NewLinter(MegacheckStylecheckName, "", analyzers, nil) + linters = append(linters, lnt) } + + var u lint.CumulativeChecker if m.unusedEnabled { - uc := unused.NewChecker(unused.CheckAll) - uc.ConsiderReflection = true - uc.WholeProgram = checkExportedUnused - checkers = append(checkers, unused.NewLintChecker(uc)) + u = unused.NewChecker(lintCtx.Settings().Unused.CheckExported) + analyzers := []*analysis.Analyzer{u.Analyzer()} + setGoVersion(analyzers) + lnt := goanalysis.NewLinter(MegacheckUnusedName, "", analyzers, nil) + linters = append(linters, lnt) } - if len(checkers) == 0 { + if len(linters) == 0 { return nil, nil } - cfg := config.Config{} - opts := &lintutil.Options{ - // TODO: get current go version, but now it doesn't matter, - // may be needed after next updates of megacheck - GoVersion: 12, - - Config: cfg, - // TODO: support Ignores option + var issues []result.Issue + for _, lnt := range linters { + i, err := lnt.Run(ctx, lintCtx) + if err != nil { + return nil, err + } + issues = append(issues, i...) } - return runMegacheckCheckers(checkers, workingPkgs, opts) -} - -// parseIgnore is a copy from megacheck honnef.co/go/tools/lint/lintutil.parseIgnore -// just to not fork megacheck. -func parseIgnore(s string) ([]lint.Ignore, error) { - var out []lint.Ignore - if s == "" { - return nil, nil - } - for _, part := range strings.Fields(s) { - p := strings.Split(part, ":") - if len(p) != 2 { - return nil, errors.New("malformed ignore string") + if u != nil { + for _, ur := range u.Result() { + p := u.ProblemObject(lintCtx.Packages[0].Fset, ur) + issues = append(issues, result.Issue{ + FromLinter: MegacheckUnusedName, + Text: p.Message, + Pos: p.Pos, + }) } - path := p[0] - checks := strings.Split(p[1], ",") - out = append(out, &lint.GlobIgnore{Pattern: path, Checks: checks}) } - return out, nil + + return issues, nil } -// runMegacheckCheckers is like megacheck honnef.co/go/tools/lint/lintutil.Lint, -// but takes a list of already-parsed packages instead of a list of -// package-paths to parse. -func runMegacheckCheckers(cs []lint.Checker, workingPkgs []*packages.Package, opt *lintutil.Options) ([]lint.Problem, error) { - stats := lint.PerfStats{ - CheckerInits: map[string]time.Duration{}, +func (m megacheck) Analyzers() []*analysis.Analyzer { + if m.unusedEnabled { + // Don't treat this linter as go/analysis linter if unused is used + // because it has non-standard API. + return nil } - if opt == nil { - opt = &lintutil.Options{} + var allAnalyzers []*analysis.Analyzer + if m.gosimpleEnabled { + allAnalyzers = append(allAnalyzers, getAnalyzers(simple.Analyzers)...) } - ignores, err := parseIgnore(opt.Ignores) - if err != nil { - return nil, err + if m.staticcheckEnabled { + allAnalyzers = append(allAnalyzers, getAnalyzers(staticcheck.Analyzers)...) } - - // package-parsing elided here - stats.PackageLoading = 0 - - var problems []lint.Problem - // populating 'problems' with parser-problems elided here - - if len(workingPkgs) == 0 { - return problems, nil + if m.stylecheckEnabled { + allAnalyzers = append(allAnalyzers, getAnalyzers(stylecheck.Analyzers)...) } + setGoVersion(allAnalyzers) + return allAnalyzers +} - l := &lint.Linter{ - Checkers: cs, - Ignores: ignores, - GoVersion: opt.GoVersion, - ReturnIgnored: opt.ReturnIgnored, - Config: opt.Config, +func (megacheck) Cfg() map[string]map[string]interface{} { + return nil +} - MaxConcurrentJobs: opt.MaxConcurrentJobs, - PrintStats: opt.PrintStats, +func (m megacheck) AnalyzerToLinterNameMapping() map[*analysis.Analyzer]string { + ret := map[*analysis.Analyzer]string{} + if m.gosimpleEnabled { + for _, a := range simple.Analyzers { + ret[a] = MegacheckGosimpleName + } } - problems = append(problems, l.Lint(workingPkgs, &stats)...) - - return problems, nil + if m.staticcheckEnabled { + for _, a := range staticcheck.Analyzers { + ret[a] = MegacheckStaticcheckName + } + } + if m.stylecheckEnabled { + for _, a := range stylecheck.Analyzers { + ret[a] = MegacheckStylecheckName + } + } + return ret } diff --git a/pkg/lint/linter/config.go b/pkg/lint/linter/config.go index 75a281ad820c..2dfb8e3d2907 100644 --- a/pkg/lint/linter/config.go +++ b/pkg/lint/linter/config.go @@ -1,5 +1,9 @@ package linter +import ( + "golang.org/x/tools/go/packages" +) + const ( PresetFormatting = "format" PresetComplexity = "complexity" @@ -13,9 +17,9 @@ type Config struct { Linter Linter EnabledByDefault bool - NeedsTypeInfo bool - NeedsDepsTypeInfo bool - NeedsSSARepr bool + LoadMode packages.LoadMode + + NeedsSSARepr bool InPresets []string Speed int // more value means faster execution of linter @@ -24,21 +28,43 @@ type Config struct { OriginalURL string // URL of original (not forked) repo, needed for autogenerated README ParentLinterName string // used only for megacheck's children now CanAutoFix bool + IsSlow bool +} + +func (lc *Config) ConsiderSlow() *Config { + lc.IsSlow = true + return lc +} + +func (lc *Config) IsSlowLinter() bool { + return lc.IsSlow || (lc.LoadMode&packages.NeedTypesInfo != 0 && lc.LoadMode&packages.NeedDeps != 0) +} + +func (lc *Config) WithLoadFiles() *Config { + lc.LoadMode |= packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles + return lc +} + +func (lc *Config) WithLoadForGoAnalysis() *Config { + lc = lc.WithLoadFiles() + lc.LoadMode |= packages.NeedImports | packages.NeedDeps | packages.NeedExportsFile | packages.NeedTypesSizes + return lc.ConsiderSlow() } -func (lc *Config) WithTypeInfo() *Config { - lc.NeedsTypeInfo = true +func (lc *Config) WithLoadTypeInfo() *Config { + lc = lc.WithLoadFiles() + lc.LoadMode |= packages.NeedImports | packages.NeedTypes | packages.NeedTypesSizes | packages.NeedTypesInfo | packages.NeedSyntax return lc } -func (lc *Config) WithDepsTypeInfo() *Config { - lc.NeedsTypeInfo = true - lc.NeedsDepsTypeInfo = true +func (lc *Config) WithLoadDepsTypeInfo() *Config { + lc = lc.WithLoadTypeInfo() + lc.LoadMode |= packages.NeedDeps return lc } func (lc *Config) WithSSA() *Config { - lc.NeedsTypeInfo = true + lc = lc.WithLoadDepsTypeInfo() lc.NeedsSSARepr = true return lc } @@ -86,7 +112,8 @@ func (lc *Config) Name() string { } func NewConfig(linter Linter) *Config { - return &Config{ + lc := &Config{ Linter: linter, } + return lc.WithLoadFiles() } diff --git a/pkg/lint/linter/context.go b/pkg/lint/linter/context.go index aaf805821d70..68f9d9e92f49 100644 --- a/pkg/lint/linter/context.go +++ b/pkg/lint/linter/context.go @@ -5,6 +5,10 @@ import ( "golang.org/x/tools/go/packages" "golang.org/x/tools/go/ssa" + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis/load" + + "github.com/golangci/golangci-lint/internal/pkgcache" + "github.com/golangci/golangci-lint/pkg/fsutils" "github.com/golangci/golangci-lint/pkg/config" @@ -32,6 +36,9 @@ type Context struct { FileCache *fsutils.FileCache LineCache *fsutils.LineCache Log logutils.Log + + PkgCache *pkgcache.Cache + LoadGuard *load.Guard } func (c *Context) Settings() *config.LintersSettings { diff --git a/pkg/lint/lintersdb/enabled_set.go b/pkg/lint/lintersdb/enabled_set.go index e4f3d15444fd..7767fee290c0 100644 --- a/pkg/lint/lintersdb/enabled_set.go +++ b/pkg/lint/lintersdb/enabled_set.go @@ -3,24 +3,30 @@ package lintersdb import ( "sort" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" + "github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/lint/linter" "github.com/golangci/golangci-lint/pkg/logutils" ) type EnabledSet struct { - m *Manager - v *Validator - log logutils.Log - cfg *config.Config + m *Manager + v *Validator + log logutils.Log + cfg *config.Config + debugf logutils.DebugFunc } func NewEnabledSet(m *Manager, v *Validator, log logutils.Log, cfg *config.Config) *EnabledSet { return &EnabledSet{ - m: m, - v: v, - log: log, - cfg: cfg, + m: m, + v: v, + log: log, + cfg: cfg, + debugf: logutils.Debug("enabled_linters"), } } @@ -51,7 +57,7 @@ func (es EnabledSet) build(lcfg *config.Linters, enabledByDefaultLinters []*lint // It should be before --enable and --disable to be able to enable or disable specific linter. if lcfg.Fast { for name := range resultLintersSet { - if es.m.GetLinterConfig(name).NeedsDepsTypeInfo { + if es.m.GetLinterConfig(name).IsSlowLinter() { delete(resultLintersSet, name) } } @@ -125,6 +131,7 @@ func (es EnabledSet) Get(optimize bool) ([]*linter.Config, error) { if optimize { es.optimizeLintersSet(resultLintersSet) } + es.combineGoAnalysisLinters(resultLintersSet) var resultLinters []*linter.Config for _, lc := range resultLintersSet { @@ -134,6 +141,62 @@ func (es EnabledSet) Get(optimize bool) ([]*linter.Config, error) { return resultLinters, nil } +func (es EnabledSet) combineGoAnalysisLinters(linters map[string]*linter.Config) { + var goanalysisLinters []*goanalysis.Linter + goanalysisPresets := map[string]bool{} + analyzerToLinterName := map[*analysis.Analyzer]string{} + for _, linter := range linters { + lnt, ok := linter.Linter.(goanalysis.SupportedLinter) + if !ok { + continue + } + + analyzers := lnt.Analyzers() + if len(analyzers) == 0 { + continue // e.g. if "unused" is enabled + } + gl := goanalysis.NewLinter(linter.Name(), "", analyzers, lnt.Cfg()) + goanalysisLinters = append(goanalysisLinters, gl) + for _, p := range linter.InPresets { + goanalysisPresets[p] = true + } + for a, name := range lnt.AnalyzerToLinterNameMapping() { + analyzerToLinterName[a] = name + } + } + + if len(goanalysisLinters) <= 1 { + es.debugf("Didn't combine go/analysis linters: got only %d linters", len(goanalysisLinters)) + return + } + + for _, lnt := range goanalysisLinters { + delete(linters, lnt.Name()) + } + + ml := goanalysis.NewMetaLinter(goanalysisLinters, analyzerToLinterName) + + var presets []string + for p := range goanalysisPresets { + presets = append(presets, p) + } + + mlConfig := &linter.Config{ + Linter: ml, + EnabledByDefault: false, + NeedsSSARepr: false, + InPresets: presets, + Speed: 5, + AlternativeNames: nil, + OriginalURL: "", + ParentLinterName: "", + } + mlConfig = mlConfig.WithLoadForGoAnalysis() + + linters[ml.Name()] = mlConfig + es.debugf("Combined %d go/analysis linters into one metalinter", len(goanalysisLinters)) +} + func (es EnabledSet) verbosePrintLintersStatus(lcs map[string]*linter.Config) { var linterNames []string for _, lc := range lcs { diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index a1e7bcfa4539..5a2a57884ce0 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -85,19 +85,18 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { } lcs := []*linter.Config{ linter.NewConfig(golinters.NewGovet(govetCfg)). - WithDepsTypeInfo(). + WithLoadForGoAnalysis(). WithPresets(linter.PresetBugs). WithSpeed(4). WithAlternativeNames("vet", "vetshadow"). WithURL("https://golang.org/cmd/vet/"), linter.NewConfig(golinters.NewBodyclose()). - WithDepsTypeInfo(). - WithSSA(). + WithLoadForGoAnalysis(). WithPresets(linter.PresetPerformance, linter.PresetBugs). WithSpeed(4). WithURL("https://github.com/timakin/bodyclose"), linter.NewConfig(golinters.Errcheck{}). - WithTypeInfo(). + WithLoadTypeInfo(). WithPresets(linter.PresetBugs). WithSpeed(10). WithURL("https://github.com/kisielk/errcheck"), @@ -107,54 +106,50 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/golang/lint"), linter.NewConfig(golinters.NewStaticcheck()). - WithDepsTypeInfo(). - WithSSA(). + WithLoadForGoAnalysis(). WithPresets(linter.PresetBugs). WithSpeed(2). WithURL("https://staticcheck.io/"), linter.NewConfig(golinters.NewUnused()). - WithDepsTypeInfo(). - WithSSA(). + WithLoadDepsTypeInfo(). WithPresets(linter.PresetUnused). WithSpeed(5). WithURL("https://github.com/dominikh/go-tools/tree/master/cmd/unused"), linter.NewConfig(golinters.NewGosimple()). - WithDepsTypeInfo(). - WithSSA(). + WithLoadForGoAnalysis(). WithPresets(linter.PresetStyle). WithSpeed(5). WithURL("https://github.com/dominikh/go-tools/tree/master/cmd/gosimple"), linter.NewConfig(golinters.NewStylecheck()). - WithDepsTypeInfo(). - WithSSA(). + WithLoadForGoAnalysis(). WithPresets(linter.PresetStyle). WithSpeed(5). WithURL("https://github.com/dominikh/go-tools/tree/master/stylecheck"), linter.NewConfig(golinters.Gosec{}). - WithTypeInfo(). + WithLoadTypeInfo(). WithPresets(linter.PresetBugs). WithSpeed(8). WithURL("https://github.com/securego/gosec"). WithAlternativeNames("gas"), linter.NewConfig(golinters.Structcheck{}). - WithTypeInfo(). + WithLoadTypeInfo(). WithPresets(linter.PresetUnused). WithSpeed(10). WithURL("https://github.com/opennota/check"), linter.NewConfig(golinters.Varcheck{}). - WithTypeInfo(). + WithLoadTypeInfo(). WithPresets(linter.PresetUnused). WithSpeed(10). WithURL("https://github.com/opennota/check"), linter.NewConfig(golinters.Interfacer{}). - WithDepsTypeInfo(). + WithLoadDepsTypeInfo(). WithSSA(). WithPresets(linter.PresetStyle). WithSpeed(6). WithURL("https://github.com/mvdan/interfacer"), linter.NewConfig(golinters.Unconvert{}). - WithTypeInfo(). + WithLoadTypeInfo(). WithPresets(linter.PresetStyle). WithSpeed(10). WithURL("https://github.com/mdempsky/unconvert"), @@ -171,7 +166,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithSpeed(9). WithURL("https://github.com/jgautheron/goconst"), linter.NewConfig(golinters.Deadcode{}). - WithTypeInfo(). + WithLoadTypeInfo(). WithPresets(linter.PresetUnused). WithSpeed(10). WithURL("https://github.com/remyoudompheng/go-misc/tree/master/deadcode"), @@ -180,7 +175,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithSpeed(8). WithURL("https://github.com/alecthomas/gocyclo"), linter.NewConfig(golinters.TypeCheck{}). - WithTypeInfo(). + WithLoadTypeInfo(). WithPresets(linter.PresetBugs). WithSpeed(10). WithURL(""), @@ -196,12 +191,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithAutoFix(). WithURL("https://godoc.org/golang.org/x/tools/cmd/goimports"), linter.NewConfig(golinters.Maligned{}). - WithTypeInfo(). + WithLoadTypeInfo(). WithPresets(linter.PresetPerformance). WithSpeed(10). WithURL("https://github.com/mdempsky/maligned"), linter.NewConfig(golinters.Depguard{}). - WithTypeInfo(). + WithLoadTypeInfo(). WithPresets(linter.PresetStyle). WithSpeed(6). WithURL("https://github.com/OpenPeeDeeP/depguard"), @@ -217,7 +212,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { linter.NewConfig(golinters.Unparam{}). WithPresets(linter.PresetUnused). WithSpeed(3). - WithDepsTypeInfo(). + WithLoadDepsTypeInfo(). WithSSA(). WithURL("https://github.com/mvdan/unparam"), linter.NewConfig(golinters.Nakedret{}). @@ -235,7 +230,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { linter.NewConfig(golinters.Gocritic{}). WithPresets(linter.PresetStyle). WithSpeed(5). - WithTypeInfo(). + WithLoadTypeInfo(). WithURL("https://github.com/go-critic/go-critic"), linter.NewConfig(golinters.Gochecknoinits{}). WithPresets(linter.PresetStyle). diff --git a/pkg/lint/load.go b/pkg/lint/load.go index a7344773d2f1..31666ffbc863 100644 --- a/pkg/lint/load.go +++ b/pkg/lint/load.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "go/build" + "go/token" "go/types" "os" "path/filepath" @@ -11,6 +12,10 @@ import ( "strings" "time" + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis/load" + + "github.com/golangci/golangci-lint/internal/pkgcache" + "github.com/golangci/golangci-lint/pkg/fsutils" "github.com/pkg/errors" @@ -35,10 +40,12 @@ type ContextLoader struct { pkgTestIDRe *regexp.Regexp lineCache *fsutils.LineCache fileCache *fsutils.FileCache + pkgCache *pkgcache.Cache + loadGuard *load.Guard } func NewContextLoader(cfg *config.Config, log logutils.Log, goenv *goutil.Env, - lineCache *fsutils.LineCache, fileCache *fsutils.FileCache) *ContextLoader { + lineCache *fsutils.LineCache, fileCache *fsutils.FileCache, pkgCache *pkgcache.Cache, loadGuard *load.Guard) *ContextLoader { return &ContextLoader{ cfg: cfg, log: log, @@ -47,10 +54,12 @@ func NewContextLoader(cfg *config.Config, log logutils.Log, goenv *goutil.Env, pkgTestIDRe: regexp.MustCompile(`^(.*) \[(.*)\.test\]`), lineCache: lineCache, fileCache: fileCache, + pkgCache: pkgCache, + loadGuard: loadGuard, } } -func (cl ContextLoader) prepareBuildContext() { +func (cl *ContextLoader) prepareBuildContext() { // Set GOROOT to have working cross-compilation: cross-compiled binaries // have invalid GOROOT. XXX: can't use runtime.GOROOT(). goroot := cl.goenv.Get(goutil.EnvGoRoot) @@ -63,7 +72,7 @@ func (cl ContextLoader) prepareBuildContext() { build.Default.BuildTags = cl.cfg.Run.BuildTags } -func (cl ContextLoader) makeFakeLoaderPackageInfo(pkg *packages.Package) *loader.PackageInfo { +func (cl *ContextLoader) makeFakeLoaderPackageInfo(pkg *packages.Package) *loader.PackageInfo { var errs []error for _, err := range pkg.Errors { errs = append(errs, err) @@ -87,7 +96,7 @@ func (cl ContextLoader) makeFakeLoaderPackageInfo(pkg *packages.Package) *loader } } -func (cl ContextLoader) makeFakeLoaderProgram(pkgs []*packages.Package) *loader.Program { +func (cl *ContextLoader) makeFakeLoaderProgram(pkgs []*packages.Package) *loader.Program { var createdPkgs []*loader.PackageInfo for _, pkg := range pkgs { if pkg.IllTyped { @@ -127,7 +136,7 @@ func (cl ContextLoader) makeFakeLoaderProgram(pkgs []*packages.Package) *loader. } } -func (cl ContextLoader) buildSSAProgram(pkgs []*packages.Package) *ssa.Program { +func (cl *ContextLoader) buildSSAProgram(pkgs []*packages.Package) *ssa.Program { startedAt := time.Now() var pkgsBuiltDuration time.Duration defer func() { @@ -141,23 +150,16 @@ func (cl ContextLoader) buildSSAProgram(pkgs []*packages.Package) *ssa.Program { return ssaProg } -func (cl ContextLoader) findLoadMode(linters []*linter.Config) packages.LoadMode { - //TODO: specify them in linters: need more fine-grained control. - // e.g. NeedTypesSizes is needed only for go vet - loadMode := packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles +func (cl *ContextLoader) findLoadMode(linters []*linter.Config) packages.LoadMode { + loadMode := packages.LoadMode(0) for _, lc := range linters { - if lc.NeedsTypeInfo { - loadMode |= packages.NeedImports | packages.NeedTypes | packages.NeedTypesSizes | packages.NeedTypesInfo | packages.NeedSyntax - } - if lc.NeedsDepsTypeInfo { - loadMode |= packages.NeedDeps - } + loadMode |= lc.LoadMode } return loadMode } -func (cl ContextLoader) buildArgs() []string { +func (cl *ContextLoader) buildArgs() []string { args := cl.cfg.Run.Args if len(args) == 0 { return []string{"./..."} @@ -176,7 +178,7 @@ func (cl ContextLoader) buildArgs() []string { return retArgs } -func (cl ContextLoader) makeBuildFlags() ([]string, error) { +func (cl *ContextLoader) makeBuildFlags() ([]string, error) { var buildFlags []string if len(cl.cfg.Run.BuildTags) != 0 { @@ -229,7 +231,7 @@ func stringifyLoadMode(mode packages.LoadMode) string { return fmt.Sprintf("%d (%s)", mode, strings.Join(flags, "|")) } -func (cl ContextLoader) debugPrintLoadedPackages(pkgs []*packages.Package) { +func (cl *ContextLoader) debugPrintLoadedPackages(pkgs []*packages.Package) { cl.debugf("loaded %d pkgs", len(pkgs)) for i, pkg := range pkgs { var syntaxFiles []string @@ -241,7 +243,7 @@ func (cl ContextLoader) debugPrintLoadedPackages(pkgs []*packages.Package) { } } -func (cl ContextLoader) parseLoadedPackagesErrors(pkgs []*packages.Package) error { +func (cl *ContextLoader) parseLoadedPackagesErrors(pkgs []*packages.Package) error { for _, pkg := range pkgs { for _, err := range pkg.Errors { if strings.Contains(err.Msg, "no Go files") { @@ -257,7 +259,7 @@ func (cl ContextLoader) parseLoadedPackagesErrors(pkgs []*packages.Package) erro return nil } -func (cl ContextLoader) loadPackages(ctx context.Context, loadMode packages.LoadMode) ([]*packages.Package, error) { +func (cl *ContextLoader) loadPackages(ctx context.Context, loadMode packages.LoadMode) ([]*packages.Package, error) { defer func(startedAt time.Time) { cl.log.Infof("Go packages loading at mode %s took %s", stringifyLoadMode(loadMode), time.Since(startedAt)) }(time.Now()) @@ -284,6 +286,16 @@ func (cl ContextLoader) loadPackages(ctx context.Context, loadMode packages.Load if err != nil { return nil, errors.Wrap(err, "failed to load program with go/packages") } + + if loadMode&packages.NeedSyntax == 0 { + // Needed e.g. for go/analysis loading. + fset := token.NewFileSet() + packages.Visit(pkgs, nil, func(pkg *packages.Package) { + pkg.Fset = fset + cl.loadGuard.AddMutexForPkg(pkg) + }) + } + cl.debugPrintLoadedPackages(pkgs) if err := cl.parseLoadedPackagesErrors(pkgs); err != nil { @@ -293,7 +305,7 @@ func (cl ContextLoader) loadPackages(ctx context.Context, loadMode packages.Load return cl.filterTestMainPackages(pkgs), nil } -func (cl ContextLoader) tryParseTestPackage(pkg *packages.Package) (name, testName string, isTest bool) { +func (cl *ContextLoader) tryParseTestPackage(pkg *packages.Package) (name, testName string, isTest bool) { matches := cl.pkgTestIDRe.FindStringSubmatch(pkg.ID) if matches == nil { return "", "", false @@ -302,7 +314,7 @@ func (cl ContextLoader) tryParseTestPackage(pkg *packages.Package) (name, testNa return matches[1], matches[2], true } -func (cl ContextLoader) filterTestMainPackages(pkgs []*packages.Package) []*packages.Package { +func (cl *ContextLoader) filterTestMainPackages(pkgs []*packages.Package) []*packages.Package { var retPkgs []*packages.Package for _, pkg := range pkgs { if pkg.Name == "main" && strings.HasSuffix(pkg.PkgPath, ".test") { @@ -317,7 +329,7 @@ func (cl ContextLoader) filterTestMainPackages(pkgs []*packages.Package) []*pack return retPkgs } -func (cl ContextLoader) filterDuplicatePackages(pkgs []*packages.Package) []*packages.Package { +func (cl *ContextLoader) filterDuplicatePackages(pkgs []*packages.Package) []*packages.Package { packagesWithTests := map[string]bool{} for _, pkg := range pkgs { name, _, isTest := cl.tryParseTestPackage(pkg) @@ -359,7 +371,7 @@ func needSSA(linters []*linter.Config) bool { } //nolint:gocyclo -func (cl ContextLoader) Load(ctx context.Context, linters []*linter.Config) (*linter.Context, error) { +func (cl *ContextLoader) Load(ctx context.Context, linters []*linter.Config) (*linter.Context, error) { loadMode := cl.findLoadMode(linters) pkgs, err := cl.loadPackages(ctx, loadMode) if err != nil { @@ -406,6 +418,8 @@ func (cl ContextLoader) Load(ctx context.Context, linters []*linter.Config) (*li Log: cl.log, FileCache: cl.fileCache, LineCache: cl.lineCache, + PkgCache: cl.pkgCache, + LoadGuard: cl.loadGuard, } separateNotCompilingPackages(ret) diff --git a/pkg/lint/runner.go b/pkg/lint/runner.go index 77a70e5af34f..52a0f0fdfc9e 100644 --- a/pkg/lint/runner.go +++ b/pkg/lint/runner.go @@ -9,6 +9,7 @@ import ( "sync" "time" + "github.com/golangci/golangci-lint/internal/errorutil" "github.com/golangci/golangci-lint/pkg/lint/lintersdb" "github.com/golangci/golangci-lint/pkg/fsutils" @@ -102,8 +103,13 @@ func (r *Runner) runLinterSafe(ctx context.Context, lintCtx *linter.Context, lc *linter.Config) (ret []result.Issue, err error) { defer func() { if panicData := recover(); panicData != nil { - err = fmt.Errorf("panic occurred: %s", panicData) - r.Log.Warnf("Panic stack trace: %s", debug.Stack()) + if pe, ok := panicData.(*errorutil.PanicError); ok { + // Don't print stacktrace from goroutines twice + lintCtx.Log.Warnf("Panic: %s: %s", pe, pe.Stack()) + } else { + err = fmt.Errorf("panic occurred: %s", panicData) + r.Log.Warnf("Panic stack trace: %s", debug.Stack()) + } } }() @@ -272,7 +278,9 @@ func (r Runner) printPerProcessorStat(stat map[string]processorStat) { parts = append(parts, fmt.Sprintf("%s: %d/%d", name, ps.outCount, ps.inCount)) } } - r.Log.Infof("Processors filtering stat (out/in): %s", strings.Join(parts, ", ")) + if len(parts) != 0 { + r.Log.Infof("Processors filtering stat (out/in): %s", strings.Join(parts, ", ")) + } } func collectIssues(resCh <-chan lintRes) <-chan result.Issue { diff --git a/pkg/timeutils/stopwatch.go b/pkg/timeutils/stopwatch.go index 4390bf35a937..78d1e3cdb0fd 100644 --- a/pkg/timeutils/stopwatch.go +++ b/pkg/timeutils/stopwatch.go @@ -34,6 +34,10 @@ type stageDuration struct { } func (s *Stopwatch) sprintStages() string { + if len(s.stages) == 0 { + return "no stages" + } + stageDurations := []stageDuration{} for n, d := range s.stages { stageDurations = append(stageDurations, stageDuration{ diff --git a/test/enabled_linters_test.go b/test/enabled_linters_test.go index 4eb5574e5d70..47aad3b59b75 100644 --- a/test/enabled_linters_test.go +++ b/test/enabled_linters_test.go @@ -25,7 +25,7 @@ func getEnabledByDefaultFastLintersExcept(except ...string) []string { ebdl := m.GetAllEnabledByDefaultLinters() ret := []string{} for _, lc := range ebdl { - if lc.NeedsDepsTypeInfo { + if lc.IsSlowLinter() { continue } @@ -41,7 +41,7 @@ func getAllFastLintersWith(with ...string) []string { linters := lintersdb.NewManager(nil).GetAllSupportedLinterConfigs() ret := append([]string{}, with...) for _, lc := range linters { - if lc.NeedsDepsTypeInfo { + if lc.IsSlowLinter() { continue } ret = append(ret, lc.Name()) @@ -64,7 +64,7 @@ func getEnabledByDefaultFastLintersWith(with ...string) []string { ebdl := lintersdb.NewManager(nil).GetAllEnabledByDefaultLinters() ret := append([]string{}, with...) for _, lc := range ebdl { - if lc.NeedsDepsTypeInfo { + if lc.IsSlowLinter() { continue } diff --git a/test/testdata/staticcheck.go b/test/testdata/staticcheck.go index 5cdd1abb1753..6297b8a2e9c5 100644 --- a/test/testdata/staticcheck.go +++ b/test/testdata/staticcheck.go @@ -1,6 +1,10 @@ //args: -Estaticcheck package testdata +import ( + "runtime" +) + func Staticcheck() { var x int x = x // ERROR "self-assignment of x to x" @@ -15,3 +19,7 @@ func StaticcheckNolintMegacheck() { var x int x = x //nolint:megacheck } + +func StaticcheckDeprecated() { + _ = runtime.CPUProfile() // ERROR "SA1019: runtime.CPUProfile is deprecated" +} diff --git a/vendor/github.com/golangci/go-tools/callgraph/callgraph.go b/vendor/github.com/golangci/go-tools/callgraph/callgraph.go deleted file mode 100644 index 713a80383f22..000000000000 --- a/vendor/github.com/golangci/go-tools/callgraph/callgraph.go +++ /dev/null @@ -1,129 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -/* - -Package callgraph defines the call graph and various algorithms -and utilities to operate on it. - -A call graph is a labelled directed graph whose nodes represent -functions and whose edge labels represent syntactic function call -sites. The presence of a labelled edge (caller, site, callee) -indicates that caller may call callee at the specified call site. - -A call graph is a multigraph: it may contain multiple edges (caller, -*, callee) connecting the same pair of nodes, so long as the edges -differ by label; this occurs when one function calls another function -from multiple call sites. Also, it may contain multiple edges -(caller, site, *) that differ only by callee; this indicates a -polymorphic call. - -A SOUND call graph is one that overapproximates the dynamic calling -behaviors of the program in all possible executions. One call graph -is more PRECISE than another if it is a smaller overapproximation of -the dynamic behavior. - -All call graphs have a synthetic root node which is responsible for -calling main() and init(). - -Calls to built-in functions (e.g. panic, println) are not represented -in the call graph; they are treated like built-in operators of the -language. - -*/ -package callgraph // import "github.com/golangci/go-tools/callgraph" - -// TODO(adonovan): add a function to eliminate wrappers from the -// callgraph, preserving topology. -// More generally, we could eliminate "uninteresting" nodes such as -// nodes from packages we don't care about. - -import ( - "fmt" - "go/token" - - "github.com/golangci/go-tools/ssa" -) - -// A Graph represents a call graph. -// -// A graph may contain nodes that are not reachable from the root. -// If the call graph is sound, such nodes indicate unreachable -// functions. -// -type Graph struct { - Root *Node // the distinguished root node - Nodes map[*ssa.Function]*Node // all nodes by function -} - -// New returns a new Graph with the specified root node. -func New(root *ssa.Function) *Graph { - g := &Graph{Nodes: make(map[*ssa.Function]*Node)} - g.Root = g.CreateNode(root) - return g -} - -// CreateNode returns the Node for fn, creating it if not present. -func (g *Graph) CreateNode(fn *ssa.Function) *Node { - n, ok := g.Nodes[fn] - if !ok { - n = &Node{Func: fn, ID: len(g.Nodes)} - g.Nodes[fn] = n - } - return n -} - -// A Node represents a node in a call graph. -type Node struct { - Func *ssa.Function // the function this node represents - ID int // 0-based sequence number - In []*Edge // unordered set of incoming call edges (n.In[*].Callee == n) - Out []*Edge // unordered set of outgoing call edges (n.Out[*].Caller == n) -} - -func (n *Node) String() string { - return fmt.Sprintf("n%d:%s", n.ID, n.Func) -} - -// A Edge represents an edge in the call graph. -// -// Site is nil for edges originating in synthetic or intrinsic -// functions, e.g. reflect.Call or the root of the call graph. -type Edge struct { - Caller *Node - Site ssa.CallInstruction - Callee *Node -} - -func (e Edge) String() string { - return fmt.Sprintf("%s --> %s", e.Caller, e.Callee) -} - -func (e Edge) Description() string { - var prefix string - switch e.Site.(type) { - case nil: - return "synthetic call" - case *ssa.Go: - prefix = "concurrent " - case *ssa.Defer: - prefix = "deferred " - } - return prefix + e.Site.Common().Description() -} - -func (e Edge) Pos() token.Pos { - if e.Site == nil { - return token.NoPos - } - return e.Site.Pos() -} - -// AddEdge adds the edge (caller, site, callee) to the call graph. -// Elimination of duplicate edges is the caller's responsibility. -func AddEdge(caller *Node, site ssa.CallInstruction, callee *Node) { - e := &Edge{caller, site, callee} - callee.In = append(callee.In, e) - caller.Out = append(caller.Out, e) -} diff --git a/vendor/github.com/golangci/go-tools/callgraph/static/static.go b/vendor/github.com/golangci/go-tools/callgraph/static/static.go deleted file mode 100644 index 6b4e1e3ba35b..000000000000 --- a/vendor/github.com/golangci/go-tools/callgraph/static/static.go +++ /dev/null @@ -1,35 +0,0 @@ -// Package static computes the call graph of a Go program containing -// only static call edges. -package static // import "github.com/golangci/go-tools/callgraph/static" - -import ( - "github.com/golangci/go-tools/callgraph" - "github.com/golangci/go-tools/ssa" - "github.com/golangci/go-tools/ssa/ssautil" -) - -// CallGraph computes the call graph of the specified program -// considering only static calls. -// -func CallGraph(prog *ssa.Program) *callgraph.Graph { - cg := callgraph.New(nil) // TODO(adonovan) eliminate concept of rooted callgraph - - // TODO(adonovan): opt: use only a single pass over the ssa.Program. - // TODO(adonovan): opt: this is slower than RTA (perhaps because - // the lower precision means so many edges are allocated)! - for f := range ssautil.AllFunctions(prog) { - fnode := cg.CreateNode(f) - for _, b := range f.Blocks { - for _, instr := range b.Instrs { - if site, ok := instr.(ssa.CallInstruction); ok { - if g := site.Common().StaticCallee(); g != nil { - gnode := cg.CreateNode(g) - callgraph.AddEdge(fnode, site, gnode) - } - } - } - } - } - - return cg -} diff --git a/vendor/github.com/golangci/go-tools/callgraph/util.go b/vendor/github.com/golangci/go-tools/callgraph/util.go deleted file mode 100644 index 2e8ded1dfa06..000000000000 --- a/vendor/github.com/golangci/go-tools/callgraph/util.go +++ /dev/null @@ -1,181 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package callgraph - -import "github.com/golangci/go-tools/ssa" - -// This file provides various utilities over call graphs, such as -// visitation and path search. - -// CalleesOf returns a new set containing all direct callees of the -// caller node. -// -func CalleesOf(caller *Node) map[*Node]bool { - callees := make(map[*Node]bool) - for _, e := range caller.Out { - callees[e.Callee] = true - } - return callees -} - -// GraphVisitEdges visits all the edges in graph g in depth-first order. -// The edge function is called for each edge in postorder. If it -// returns non-nil, visitation stops and GraphVisitEdges returns that -// value. -// -func GraphVisitEdges(g *Graph, edge func(*Edge) error) error { - seen := make(map[*Node]bool) - var visit func(n *Node) error - visit = func(n *Node) error { - if !seen[n] { - seen[n] = true - for _, e := range n.Out { - if err := visit(e.Callee); err != nil { - return err - } - if err := edge(e); err != nil { - return err - } - } - } - return nil - } - for _, n := range g.Nodes { - if err := visit(n); err != nil { - return err - } - } - return nil -} - -// PathSearch finds an arbitrary path starting at node start and -// ending at some node for which isEnd() returns true. On success, -// PathSearch returns the path as an ordered list of edges; on -// failure, it returns nil. -// -func PathSearch(start *Node, isEnd func(*Node) bool) []*Edge { - stack := make([]*Edge, 0, 32) - seen := make(map[*Node]bool) - var search func(n *Node) []*Edge - search = func(n *Node) []*Edge { - if !seen[n] { - seen[n] = true - if isEnd(n) { - return stack - } - for _, e := range n.Out { - stack = append(stack, e) // push - if found := search(e.Callee); found != nil { - return found - } - stack = stack[:len(stack)-1] // pop - } - } - return nil - } - return search(start) -} - -// DeleteSyntheticNodes removes from call graph g all nodes for -// synthetic functions (except g.Root and package initializers), -// preserving the topology. In effect, calls to synthetic wrappers -// are "inlined". -// -func (g *Graph) DeleteSyntheticNodes() { - // Measurements on the standard library and go.tools show that - // resulting graph has ~15% fewer nodes and 4-8% fewer edges - // than the input. - // - // Inlining a wrapper of in-degree m, out-degree n adds m*n - // and removes m+n edges. Since most wrappers are monomorphic - // (n=1) this results in a slight reduction. Polymorphic - // wrappers (n>1), e.g. from embedding an interface value - // inside a struct to satisfy some interface, cause an - // increase in the graph, but they seem to be uncommon. - - // Hash all existing edges to avoid creating duplicates. - edges := make(map[Edge]bool) - for _, cgn := range g.Nodes { - for _, e := range cgn.Out { - edges[*e] = true - } - } - for fn, cgn := range g.Nodes { - if cgn == g.Root || fn.Synthetic == "" || isInit(cgn.Func) { - continue // keep - } - for _, eIn := range cgn.In { - for _, eOut := range cgn.Out { - newEdge := Edge{eIn.Caller, eIn.Site, eOut.Callee} - if edges[newEdge] { - continue // don't add duplicate - } - AddEdge(eIn.Caller, eIn.Site, eOut.Callee) - edges[newEdge] = true - } - } - g.DeleteNode(cgn) - } -} - -func isInit(fn *ssa.Function) bool { - return fn.Pkg != nil && fn.Pkg.Func("init") == fn -} - -// DeleteNode removes node n and its edges from the graph g. -// (NB: not efficient for batch deletion.) -func (g *Graph) DeleteNode(n *Node) { - n.deleteIns() - n.deleteOuts() - delete(g.Nodes, n.Func) -} - -// deleteIns deletes all incoming edges to n. -func (n *Node) deleteIns() { - for _, e := range n.In { - removeOutEdge(e) - } - n.In = nil -} - -// deleteOuts deletes all outgoing edges from n. -func (n *Node) deleteOuts() { - for _, e := range n.Out { - removeInEdge(e) - } - n.Out = nil -} - -// removeOutEdge removes edge.Caller's outgoing edge 'edge'. -func removeOutEdge(edge *Edge) { - caller := edge.Caller - n := len(caller.Out) - for i, e := range caller.Out { - if e == edge { - // Replace it with the final element and shrink the slice. - caller.Out[i] = caller.Out[n-1] - caller.Out[n-1] = nil // aid GC - caller.Out = caller.Out[:n-1] - return - } - } - panic("edge not found: " + edge.String()) -} - -// removeInEdge removes edge.Callee's incoming edge 'edge'. -func removeInEdge(edge *Edge) { - caller := edge.Callee - n := len(caller.In) - for i, e := range caller.In { - if e == edge { - // Replace it with the final element and shrink the slice. - caller.In[i] = caller.In[n-1] - caller.In[n-1] = nil // aid GC - caller.In = caller.In[:n-1] - return - } - } - panic("edge not found: " + edge.String()) -} diff --git a/vendor/github.com/golangci/go-tools/deprecated/stdlib.go b/vendor/github.com/golangci/go-tools/deprecated/stdlib.go deleted file mode 100644 index b6b217c3e37d..000000000000 --- a/vendor/github.com/golangci/go-tools/deprecated/stdlib.go +++ /dev/null @@ -1,54 +0,0 @@ -package deprecated - -type Deprecation struct { - DeprecatedSince int - AlternativeAvailableSince int -} - -var Stdlib = map[string]Deprecation{ - "image/jpeg.Reader": {4, 0}, - // FIXME(dh): AllowBinary isn't being detected as deprecated - // because the comment has a newline right after "Deprecated:" - "go/build.AllowBinary": {7, 7}, - "(archive/zip.FileHeader).CompressedSize": {1, 1}, - "(archive/zip.FileHeader).UncompressedSize": {1, 1}, - "(go/doc.Package).Bugs": {1, 1}, - "os.SEEK_SET": {7, 7}, - "os.SEEK_CUR": {7, 7}, - "os.SEEK_END": {7, 7}, - "(net.Dialer).Cancel": {7, 7}, - "runtime.CPUProfile": {9, 0}, - "compress/flate.ReadError": {6, 6}, - "compress/flate.WriteError": {6, 6}, - "path/filepath.HasPrefix": {0, 0}, - "(net/http.Transport).Dial": {7, 7}, - "(*net/http.Transport).CancelRequest": {6, 5}, - "net/http.ErrWriteAfterFlush": {7, 0}, - "net/http.ErrHeaderTooLong": {8, 0}, - "net/http.ErrShortBody": {8, 0}, - "net/http.ErrMissingContentLength": {8, 0}, - "net/http/httputil.ErrPersistEOF": {0, 0}, - "net/http/httputil.ErrClosed": {0, 0}, - "net/http/httputil.ErrPipeline": {0, 0}, - "net/http/httputil.ServerConn": {0, 0}, - "net/http/httputil.NewServerConn": {0, 0}, - "net/http/httputil.ClientConn": {0, 0}, - "net/http/httputil.NewClientConn": {0, 0}, - "net/http/httputil.NewProxyClientConn": {0, 0}, - "(net/http.Request).Cancel": {7, 7}, - "(text/template/parse.PipeNode).Line": {1, 1}, - "(text/template/parse.ActionNode).Line": {1, 1}, - "(text/template/parse.BranchNode).Line": {1, 1}, - "(text/template/parse.TemplateNode).Line": {1, 1}, - "database/sql/driver.ColumnConverter": {9, 9}, - "database/sql/driver.Execer": {8, 8}, - "database/sql/driver.Queryer": {8, 8}, - "(database/sql/driver.Conn).Begin": {8, 8}, - "(database/sql/driver.Stmt).Exec": {8, 8}, - "(database/sql/driver.Stmt).Query": {8, 8}, - "syscall.StringByteSlice": {1, 1}, - "syscall.StringBytePtr": {1, 1}, - "syscall.StringSlicePtr": {1, 1}, - "syscall.StringToUTF16": {1, 1}, - "syscall.StringToUTF16Ptr": {1, 1}, -} diff --git a/vendor/github.com/golangci/go-tools/functions/concrete.go b/vendor/github.com/golangci/go-tools/functions/concrete.go deleted file mode 100644 index 4a47e9fa8667..000000000000 --- a/vendor/github.com/golangci/go-tools/functions/concrete.go +++ /dev/null @@ -1,56 +0,0 @@ -package functions - -import ( - "go/token" - "go/types" - - "github.com/golangci/go-tools/ssa" -) - -func concreteReturnTypes(fn *ssa.Function) []*types.Tuple { - res := fn.Signature.Results() - if res == nil { - return nil - } - ifaces := make([]bool, res.Len()) - any := false - for i := 0; i < res.Len(); i++ { - _, ifaces[i] = res.At(i).Type().Underlying().(*types.Interface) - any = any || ifaces[i] - } - if !any { - return []*types.Tuple{res} - } - var out []*types.Tuple - for _, block := range fn.Blocks { - if len(block.Instrs) == 0 { - continue - } - ret, ok := block.Instrs[len(block.Instrs)-1].(*ssa.Return) - if !ok { - continue - } - vars := make([]*types.Var, res.Len()) - for i, v := range ret.Results { - var typ types.Type - if !ifaces[i] { - typ = res.At(i).Type() - } else if mi, ok := v.(*ssa.MakeInterface); ok { - // TODO(dh): if mi.X is a function call that returns - // an interface, call concreteReturnTypes on that - // function (or, really, go through Descriptions, - // avoid infinite recursion etc, just like nil error - // detection) - - // TODO(dh): support Phi nodes - typ = mi.X.Type() - } else { - typ = res.At(i).Type() - } - vars[i] = types.NewParam(token.NoPos, nil, "", typ) - } - out = append(out, types.NewTuple(vars...)) - } - // TODO(dh): deduplicate out - return out -} diff --git a/vendor/github.com/golangci/go-tools/functions/functions.go b/vendor/github.com/golangci/go-tools/functions/functions.go deleted file mode 100644 index e86796039038..000000000000 --- a/vendor/github.com/golangci/go-tools/functions/functions.go +++ /dev/null @@ -1,150 +0,0 @@ -package functions - -import ( - "go/types" - "sync" - - "github.com/golangci/go-tools/callgraph" - "github.com/golangci/go-tools/callgraph/static" - "github.com/golangci/go-tools/ssa" - "github.com/golangci/go-tools/staticcheck/vrp" -) - -var stdlibDescs = map[string]Description{ - "errors.New": {Pure: true}, - - "fmt.Errorf": {Pure: true}, - "fmt.Sprintf": {Pure: true}, - "fmt.Sprint": {Pure: true}, - - "sort.Reverse": {Pure: true}, - - "strings.Map": {Pure: true}, - "strings.Repeat": {Pure: true}, - "strings.Replace": {Pure: true}, - "strings.Title": {Pure: true}, - "strings.ToLower": {Pure: true}, - "strings.ToLowerSpecial": {Pure: true}, - "strings.ToTitle": {Pure: true}, - "strings.ToTitleSpecial": {Pure: true}, - "strings.ToUpper": {Pure: true}, - "strings.ToUpperSpecial": {Pure: true}, - "strings.Trim": {Pure: true}, - "strings.TrimFunc": {Pure: true}, - "strings.TrimLeft": {Pure: true}, - "strings.TrimLeftFunc": {Pure: true}, - "strings.TrimPrefix": {Pure: true}, - "strings.TrimRight": {Pure: true}, - "strings.TrimRightFunc": {Pure: true}, - "strings.TrimSpace": {Pure: true}, - "strings.TrimSuffix": {Pure: true}, - - "(*net/http.Request).WithContext": {Pure: true}, - - "math/rand.Read": {NilError: true}, - "(*math/rand.Rand).Read": {NilError: true}, -} - -type Description struct { - // The function is known to be pure - Pure bool - // The function is known to be a stub - Stub bool - // The function is known to never return (panics notwithstanding) - Infinite bool - // Variable ranges - Ranges vrp.Ranges - Loops []Loop - // Function returns an error as its last argument, but it is - // always nil - NilError bool - ConcreteReturnTypes []*types.Tuple -} - -type descriptionEntry struct { - ready chan struct{} - result Description -} - -type Descriptions struct { - CallGraph *callgraph.Graph - mu sync.Mutex - cache map[*ssa.Function]*descriptionEntry -} - -func NewDescriptions(prog *ssa.Program) *Descriptions { - return &Descriptions{ - CallGraph: static.CallGraph(prog), - cache: map[*ssa.Function]*descriptionEntry{}, - } -} - -func (d *Descriptions) Get(fn *ssa.Function) Description { - d.mu.Lock() - fd := d.cache[fn] - if fd == nil { - fd = &descriptionEntry{ - ready: make(chan struct{}), - } - d.cache[fn] = fd - d.mu.Unlock() - - { - fd.result = stdlibDescs[fn.RelString(nil)] - fd.result.Pure = fd.result.Pure || d.IsPure(fn) - fd.result.Stub = fd.result.Stub || d.IsStub(fn) - fd.result.Infinite = fd.result.Infinite || !terminates(fn) - fd.result.Ranges = vrp.BuildGraph(fn).Solve() - fd.result.Loops = findLoops(fn) - fd.result.NilError = fd.result.NilError || IsNilError(fn) - fd.result.ConcreteReturnTypes = concreteReturnTypes(fn) - } - - close(fd.ready) - } else { - d.mu.Unlock() - <-fd.ready - } - return fd.result -} - -func IsNilError(fn *ssa.Function) bool { - // TODO(dh): This is very simplistic, as we only look for constant - // nil returns. A more advanced approach would work transitively. - // An even more advanced approach would be context-aware and - // determine nil errors based on inputs (e.g. io.WriteString to a - // bytes.Buffer will always return nil, but an io.WriteString to - // an os.File might not). Similarly, an os.File opened for reading - // won't error on Close, but other files will. - res := fn.Signature.Results() - if res.Len() == 0 { - return false - } - last := res.At(res.Len() - 1) - if types.TypeString(last.Type(), nil) != "error" { - return false - } - - if fn.Blocks == nil { - return false - } - for _, block := range fn.Blocks { - if len(block.Instrs) == 0 { - continue - } - ins := block.Instrs[len(block.Instrs)-1] - ret, ok := ins.(*ssa.Return) - if !ok { - continue - } - v := ret.Results[len(ret.Results)-1] - c, ok := v.(*ssa.Const) - if !ok { - return false - } - if !c.IsNil() { - return false - } - } - return true -} diff --git a/vendor/github.com/golangci/go-tools/functions/pure.go b/vendor/github.com/golangci/go-tools/functions/pure.go deleted file mode 100644 index ae084073fbed..000000000000 --- a/vendor/github.com/golangci/go-tools/functions/pure.go +++ /dev/null @@ -1,123 +0,0 @@ -package functions - -import ( - "go/token" - "go/types" - - "github.com/golangci/go-tools/callgraph" - "github.com/golangci/go-tools/lint/lintdsl" - "github.com/golangci/go-tools/ssa" -) - -// IsStub reports whether a function is a stub. A function is -// considered a stub if it has no instructions or exactly one -// instruction, which must be either returning only constant values or -// a panic. -func (d *Descriptions) IsStub(fn *ssa.Function) bool { - if len(fn.Blocks) == 0 { - return true - } - if len(fn.Blocks) > 1 { - return false - } - instrs := lintdsl.FilterDebug(fn.Blocks[0].Instrs) - if len(instrs) != 1 { - return false - } - - switch instrs[0].(type) { - case *ssa.Return: - // Since this is the only instruction, the return value must - // be a constant. We consider all constants as stubs, not just - // the zero value. This does not, unfortunately, cover zero - // initialised structs, as these cause additional - // instructions. - return true - case *ssa.Panic: - return true - default: - return false - } -} - -func (d *Descriptions) IsPure(fn *ssa.Function) bool { - if fn.Signature.Results().Len() == 0 { - // A function with no return values is empty or is doing some - // work we cannot see (for example because of build tags); - // don't consider it pure. - return false - } - - for _, param := range fn.Params { - if _, ok := param.Type().Underlying().(*types.Basic); !ok { - return false - } - } - - if fn.Blocks == nil { - return false - } - checkCall := func(common *ssa.CallCommon) bool { - if common.IsInvoke() { - return false - } - builtin, ok := common.Value.(*ssa.Builtin) - if !ok { - if common.StaticCallee() != fn { - if common.StaticCallee() == nil { - return false - } - // TODO(dh): ideally, IsPure wouldn't be responsible - // for avoiding infinite recursion, but - // FunctionDescriptions would be. - node := d.CallGraph.CreateNode(common.StaticCallee()) - if callgraph.PathSearch(node, func(other *callgraph.Node) bool { - return other.Func == fn - }) != nil { - return false - } - if !d.Get(common.StaticCallee()).Pure { - return false - } - } - } else { - switch builtin.Name() { - case "len", "cap", "make", "new": - default: - return false - } - } - return true - } - for _, b := range fn.Blocks { - for _, ins := range b.Instrs { - switch ins := ins.(type) { - case *ssa.Call: - if !checkCall(ins.Common()) { - return false - } - case *ssa.Defer: - if !checkCall(&ins.Call) { - return false - } - case *ssa.Select: - return false - case *ssa.Send: - return false - case *ssa.Go: - return false - case *ssa.Panic: - return false - case *ssa.Store: - return false - case *ssa.FieldAddr: - return false - case *ssa.UnOp: - if ins.Op == token.MUL || ins.Op == token.AND { - return false - } - } - } - } - return true -} diff --git a/vendor/github.com/golangci/go-tools/lint/generated.go b/vendor/github.com/golangci/go-tools/lint/generated.go deleted file mode 100644 index 58b23f68f97c..000000000000 --- a/vendor/github.com/golangci/go-tools/lint/generated.go +++ /dev/null @@ -1,38 +0,0 @@ -package lint - -import ( - "bufio" - "bytes" - "io" -) - -var ( - // used by cgo before Go 1.11 - oldCgo = []byte("// Created by cgo - DO NOT EDIT") - prefix = []byte("// Code generated ") - suffix = []byte(" DO NOT EDIT.") - nl = []byte("\n") - crnl = []byte("\r\n") -) - -func isGenerated(r io.Reader) bool { - br := bufio.NewReader(r) - for { - s, err := br.ReadBytes('\n') - if err != nil && err != io.EOF { - return false - } - s = bytes.TrimSuffix(s, crnl) - s = bytes.TrimSuffix(s, nl) - if bytes.HasPrefix(s, prefix) && bytes.HasSuffix(s, suffix) { - return true - } - if bytes.Equal(s, oldCgo) { - return true - } - if err == io.EOF { - break - } - } - return false -} diff --git a/vendor/github.com/golangci/go-tools/lint/lint.go b/vendor/github.com/golangci/go-tools/lint/lint.go deleted file mode 100644 index e9aa7933fdb5..000000000000 --- a/vendor/github.com/golangci/go-tools/lint/lint.go +++ /dev/null @@ -1,706 +0,0 @@ -// Package lint provides the foundation for tools like staticcheck -package lint // import "github.com/golangci/go-tools/lint" - -import ( - "fmt" - "go/ast" - "go/token" - "go/types" - "io" - "os" - "path/filepath" - "sort" - "strings" - "sync" - "time" - "unicode" - - "golang.org/x/tools/go/packages" - "github.com/golangci/go-tools/config" - "github.com/golangci/go-tools/ssa" - "github.com/golangci/go-tools/ssa/ssautil" -) - -type Job struct { - Program *Program - - checker string - check Check - problems []Problem - - duration time.Duration -} - -type Ignore interface { - Match(p Problem) bool -} - -type LineIgnore struct { - File string - Line int - Checks []string - matched bool - pos token.Pos -} - -func (li *LineIgnore) Match(p Problem) bool { - if p.Position.Filename != li.File || p.Position.Line != li.Line { - return false - } - for _, c := range li.Checks { - if m, _ := filepath.Match(c, p.Check); m { - li.matched = true - return true - } - } - return false -} - -func (li *LineIgnore) String() string { - matched := "not matched" - if li.matched { - matched = "matched" - } - return fmt.Sprintf("%s:%d %s (%s)", li.File, li.Line, strings.Join(li.Checks, ", "), matched) -} - -type FileIgnore struct { - File string - Checks []string -} - -func (fi *FileIgnore) Match(p Problem) bool { - if p.Position.Filename != fi.File { - return false - } - for _, c := range fi.Checks { - if m, _ := filepath.Match(c, p.Check); m { - return true - } - } - return false -} - -type GlobIgnore struct { - Pattern string - Checks []string -} - -func (gi *GlobIgnore) Match(p Problem) bool { - if gi.Pattern != "*" { - pkgpath := p.Package.Types.Path() - if strings.HasSuffix(pkgpath, "_test") { - pkgpath = pkgpath[:len(pkgpath)-len("_test")] - } - name := filepath.Join(pkgpath, filepath.Base(p.Position.Filename)) - if m, _ := filepath.Match(gi.Pattern, name); !m { - return false - } - } - for _, c := range gi.Checks { - if m, _ := filepath.Match(c, p.Check); m { - return true - } - } - return false -} - -type Program struct { - SSA *ssa.Program - InitialPackages []*Pkg - InitialFunctions []*ssa.Function - AllPackages []*packages.Package - AllFunctions []*ssa.Function - Files []*ast.File - GoVersion int - - tokenFileMap map[*token.File]*ast.File - astFileMap map[*ast.File]*Pkg - packagesMap map[string]*packages.Package - - genMu sync.RWMutex - generatedMap map[string]bool -} - -func (prog *Program) Fset() *token.FileSet { - return prog.InitialPackages[0].Fset -} - -type Func func(*Job) - -type Severity uint8 - -const ( - Error Severity = iota - Warning - Ignored -) - -// Problem represents a problem in some source code. -type Problem struct { - Position token.Position // position in source file - Text string // the prose that describes the problem - Check string - Checker string - Package *Pkg - Severity Severity -} - -func (p *Problem) String() string { - if p.Check == "" { - return p.Text - } - return fmt.Sprintf("%s (%s)", p.Text, p.Check) -} - -type Checker interface { - Name() string - Prefix() string - Init(*Program) - Checks() []Check -} - -type Check struct { - Fn Func - ID string - FilterGenerated bool -} - -// A Linter lints Go source code. -type Linter struct { - Checkers []Checker - Ignores []Ignore - GoVersion int - ReturnIgnored bool - Config config.Config - - MaxConcurrentJobs int - PrintStats bool - - automaticIgnores []Ignore -} - -func (l *Linter) ignore(p Problem) bool { - ignored := false - for _, ig := range l.automaticIgnores { - // We cannot short-circuit these, as we want to record, for - // each ignore, whether it matched or not. - if ig.Match(p) { - ignored = true - } - } - if ignored { - // no need to execute other ignores if we've already had a - // match. - return true - } - for _, ig := range l.Ignores { - // We can short-circuit here, as we aren't tracking any - // information. - if ig.Match(p) { - return true - } - } - - return false -} - -func (prog *Program) File(node Positioner) *ast.File { - return prog.tokenFileMap[prog.SSA.Fset.File(node.Pos())] -} - -func (j *Job) File(node Positioner) *ast.File { - return j.Program.File(node) -} - -func parseDirective(s string) (cmd string, args []string) { - if !strings.HasPrefix(s, "//lint:") { - return "", nil - } - s = strings.TrimPrefix(s, "//lint:") - fields := strings.Split(s, " ") - return fields[0], fields[1:] -} - -type PerfStats struct { - PackageLoading time.Duration - SSABuild time.Duration - OtherInitWork time.Duration - CheckerInits map[string]time.Duration - Jobs []JobStat -} - -type JobStat struct { - Job string - Duration time.Duration -} - -func (stats *PerfStats) Print(w io.Writer) { - fmt.Fprintln(w, "Package loading:", stats.PackageLoading) - fmt.Fprintln(w, "SSA build:", stats.SSABuild) - fmt.Fprintln(w, "Other init work:", stats.OtherInitWork) - - fmt.Fprintln(w, "Checker inits:") - for checker, d := range stats.CheckerInits { - fmt.Fprintf(w, "\t%s: %s\n", checker, d) - } - fmt.Fprintln(w) - - fmt.Fprintln(w, "Jobs:") - sort.Slice(stats.Jobs, func(i, j int) bool { - return stats.Jobs[i].Duration < stats.Jobs[j].Duration - }) - var total time.Duration - for _, job := range stats.Jobs { - fmt.Fprintf(w, "\t%s: %s\n", job.Job, job.Duration) - total += job.Duration - } - fmt.Fprintf(w, "\tTotal: %s\n", total) -} - -func (l *Linter) Lint(initial []*packages.Package, stats *PerfStats) []Problem { - allPkgs := allPackages(initial) - t := time.Now() - ssaprog, _ := ssautil.Packages(allPkgs, ssa.GlobalDebug) - ssaprog.Build() - if stats != nil { - stats.SSABuild = time.Since(t) - } - - t = time.Now() - pkgMap := map[*ssa.Package]*Pkg{} - var pkgs []*Pkg - for _, pkg := range initial { - ssapkg := ssaprog.Package(pkg.Types) - var cfg config.Config - if len(pkg.GoFiles) != 0 { - path := pkg.GoFiles[0] - dir := filepath.Dir(path) - var err error - // OPT(dh): we're rebuilding the entire config tree for - // each package. for example, if we check a/b/c and - // a/b/c/d, we'll process a, a/b, a/b/c, a, a/b, a/b/c, - // a/b/c/d – we should cache configs per package and only - // load the new levels. - cfg, err = config.Load(dir) - if err != nil { - // FIXME(dh): we couldn't load the config, what are we - // supposed to do? probably tell the user somehow - } - cfg = cfg.Merge(l.Config) - } - - pkg := &Pkg{ - SSA: ssapkg, - Package: pkg, - Config: cfg, - } - pkgMap[ssapkg] = pkg - pkgs = append(pkgs, pkg) - } - - prog := &Program{ - SSA: ssaprog, - InitialPackages: pkgs, - AllPackages: allPkgs, - GoVersion: l.GoVersion, - tokenFileMap: map[*token.File]*ast.File{}, - astFileMap: map[*ast.File]*Pkg{}, - generatedMap: map[string]bool{}, - } - prog.packagesMap = map[string]*packages.Package{} - for _, pkg := range allPkgs { - prog.packagesMap[pkg.Types.Path()] = pkg - } - - isInitial := map[*types.Package]struct{}{} - for _, pkg := range pkgs { - isInitial[pkg.Types] = struct{}{} - } - for fn := range ssautil.AllFunctions(ssaprog) { - if fn.Pkg == nil { - continue - } - prog.AllFunctions = append(prog.AllFunctions, fn) - if _, ok := isInitial[fn.Pkg.Pkg]; ok { - prog.InitialFunctions = append(prog.InitialFunctions, fn) - } - } - for _, pkg := range pkgs { - prog.Files = append(prog.Files, pkg.Syntax...) - - ssapkg := ssaprog.Package(pkg.Types) - for _, f := range pkg.Syntax { - prog.astFileMap[f] = pkgMap[ssapkg] - } - } - - for _, pkg := range allPkgs { - for _, f := range pkg.Syntax { - tf := pkg.Fset.File(f.Pos()) - prog.tokenFileMap[tf] = f - } - } - - var out []Problem - l.automaticIgnores = nil - for _, pkg := range initial { - for _, f := range pkg.Syntax { - cm := ast.NewCommentMap(pkg.Fset, f, f.Comments) - for node, cgs := range cm { - for _, cg := range cgs { - for _, c := range cg.List { - if !strings.HasPrefix(c.Text, "//lint:") { - continue - } - cmd, args := parseDirective(c.Text) - switch cmd { - case "ignore", "file-ignore": - if len(args) < 2 { - // FIXME(dh): this causes duplicated warnings when using megacheck - p := Problem{ - Position: prog.DisplayPosition(c.Pos()), - Text: "malformed linter directive; missing the required reason field?", - Check: "", - Checker: "lint", - Package: nil, - } - out = append(out, p) - continue - } - default: - // unknown directive, ignore - continue - } - checks := strings.Split(args[0], ",") - pos := prog.DisplayPosition(node.Pos()) - var ig Ignore - switch cmd { - case "ignore": - ig = &LineIgnore{ - File: pos.Filename, - Line: pos.Line, - Checks: checks, - pos: c.Pos(), - } - case "file-ignore": - ig = &FileIgnore{ - File: pos.Filename, - Checks: checks, - } - } - l.automaticIgnores = append(l.automaticIgnores, ig) - } - } - } - } - } - - sizes := struct { - types int - defs int - uses int - implicits int - selections int - scopes int - }{} - for _, pkg := range pkgs { - sizes.types += len(pkg.TypesInfo.Types) - sizes.defs += len(pkg.TypesInfo.Defs) - sizes.uses += len(pkg.TypesInfo.Uses) - sizes.implicits += len(pkg.TypesInfo.Implicits) - sizes.selections += len(pkg.TypesInfo.Selections) - sizes.scopes += len(pkg.TypesInfo.Scopes) - } - - if stats != nil { - stats.OtherInitWork = time.Since(t) - } - - for _, checker := range l.Checkers { - t := time.Now() - checker.Init(prog) - if stats != nil { - stats.CheckerInits[checker.Name()] = time.Since(t) - } - } - - var jobs []*Job - var allChecks []string - - for _, checker := range l.Checkers { - checks := checker.Checks() - for _, check := range checks { - allChecks = append(allChecks, check.ID) - j := &Job{ - Program: prog, - checker: checker.Name(), - check: check, - } - jobs = append(jobs, j) - } - } - - max := len(jobs) - if l.MaxConcurrentJobs > 0 { - max = l.MaxConcurrentJobs - } - - sem := make(chan struct{}, max) - wg := &sync.WaitGroup{} - for _, j := range jobs { - wg.Add(1) - go func(j *Job) { - defer wg.Done() - sem <- struct{}{} - defer func() { <-sem }() - fn := j.check.Fn - if fn == nil { - return - } - t := time.Now() - fn(j) - j.duration = time.Since(t) - }(j) - } - wg.Wait() - - for _, j := range jobs { - if stats != nil { - stats.Jobs = append(stats.Jobs, JobStat{j.check.ID, j.duration}) - } - for _, p := range j.problems { - allowedChecks := FilterChecks(allChecks, p.Package.Config.Checks) - - if l.ignore(p) { - p.Severity = Ignored - } - // TODO(dh): support globs in check white/blacklist - // OPT(dh): this approach doesn't actually disable checks, - // it just discards their results. For the moment, that's - // fine. None of our checks are super expensive. In the - // future, we may want to provide opt-in expensive - // analysis, which shouldn't run at all. It may be easiest - // to implement this in the individual checks. - if (l.ReturnIgnored || p.Severity != Ignored) && allowedChecks[p.Check] { - out = append(out, p) - } - } - } - - for _, ig := range l.automaticIgnores { - ig, ok := ig.(*LineIgnore) - if !ok { - continue - } - if ig.matched { - continue - } - - couldveMatched := false - for f, pkg := range prog.astFileMap { - if prog.Fset().Position(f.Pos()).Filename != ig.File { - continue - } - allowedChecks := FilterChecks(allChecks, pkg.Config.Checks) - for _, c := range ig.Checks { - if !allowedChecks[c] { - continue - } - couldveMatched = true - break - } - break - } - - if !couldveMatched { - // The ignored checks were disabled for the containing package. - // Don't flag the ignore for not having matched. - continue - } - p := Problem{ - Position: prog.DisplayPosition(ig.pos), - Text: "this linter directive didn't match anything; should it be removed?", - Check: "", - Checker: "lint", - Package: nil, - } - out = append(out, p) - } - - sort.Slice(out, func(i int, j int) bool { - pi, pj := out[i].Position, out[j].Position - - if pi.Filename != pj.Filename { - return pi.Filename < pj.Filename - } - if pi.Line != pj.Line { - return pi.Line < pj.Line - } - if pi.Column != pj.Column { - return pi.Column < pj.Column - } - - return out[i].Text < out[j].Text - }) - - if l.PrintStats && stats != nil { - stats.Print(os.Stderr) - } - - if len(out) < 2 { - return out - } - - uniq := make([]Problem, 0, len(out)) - uniq = append(uniq, out[0]) - prev := out[0] - for _, p := range out[1:] { - if prev.Position == p.Position && prev.Text == p.Text { - continue - } - prev = p - uniq = append(uniq, p) - } - - return uniq -} - -func FilterChecks(allChecks []string, checks []string) map[string]bool { - // OPT(dh): this entire computation could be cached per package - allowedChecks := map[string]bool{} - - for _, check := range checks { - b := true - if len(check) > 1 && check[0] == '-' { - b = false - check = check[1:] - } - if check == "*" || check == "all" { - // Match all - for _, c := range allChecks { - allowedChecks[c] = b - } - } else if strings.HasSuffix(check, "*") { - // Glob - prefix := check[:len(check)-1] - isCat := strings.IndexFunc(prefix, func(r rune) bool { return unicode.IsNumber(r) }) == -1 - - for _, c := range allChecks { - idx := strings.IndexFunc(c, func(r rune) bool { return unicode.IsNumber(r) }) - if isCat { - // Glob is S*, which should match S1000 but not SA1000 - cat := c[:idx] - if prefix == cat { - allowedChecks[c] = b - } - } else { - // Glob is S1* - if strings.HasPrefix(c, prefix) { - allowedChecks[c] = b - } - } - } - } else { - // Literal check name - allowedChecks[check] = b - } - } - return allowedChecks -} - -func (prog *Program) Package(path string) *packages.Package { - return prog.packagesMap[path] -} - -// Pkg represents a package being linted. -type Pkg struct { - SSA *ssa.Package - *packages.Package - Config config.Config -} - -type Positioner interface { - Pos() token.Pos -} - -func (prog *Program) DisplayPosition(p token.Pos) token.Position { - // Only use the adjusted position if it points to another Go file. - // This means we'll point to the original file for cgo files, but - // we won't point to a YACC grammar file. - - pos := prog.Fset().PositionFor(p, false) - adjPos := prog.Fset().PositionFor(p, true) - - if filepath.Ext(adjPos.Filename) == ".go" { - return adjPos - } - return pos -} - -func (prog *Program) isGenerated(path string) bool { - // This function isn't very efficient in terms of lock contention - // and lack of parallelism, but it really shouldn't matter. - // Projects consists of thousands of files, and have hundreds of - // errors. That's not a lot of calls to isGenerated. - - prog.genMu.RLock() - if b, ok := prog.generatedMap[path]; ok { - prog.genMu.RUnlock() - return b - } - prog.genMu.RUnlock() - prog.genMu.Lock() - defer prog.genMu.Unlock() - // recheck to avoid doing extra work in case of race - if b, ok := prog.generatedMap[path]; ok { - return b - } - - f, err := os.Open(path) - if err != nil { - return false - } - defer f.Close() - b := isGenerated(f) - prog.generatedMap[path] = b - return b -} - -func (j *Job) Errorf(n Positioner, format string, args ...interface{}) *Problem { - tf := j.Program.SSA.Fset.File(n.Pos()) - f := j.Program.tokenFileMap[tf] - pkg := j.Program.astFileMap[f] - - pos := j.Program.DisplayPosition(n.Pos()) - if j.Program.isGenerated(pos.Filename) && j.check.FilterGenerated { - return nil - } - problem := Problem{ - Position: pos, - Text: fmt.Sprintf(format, args...), - Check: j.check.ID, - Checker: j.checker, - Package: pkg, - } - j.problems = append(j.problems, problem) - return &j.problems[len(j.problems)-1] -} - -func (j *Job) NodePackage(node Positioner) *Pkg { - f := j.File(node) - return j.Program.astFileMap[f] -} - -func allPackages(pkgs []*packages.Package) []*packages.Package { - var out []*packages.Package - packages.Visit( - pkgs, - func(pkg *packages.Package) bool { - out = append(out, pkg) - return true - }, - nil, - ) - return out -} diff --git a/vendor/github.com/golangci/go-tools/simple/lint.go b/vendor/github.com/golangci/go-tools/simple/lint.go deleted file mode 100644 index 67eebe3988e8..000000000000 --- a/vendor/github.com/golangci/go-tools/simple/lint.go +++ /dev/null @@ -1,1734 +0,0 @@ -// Package simple contains a linter for Go source code. -package simple // import "github.com/golangci/go-tools/simple" - -import ( - "go/ast" - "go/constant" - "go/token" - "go/types" - "reflect" - "strings" - - . "github.com/golangci/go-tools/arg" - "github.com/golangci/go-tools/internal/sharedcheck" - "github.com/golangci/go-tools/lint" - . "github.com/golangci/go-tools/lint/lintdsl" - - "golang.org/x/tools/go/types/typeutil" -) - -type Checker struct { - CheckGenerated bool - MS *typeutil.MethodSetCache -} - -func NewChecker() *Checker { - return &Checker{ - MS: &typeutil.MethodSetCache{}, - } -} - -func (*Checker) Name() string { return "gosimple" } -func (*Checker) Prefix() string { return "S" } - -func (c *Checker) Init(prog *lint.Program) {} - -func (c *Checker) Checks() []lint.Check { - return []lint.Check{ - {ID: "S1000", FilterGenerated: true, Fn: c.LintSingleCaseSelect}, - {ID: "S1001", FilterGenerated: true, Fn: c.LintLoopCopy}, - {ID: "S1002", FilterGenerated: true, Fn: c.LintIfBoolCmp}, - {ID: "S1003", FilterGenerated: true, Fn: c.LintStringsContains}, - {ID: "S1004", FilterGenerated: true, Fn: c.LintBytesCompare}, - {ID: "S1005", FilterGenerated: true, Fn: c.LintUnnecessaryBlank}, - {ID: "S1006", FilterGenerated: true, Fn: c.LintForTrue}, - {ID: "S1007", FilterGenerated: true, Fn: c.LintRegexpRaw}, - {ID: "S1008", FilterGenerated: true, Fn: c.LintIfReturn}, - {ID: "S1009", FilterGenerated: true, Fn: c.LintRedundantNilCheckWithLen}, - {ID: "S1010", FilterGenerated: true, Fn: c.LintSlicing}, - {ID: "S1011", FilterGenerated: true, Fn: c.LintLoopAppend}, - {ID: "S1012", FilterGenerated: true, Fn: c.LintTimeSince}, - {ID: "S1016", FilterGenerated: true, Fn: c.LintSimplerStructConversion}, - {ID: "S1017", FilterGenerated: true, Fn: c.LintTrim}, - {ID: "S1018", FilterGenerated: true, Fn: c.LintLoopSlide}, - {ID: "S1019", FilterGenerated: true, Fn: c.LintMakeLenCap}, - {ID: "S1020", FilterGenerated: true, Fn: c.LintAssertNotNil}, - {ID: "S1021", FilterGenerated: true, Fn: c.LintDeclareAssign}, - {ID: "S1023", FilterGenerated: true, Fn: c.LintRedundantBreak}, - {ID: "S1024", FilterGenerated: true, Fn: c.LintTimeUntil}, - {ID: "S1025", FilterGenerated: true, Fn: c.LintRedundantSprintf}, - {ID: "S1028", FilterGenerated: true, Fn: c.LintErrorsNewSprintf}, - {ID: "S1029", FilterGenerated: false, Fn: c.LintRangeStringRunes}, - {ID: "S1030", FilterGenerated: true, Fn: c.LintBytesBufferConversions}, - {ID: "S1031", FilterGenerated: true, Fn: c.LintNilCheckAroundRange}, - {ID: "S1032", FilterGenerated: true, Fn: c.LintSortHelpers}, - } -} - -func (c *Checker) LintSingleCaseSelect(j *lint.Job) { - isSingleSelect := func(node ast.Node) bool { - v, ok := node.(*ast.SelectStmt) - if !ok { - return false - } - return len(v.Body.List) == 1 - } - - seen := map[ast.Node]struct{}{} - fn := func(node ast.Node) bool { - switch v := node.(type) { - case *ast.ForStmt: - if len(v.Body.List) != 1 { - return true - } - if !isSingleSelect(v.Body.List[0]) { - return true - } - if _, ok := v.Body.List[0].(*ast.SelectStmt).Body.List[0].(*ast.CommClause).Comm.(*ast.SendStmt); ok { - // Don't suggest using range for channel sends - return true - } - seen[v.Body.List[0]] = struct{}{} - j.Errorf(node, "should use for range instead of for { select {} }") - case *ast.SelectStmt: - if _, ok := seen[v]; ok { - return true - } - if !isSingleSelect(v) { - return true - } - j.Errorf(node, "should use a simple channel send/receive instead of select with a single case") - return true - } - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) LintLoopCopy(j *lint.Job) { - fn := func(node ast.Node) bool { - loop, ok := node.(*ast.RangeStmt) - if !ok { - return true - } - - if loop.Key == nil { - return true - } - if len(loop.Body.List) != 1 { - return true - } - stmt, ok := loop.Body.List[0].(*ast.AssignStmt) - if !ok { - return true - } - if stmt.Tok != token.ASSIGN || len(stmt.Lhs) != 1 || len(stmt.Rhs) != 1 { - return true - } - lhs, ok := stmt.Lhs[0].(*ast.IndexExpr) - if !ok { - return true - } - - if _, ok := TypeOf(j, lhs.X).(*types.Slice); !ok { - return true - } - lidx, ok := lhs.Index.(*ast.Ident) - if !ok { - return true - } - key, ok := loop.Key.(*ast.Ident) - if !ok { - return true - } - if TypeOf(j, lhs) == nil || TypeOf(j, stmt.Rhs[0]) == nil { - return true - } - if ObjectOf(j, lidx) != ObjectOf(j, key) { - return true - } - if !types.Identical(TypeOf(j, lhs), TypeOf(j, stmt.Rhs[0])) { - return true - } - if _, ok := TypeOf(j, loop.X).(*types.Slice); !ok { - return true - } - - if rhs, ok := stmt.Rhs[0].(*ast.IndexExpr); ok { - rx, ok := rhs.X.(*ast.Ident) - _ = rx - if !ok { - return true - } - ridx, ok := rhs.Index.(*ast.Ident) - if !ok { - return true - } - if ObjectOf(j, ridx) != ObjectOf(j, key) { - return true - } - } else if rhs, ok := stmt.Rhs[0].(*ast.Ident); ok { - value, ok := loop.Value.(*ast.Ident) - if !ok { - return true - } - if ObjectOf(j, rhs) != ObjectOf(j, value) { - return true - } - } else { - return true - } - j.Errorf(loop, "should use copy() instead of a loop") - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) LintIfBoolCmp(j *lint.Job) { - fn := func(node ast.Node) bool { - expr, ok := node.(*ast.BinaryExpr) - if !ok || (expr.Op != token.EQL && expr.Op != token.NEQ) { - return true - } - x := IsBoolConst(j, expr.X) - y := IsBoolConst(j, expr.Y) - if !x && !y { - return true - } - var other ast.Expr - var val bool - if x { - val = BoolConst(j, expr.X) - other = expr.Y - } else { - val = BoolConst(j, expr.Y) - other = expr.X - } - basic, ok := TypeOf(j, other).Underlying().(*types.Basic) - if !ok || basic.Kind() != types.Bool { - return true - } - op := "" - if (expr.Op == token.EQL && !val) || (expr.Op == token.NEQ && val) { - op = "!" - } - r := op + Render(j, other) - l1 := len(r) - r = strings.TrimLeft(r, "!") - if (l1-len(r))%2 == 1 { - r = "!" + r - } - if IsInTest(j, node) { - return true - } - j.Errorf(expr, "should omit comparison to bool constant, can be simplified to %s", r) - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) LintBytesBufferConversions(j *lint.Job) { - fn := func(node ast.Node) bool { - call, ok := node.(*ast.CallExpr) - if !ok || len(call.Args) != 1 { - return true - } - - argCall, ok := call.Args[0].(*ast.CallExpr) - if !ok { - return true - } - sel, ok := argCall.Fun.(*ast.SelectorExpr) - if !ok { - return true - } - - typ := TypeOf(j, call.Fun) - if typ == types.Universe.Lookup("string").Type() && IsCallToAST(j, call.Args[0], "(*bytes.Buffer).Bytes") { - j.Errorf(call, "should use %v.String() instead of %v", Render(j, sel.X), Render(j, call)) - } else if typ, ok := typ.(*types.Slice); ok && typ.Elem() == types.Universe.Lookup("byte").Type() && IsCallToAST(j, call.Args[0], "(*bytes.Buffer).String") { - j.Errorf(call, "should use %v.Bytes() instead of %v", Render(j, sel.X), Render(j, call)) - } - - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) LintStringsContains(j *lint.Job) { - // map of value to token to bool value - allowed := map[int64]map[token.Token]bool{ - -1: {token.GTR: true, token.NEQ: true, token.EQL: false}, - 0: {token.GEQ: true, token.LSS: false}, - } - fn := func(node ast.Node) bool { - expr, ok := node.(*ast.BinaryExpr) - if !ok { - return true - } - switch expr.Op { - case token.GEQ, token.GTR, token.NEQ, token.LSS, token.EQL: - default: - return true - } - - value, ok := ExprToInt(j, expr.Y) - if !ok { - return true - } - - allowedOps, ok := allowed[value] - if !ok { - return true - } - b, ok := allowedOps[expr.Op] - if !ok { - return true - } - - call, ok := expr.X.(*ast.CallExpr) - if !ok { - return true - } - sel, ok := call.Fun.(*ast.SelectorExpr) - if !ok { - return true - } - pkgIdent, ok := sel.X.(*ast.Ident) - if !ok { - return true - } - funIdent := sel.Sel - if pkgIdent.Name != "strings" && pkgIdent.Name != "bytes" { - return true - } - newFunc := "" - switch funIdent.Name { - case "IndexRune": - newFunc = "ContainsRune" - case "IndexAny": - newFunc = "ContainsAny" - case "Index": - newFunc = "Contains" - default: - return true - } - - prefix := "" - if !b { - prefix = "!" - } - j.Errorf(node, "should use %s%s.%s(%s) instead", prefix, pkgIdent.Name, newFunc, RenderArgs(j, call.Args)) - - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) LintBytesCompare(j *lint.Job) { - fn := func(node ast.Node) bool { - expr, ok := node.(*ast.BinaryExpr) - if !ok { - return true - } - if expr.Op != token.NEQ && expr.Op != token.EQL { - return true - } - call, ok := expr.X.(*ast.CallExpr) - if !ok { - return true - } - if !IsCallToAST(j, call, "bytes.Compare") { - return true - } - value, ok := ExprToInt(j, expr.Y) - if !ok || value != 0 { - return true - } - args := RenderArgs(j, call.Args) - prefix := "" - if expr.Op == token.NEQ { - prefix = "!" - } - j.Errorf(node, "should use %sbytes.Equal(%s) instead", prefix, args) - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) LintForTrue(j *lint.Job) { - fn := func(node ast.Node) bool { - loop, ok := node.(*ast.ForStmt) - if !ok { - return true - } - if loop.Init != nil || loop.Post != nil { - return true - } - if !IsBoolConst(j, loop.Cond) || !BoolConst(j, loop.Cond) { - return true - } - j.Errorf(loop, "should use for {} instead of for true {}") - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) LintRegexpRaw(j *lint.Job) { - fn := func(node ast.Node) bool { - call, ok := node.(*ast.CallExpr) - if !ok { - return true - } - if !IsCallToAST(j, call, "regexp.MustCompile") && - !IsCallToAST(j, call, "regexp.Compile") { - return true - } - sel, ok := call.Fun.(*ast.SelectorExpr) - if !ok { - return true - } - if len(call.Args) != 1 { - // invalid function call - return true - } - lit, ok := call.Args[Arg("regexp.Compile.expr")].(*ast.BasicLit) - if !ok { - // TODO(dominikh): support string concat, maybe support constants - return true - } - if lit.Kind != token.STRING { - // invalid function call - return true - } - if lit.Value[0] != '"' { - // already a raw string - return true - } - val := lit.Value - if !strings.Contains(val, `\\`) { - return true - } - if strings.Contains(val, "`") { - return true - } - - bs := false - for _, c := range val { - if !bs && c == '\\' { - bs = true - continue - } - if bs && c == '\\' { - bs = false - continue - } - if bs { - // backslash followed by non-backslash -> escape sequence - return true - } - } - - j.Errorf(call, "should use raw string (`...`) with regexp.%s to avoid having to escape twice", sel.Sel.Name) - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) LintIfReturn(j *lint.Job) { - fn := func(node ast.Node) bool { - block, ok := node.(*ast.BlockStmt) - if !ok { - return true - } - l := len(block.List) - if l < 2 { - return true - } - n1, n2 := block.List[l-2], block.List[l-1] - - if len(block.List) >= 3 { - if _, ok := block.List[l-3].(*ast.IfStmt); ok { - // Do not flag a series of if statements - return true - } - } - // if statement with no init, no else, a single condition - // checking an identifier or function call and just a return - // statement in the body, that returns a boolean constant - ifs, ok := n1.(*ast.IfStmt) - if !ok { - return true - } - if ifs.Else != nil || ifs.Init != nil { - return true - } - if len(ifs.Body.List) != 1 { - return true - } - if op, ok := ifs.Cond.(*ast.BinaryExpr); ok { - switch op.Op { - case token.EQL, token.LSS, token.GTR, token.NEQ, token.LEQ, token.GEQ: - default: - return true - } - } - ret1, ok := ifs.Body.List[0].(*ast.ReturnStmt) - if !ok { - return true - } - if len(ret1.Results) != 1 { - return true - } - if !IsBoolConst(j, ret1.Results[0]) { - return true - } - - ret2, ok := n2.(*ast.ReturnStmt) - if !ok { - return true - } - if len(ret2.Results) != 1 { - return true - } - if !IsBoolConst(j, ret2.Results[0]) { - return true - } - j.Errorf(n1, "should use 'return ' instead of 'if { return }; return '") - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -// LintRedundantNilCheckWithLen checks for the following reduntant nil-checks: -// -// if x == nil || len(x) == 0 {} -// if x != nil && len(x) != 0 {} -// if x != nil && len(x) == N {} (where N != 0) -// if x != nil && len(x) > N {} -// if x != nil && len(x) >= N {} (where N != 0) -// -func (c *Checker) LintRedundantNilCheckWithLen(j *lint.Job) { - isConstZero := func(expr ast.Expr) (isConst bool, isZero bool) { - _, ok := expr.(*ast.BasicLit) - if ok { - return true, IsZero(expr) - } - id, ok := expr.(*ast.Ident) - if !ok { - return false, false - } - c, ok := ObjectOf(j, id).(*types.Const) - if !ok { - return false, false - } - return true, c.Val().Kind() == constant.Int && c.Val().String() == "0" - } - - fn := func(node ast.Node) bool { - // check that expr is "x || y" or "x && y" - expr, ok := node.(*ast.BinaryExpr) - if !ok { - return true - } - if expr.Op != token.LOR && expr.Op != token.LAND { - return true - } - eqNil := expr.Op == token.LOR - - // check that x is "xx == nil" or "xx != nil" - x, ok := expr.X.(*ast.BinaryExpr) - if !ok { - return true - } - if eqNil && x.Op != token.EQL { - return true - } - if !eqNil && x.Op != token.NEQ { - return true - } - xx, ok := x.X.(*ast.Ident) - if !ok { - return true - } - if !IsNil(j, x.Y) { - return true - } - - // check that y is "len(xx) == 0" or "len(xx) ... " - y, ok := expr.Y.(*ast.BinaryExpr) - if !ok { - return true - } - if eqNil && y.Op != token.EQL { // must be len(xx) *==* 0 - return false - } - yx, ok := y.X.(*ast.CallExpr) - if !ok { - return true - } - yxFun, ok := yx.Fun.(*ast.Ident) - if !ok || yxFun.Name != "len" || len(yx.Args) != 1 { - return true - } - yxArg, ok := yx.Args[Arg("len.v")].(*ast.Ident) - if !ok { - return true - } - if yxArg.Name != xx.Name { - return true - } - - if eqNil && !IsZero(y.Y) { // must be len(x) == *0* - return true - } - - if !eqNil { - isConst, isZero := isConstZero(y.Y) - if !isConst { - return true - } - switch y.Op { - case token.EQL: - // avoid false positive for "xx != nil && len(xx) == 0" - if isZero { - return true - } - case token.GEQ: - // avoid false positive for "xx != nil && len(xx) >= 0" - if isZero { - return true - } - case token.NEQ: - // avoid false positive for "xx != nil && len(xx) != " - if !isZero { - return true - } - case token.GTR: - // ok - default: - return true - } - } - - // finally check that xx type is one of array, slice, map or chan - // this is to prevent false positive in case if xx is a pointer to an array - var nilType string - switch TypeOf(j, xx).(type) { - case *types.Slice: - nilType = "nil slices" - case *types.Map: - nilType = "nil maps" - case *types.Chan: - nilType = "nil channels" - default: - return true - } - j.Errorf(expr, "should omit nil check; len() for %s is defined as zero", nilType) - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) LintSlicing(j *lint.Job) { - fn := func(node ast.Node) bool { - n, ok := node.(*ast.SliceExpr) - if !ok { - return true - } - if n.Max != nil { - return true - } - s, ok := n.X.(*ast.Ident) - if !ok || s.Obj == nil { - return true - } - call, ok := n.High.(*ast.CallExpr) - if !ok || len(call.Args) != 1 || call.Ellipsis.IsValid() { - return true - } - fun, ok := call.Fun.(*ast.Ident) - if !ok || fun.Name != "len" { - return true - } - if _, ok := ObjectOf(j, fun).(*types.Builtin); !ok { - return true - } - arg, ok := call.Args[Arg("len.v")].(*ast.Ident) - if !ok || arg.Obj != s.Obj { - return true - } - j.Errorf(n, "should omit second index in slice, s[a:len(s)] is identical to s[a:]") - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func refersTo(j *lint.Job, expr ast.Expr, ident *ast.Ident) bool { - found := false - fn := func(node ast.Node) bool { - ident2, ok := node.(*ast.Ident) - if !ok { - return true - } - if ObjectOf(j, ident) == ObjectOf(j, ident2) { - found = true - return false - } - return true - } - ast.Inspect(expr, fn) - return found -} - -func (c *Checker) LintLoopAppend(j *lint.Job) { - fn := func(node ast.Node) bool { - loop, ok := node.(*ast.RangeStmt) - if !ok { - return true - } - if !IsBlank(loop.Key) { - return true - } - val, ok := loop.Value.(*ast.Ident) - if !ok { - return true - } - if len(loop.Body.List) != 1 { - return true - } - stmt, ok := loop.Body.List[0].(*ast.AssignStmt) - if !ok { - return true - } - if stmt.Tok != token.ASSIGN || len(stmt.Lhs) != 1 || len(stmt.Rhs) != 1 { - return true - } - if refersTo(j, stmt.Lhs[0], val) { - return true - } - call, ok := stmt.Rhs[0].(*ast.CallExpr) - if !ok { - return true - } - if len(call.Args) != 2 || call.Ellipsis.IsValid() { - return true - } - fun, ok := call.Fun.(*ast.Ident) - if !ok { - return true - } - obj := ObjectOf(j, fun) - fn, ok := obj.(*types.Builtin) - if !ok || fn.Name() != "append" { - return true - } - - src := TypeOf(j, loop.X) - dst := TypeOf(j, call.Args[Arg("append.slice")]) - // TODO(dominikh) remove nil check once Go issue #15173 has - // been fixed - if src == nil { - return true - } - if !types.Identical(src, dst) { - return true - } - - if Render(j, stmt.Lhs[0]) != Render(j, call.Args[Arg("append.slice")]) { - return true - } - - el, ok := call.Args[Arg("append.elems")].(*ast.Ident) - if !ok { - return true - } - if ObjectOf(j, val) != ObjectOf(j, el) { - return true - } - j.Errorf(loop, "should replace loop with %s = append(%s, %s...)", - Render(j, stmt.Lhs[0]), Render(j, call.Args[Arg("append.slice")]), Render(j, loop.X)) - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) LintTimeSince(j *lint.Job) { - fn := func(node ast.Node) bool { - call, ok := node.(*ast.CallExpr) - if !ok { - return true - } - sel, ok := call.Fun.(*ast.SelectorExpr) - if !ok { - return true - } - if !IsCallToAST(j, sel.X, "time.Now") { - return true - } - if sel.Sel.Name != "Sub" { - return true - } - j.Errorf(call, "should use time.Since instead of time.Now().Sub") - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) LintTimeUntil(j *lint.Job) { - if !IsGoVersion(j, 8) { - return - } - fn := func(node ast.Node) bool { - call, ok := node.(*ast.CallExpr) - if !ok { - return true - } - if !IsCallToAST(j, call, "(time.Time).Sub") { - return true - } - if !IsCallToAST(j, call.Args[Arg("(time.Time).Sub.u")], "time.Now") { - return true - } - j.Errorf(call, "should use time.Until instead of t.Sub(time.Now())") - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) LintUnnecessaryBlank(j *lint.Job) { - fn1 := func(node ast.Node) { - assign, ok := node.(*ast.AssignStmt) - if !ok { - return - } - if len(assign.Lhs) != 2 || len(assign.Rhs) != 1 { - return - } - if !IsBlank(assign.Lhs[1]) { - return - } - switch rhs := assign.Rhs[0].(type) { - case *ast.IndexExpr: - // The type-checker should make sure that it's a map, but - // let's be safe. - if _, ok := TypeOf(j, rhs.X).Underlying().(*types.Map); !ok { - return - } - case *ast.UnaryExpr: - if rhs.Op != token.ARROW { - return - } - default: - return - } - cp := *assign - cp.Lhs = cp.Lhs[0:1] - j.Errorf(assign, "should write %s instead of %s", Render(j, &cp), Render(j, assign)) - } - - fn2 := func(node ast.Node) { - stmt, ok := node.(*ast.AssignStmt) - if !ok { - return - } - if len(stmt.Lhs) != len(stmt.Rhs) { - return - } - for i, lh := range stmt.Lhs { - rh := stmt.Rhs[i] - if !IsBlank(lh) { - continue - } - expr, ok := rh.(*ast.UnaryExpr) - if !ok { - continue - } - if expr.Op != token.ARROW { - continue - } - j.Errorf(lh, "'_ = <-ch' can be simplified to '<-ch'") - } - } - - fn3 := func(node ast.Node) { - rs, ok := node.(*ast.RangeStmt) - if !ok { - return - } - - // for x, _ - if !IsBlank(rs.Key) && IsBlank(rs.Value) { - j.Errorf(rs.Value, "should omit value from range; this loop is equivalent to `for %s %s range ...`", Render(j, rs.Key), rs.Tok) - } - // for _, _ || for _ - if IsBlank(rs.Key) && (IsBlank(rs.Value) || rs.Value == nil) { - j.Errorf(rs.Key, "should omit values from range; this loop is equivalent to `for range ...`") - } - } - - fn := func(node ast.Node) bool { - fn1(node) - fn2(node) - if IsGoVersion(j, 4) { - fn3(node) - } - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) LintSimplerStructConversion(j *lint.Job) { - var skip ast.Node - fn := func(node ast.Node) bool { - // Do not suggest type conversion between pointers - if unary, ok := node.(*ast.UnaryExpr); ok && unary.Op == token.AND { - if lit, ok := unary.X.(*ast.CompositeLit); ok { - skip = lit - } - return true - } - - if node == skip { - return true - } - - lit, ok := node.(*ast.CompositeLit) - if !ok { - return true - } - typ1, _ := TypeOf(j, lit.Type).(*types.Named) - if typ1 == nil { - return true - } - s1, ok := typ1.Underlying().(*types.Struct) - if !ok { - return true - } - - var typ2 *types.Named - var ident *ast.Ident - getSelType := func(expr ast.Expr) (types.Type, *ast.Ident, bool) { - sel, ok := expr.(*ast.SelectorExpr) - if !ok { - return nil, nil, false - } - ident, ok := sel.X.(*ast.Ident) - if !ok { - return nil, nil, false - } - typ := TypeOf(j, sel.X) - return typ, ident, typ != nil - } - if len(lit.Elts) == 0 { - return true - } - if s1.NumFields() != len(lit.Elts) { - return true - } - for i, elt := range lit.Elts { - var t types.Type - var id *ast.Ident - var ok bool - switch elt := elt.(type) { - case *ast.SelectorExpr: - t, id, ok = getSelType(elt) - if !ok { - return true - } - if i >= s1.NumFields() || s1.Field(i).Name() != elt.Sel.Name { - return true - } - case *ast.KeyValueExpr: - var sel *ast.SelectorExpr - sel, ok = elt.Value.(*ast.SelectorExpr) - if !ok { - return true - } - - if elt.Key.(*ast.Ident).Name != sel.Sel.Name { - return true - } - t, id, ok = getSelType(elt.Value) - } - if !ok { - return true - } - // All fields must be initialized from the same object - if ident != nil && ident.Obj != id.Obj { - return true - } - typ2, _ = t.(*types.Named) - if typ2 == nil { - return true - } - ident = id - } - - if typ2 == nil { - return true - } - - if typ1.Obj().Pkg() != typ2.Obj().Pkg() { - // Do not suggest type conversions between different - // packages. Types in different packages might only match - // by coincidence. Furthermore, if the dependency ever - // adds more fields to its type, it could break the code - // that relies on the type conversion to work. - return true - } - - s2, ok := typ2.Underlying().(*types.Struct) - if !ok { - return true - } - if typ1 == typ2 { - return true - } - if IsGoVersion(j, 8) { - if !types.IdenticalIgnoreTags(s1, s2) { - return true - } - } else { - if !types.Identical(s1, s2) { - return true - } - } - j.Errorf(node, "should convert %s (type %s) to %s instead of using struct literal", - ident.Name, typ2.Obj().Name(), typ1.Obj().Name()) - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) LintTrim(j *lint.Job) { - sameNonDynamic := func(node1, node2 ast.Node) bool { - if reflect.TypeOf(node1) != reflect.TypeOf(node2) { - return false - } - - switch node1 := node1.(type) { - case *ast.Ident: - return node1.Obj == node2.(*ast.Ident).Obj - case *ast.SelectorExpr: - return Render(j, node1) == Render(j, node2) - case *ast.IndexExpr: - return Render(j, node1) == Render(j, node2) - } - return false - } - - isLenOnIdent := func(fn ast.Expr, ident ast.Expr) bool { - call, ok := fn.(*ast.CallExpr) - if !ok { - return false - } - if fn, ok := call.Fun.(*ast.Ident); !ok || fn.Name != "len" { - return false - } - if len(call.Args) != 1 { - return false - } - return sameNonDynamic(call.Args[Arg("len.v")], ident) - } - - fn := func(node ast.Node) bool { - var pkg string - var fun string - - ifstmt, ok := node.(*ast.IfStmt) - if !ok { - return true - } - if ifstmt.Init != nil { - return true - } - if ifstmt.Else != nil { - return true - } - if len(ifstmt.Body.List) != 1 { - return true - } - condCall, ok := ifstmt.Cond.(*ast.CallExpr) - if !ok { - return true - } - call, ok := condCall.Fun.(*ast.SelectorExpr) - if !ok { - return true - } - if IsIdent(call.X, "strings") { - pkg = "strings" - } else if IsIdent(call.X, "bytes") { - pkg = "bytes" - } else { - return true - } - if IsIdent(call.Sel, "HasPrefix") { - fun = "HasPrefix" - } else if IsIdent(call.Sel, "HasSuffix") { - fun = "HasSuffix" - } else { - return true - } - - assign, ok := ifstmt.Body.List[0].(*ast.AssignStmt) - if !ok { - return true - } - if assign.Tok != token.ASSIGN { - return true - } - if len(assign.Lhs) != 1 || len(assign.Rhs) != 1 { - return true - } - if !sameNonDynamic(condCall.Args[0], assign.Lhs[0]) { - return true - } - slice, ok := assign.Rhs[0].(*ast.SliceExpr) - if !ok { - return true - } - if slice.Slice3 { - return true - } - if !sameNonDynamic(slice.X, condCall.Args[0]) { - return true - } - var index ast.Expr - switch fun { - case "HasPrefix": - // TODO(dh) We could detect a High that is len(s), but another - // rule will already flag that, anyway. - if slice.High != nil { - return true - } - index = slice.Low - case "HasSuffix": - if slice.Low != nil { - n, ok := ExprToInt(j, slice.Low) - if !ok || n != 0 { - return true - } - } - index = slice.High - } - - switch index := index.(type) { - case *ast.CallExpr: - if fun != "HasPrefix" { - return true - } - if fn, ok := index.Fun.(*ast.Ident); !ok || fn.Name != "len" { - return true - } - if len(index.Args) != 1 { - return true - } - id3 := index.Args[Arg("len.v")] - switch oid3 := condCall.Args[1].(type) { - case *ast.BasicLit: - if pkg != "strings" { - return false - } - lit, ok := id3.(*ast.BasicLit) - if !ok { - return true - } - s1, ok1 := ExprToString(j, lit) - s2, ok2 := ExprToString(j, condCall.Args[1]) - if !ok1 || !ok2 || s1 != s2 { - return true - } - default: - if !sameNonDynamic(id3, oid3) { - return true - } - } - case *ast.BasicLit, *ast.Ident: - if fun != "HasPrefix" { - return true - } - if pkg != "strings" { - return true - } - string, ok1 := ExprToString(j, condCall.Args[1]) - int, ok2 := ExprToInt(j, slice.Low) - if !ok1 || !ok2 || int != int64(len(string)) { - return true - } - case *ast.BinaryExpr: - if fun != "HasSuffix" { - return true - } - if index.Op != token.SUB { - return true - } - if !isLenOnIdent(index.X, condCall.Args[0]) || - !isLenOnIdent(index.Y, condCall.Args[1]) { - return true - } - default: - return true - } - - var replacement string - switch fun { - case "HasPrefix": - replacement = "TrimPrefix" - case "HasSuffix": - replacement = "TrimSuffix" - } - j.Errorf(ifstmt, "should replace this if statement with an unconditional %s.%s", pkg, replacement) - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) LintLoopSlide(j *lint.Job) { - // TODO(dh): detect bs[i+offset] in addition to bs[offset+i] - // TODO(dh): consider merging this function with LintLoopCopy - // TODO(dh): detect length that is an expression, not a variable name - // TODO(dh): support sliding to a different offset than the beginning of the slice - - fn := func(node ast.Node) bool { - /* - for i := 0; i < n; i++ { - bs[i] = bs[offset+i] - } - - ↓ - - copy(bs[:n], bs[offset:offset+n]) - */ - - loop, ok := node.(*ast.ForStmt) - if !ok || len(loop.Body.List) != 1 || loop.Init == nil || loop.Cond == nil || loop.Post == nil { - return true - } - assign, ok := loop.Init.(*ast.AssignStmt) - if !ok || len(assign.Lhs) != 1 || len(assign.Rhs) != 1 || !IsZero(assign.Rhs[0]) { - return true - } - initvar, ok := assign.Lhs[0].(*ast.Ident) - if !ok { - return true - } - post, ok := loop.Post.(*ast.IncDecStmt) - if !ok || post.Tok != token.INC { - return true - } - postvar, ok := post.X.(*ast.Ident) - if !ok || ObjectOf(j, postvar) != ObjectOf(j, initvar) { - return true - } - bin, ok := loop.Cond.(*ast.BinaryExpr) - if !ok || bin.Op != token.LSS { - return true - } - binx, ok := bin.X.(*ast.Ident) - if !ok || ObjectOf(j, binx) != ObjectOf(j, initvar) { - return true - } - biny, ok := bin.Y.(*ast.Ident) - if !ok { - return true - } - - assign, ok = loop.Body.List[0].(*ast.AssignStmt) - if !ok || len(assign.Lhs) != 1 || len(assign.Rhs) != 1 || assign.Tok != token.ASSIGN { - return true - } - lhs, ok := assign.Lhs[0].(*ast.IndexExpr) - if !ok { - return true - } - rhs, ok := assign.Rhs[0].(*ast.IndexExpr) - if !ok { - return true - } - - bs1, ok := lhs.X.(*ast.Ident) - if !ok { - return true - } - bs2, ok := rhs.X.(*ast.Ident) - if !ok { - return true - } - obj1 := ObjectOf(j, bs1) - obj2 := ObjectOf(j, bs2) - if obj1 != obj2 { - return true - } - if _, ok := obj1.Type().Underlying().(*types.Slice); !ok { - return true - } - - index1, ok := lhs.Index.(*ast.Ident) - if !ok || ObjectOf(j, index1) != ObjectOf(j, initvar) { - return true - } - index2, ok := rhs.Index.(*ast.BinaryExpr) - if !ok || index2.Op != token.ADD { - return true - } - add1, ok := index2.X.(*ast.Ident) - if !ok { - return true - } - add2, ok := index2.Y.(*ast.Ident) - if !ok || ObjectOf(j, add2) != ObjectOf(j, initvar) { - return true - } - - j.Errorf(loop, "should use copy(%s[:%s], %s[%s:]) instead", Render(j, bs1), Render(j, biny), Render(j, bs1), Render(j, add1)) - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) LintMakeLenCap(j *lint.Job) { - fn := func(node ast.Node) bool { - call, ok := node.(*ast.CallExpr) - if !ok { - return true - } - if fn, ok := call.Fun.(*ast.Ident); !ok || fn.Name != "make" { - // FIXME check whether make is indeed the built-in function - return true - } - switch len(call.Args) { - case 2: - // make(T, len) - if _, ok := TypeOf(j, call.Args[Arg("make.t")]).Underlying().(*types.Slice); ok { - break - } - if IsZero(call.Args[Arg("make.size[0]")]) { - j.Errorf(call.Args[Arg("make.size[0]")], "should use make(%s) instead", Render(j, call.Args[Arg("make.t")])) - } - case 3: - // make(T, len, cap) - if Render(j, call.Args[Arg("make.size[0]")]) == Render(j, call.Args[Arg("make.size[1]")]) { - j.Errorf(call.Args[Arg("make.size[0]")], - "should use make(%s, %s) instead", - Render(j, call.Args[Arg("make.t")]), Render(j, call.Args[Arg("make.size[0]")])) - } - } - return false - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) LintAssertNotNil(j *lint.Job) { - isNilCheck := func(ident *ast.Ident, expr ast.Expr) bool { - xbinop, ok := expr.(*ast.BinaryExpr) - if !ok || xbinop.Op != token.NEQ { - return false - } - xident, ok := xbinop.X.(*ast.Ident) - if !ok || xident.Obj != ident.Obj { - return false - } - if !IsNil(j, xbinop.Y) { - return false - } - return true - } - isOKCheck := func(ident *ast.Ident, expr ast.Expr) bool { - yident, ok := expr.(*ast.Ident) - if !ok || yident.Obj != ident.Obj { - return false - } - return true - } - fn := func(node ast.Node) bool { - ifstmt, ok := node.(*ast.IfStmt) - if !ok { - return true - } - assign, ok := ifstmt.Init.(*ast.AssignStmt) - if !ok || len(assign.Lhs) != 2 || len(assign.Rhs) != 1 || !IsBlank(assign.Lhs[0]) { - return true - } - assert, ok := assign.Rhs[0].(*ast.TypeAssertExpr) - if !ok { - return true - } - binop, ok := ifstmt.Cond.(*ast.BinaryExpr) - if !ok || binop.Op != token.LAND { - return true - } - assertIdent, ok := assert.X.(*ast.Ident) - if !ok { - return true - } - assignIdent, ok := assign.Lhs[1].(*ast.Ident) - if !ok { - return true - } - if !(isNilCheck(assertIdent, binop.X) && isOKCheck(assignIdent, binop.Y)) && - !(isNilCheck(assertIdent, binop.Y) && isOKCheck(assignIdent, binop.X)) { - return true - } - j.Errorf(ifstmt, "when %s is true, %s can't be nil", Render(j, assignIdent), Render(j, assertIdent)) - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) LintDeclareAssign(j *lint.Job) { - fn := func(node ast.Node) bool { - block, ok := node.(*ast.BlockStmt) - if !ok { - return true - } - if len(block.List) < 2 { - return true - } - for i, stmt := range block.List[:len(block.List)-1] { - _ = i - decl, ok := stmt.(*ast.DeclStmt) - if !ok { - continue - } - gdecl, ok := decl.Decl.(*ast.GenDecl) - if !ok || gdecl.Tok != token.VAR || len(gdecl.Specs) != 1 { - continue - } - vspec, ok := gdecl.Specs[0].(*ast.ValueSpec) - if !ok || len(vspec.Names) != 1 || len(vspec.Values) != 0 { - continue - } - - assign, ok := block.List[i+1].(*ast.AssignStmt) - if !ok || assign.Tok != token.ASSIGN { - continue - } - if len(assign.Lhs) != 1 || len(assign.Rhs) != 1 { - continue - } - ident, ok := assign.Lhs[0].(*ast.Ident) - if !ok { - continue - } - if vspec.Names[0].Obj != ident.Obj { - continue - } - - if refersTo(j, assign.Rhs[0], ident) { - continue - } - j.Errorf(decl, "should merge variable declaration with assignment on next line") - } - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) LintRedundantBreak(j *lint.Job) { - fn1 := func(node ast.Node) { - clause, ok := node.(*ast.CaseClause) - if !ok { - return - } - if len(clause.Body) < 2 { - return - } - branch, ok := clause.Body[len(clause.Body)-1].(*ast.BranchStmt) - if !ok || branch.Tok != token.BREAK || branch.Label != nil { - return - } - j.Errorf(branch, "redundant break statement") - } - fn2 := func(node ast.Node) { - var ret *ast.FieldList - var body *ast.BlockStmt - switch x := node.(type) { - case *ast.FuncDecl: - ret = x.Type.Results - body = x.Body - case *ast.FuncLit: - ret = x.Type.Results - body = x.Body - default: - return - } - // if the func has results, a return can't be redundant. - // similarly, if there are no statements, there can be - // no return. - if ret != nil || body == nil || len(body.List) < 1 { - return - } - rst, ok := body.List[len(body.List)-1].(*ast.ReturnStmt) - if !ok { - return - } - // we don't need to check rst.Results as we already - // checked x.Type.Results to be nil. - j.Errorf(rst, "redundant return statement") - } - fn := func(node ast.Node) bool { - fn1(node) - fn2(node) - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) Implements(j *lint.Job, typ types.Type, iface string) bool { - // OPT(dh): we can cache the type lookup - idx := strings.IndexRune(iface, '.') - var scope *types.Scope - var ifaceName string - if idx == -1 { - scope = types.Universe - ifaceName = iface - } else { - pkgName := iface[:idx] - pkg := j.Program.Package(pkgName) - if pkg == nil { - return false - } - scope = pkg.Types.Scope() - ifaceName = iface[idx+1:] - } - - obj := scope.Lookup(ifaceName) - if obj == nil { - return false - } - i, ok := obj.Type().Underlying().(*types.Interface) - if !ok { - return false - } - return types.Implements(typ, i) -} - -func (c *Checker) LintRedundantSprintf(j *lint.Job) { - fn := func(node ast.Node) bool { - call, ok := node.(*ast.CallExpr) - if !ok { - return true - } - if !IsCallToAST(j, call, "fmt.Sprintf") { - return true - } - if len(call.Args) != 2 { - return true - } - if s, ok := ExprToString(j, call.Args[Arg("fmt.Sprintf.format")]); !ok || s != "%s" { - return true - } - arg := call.Args[Arg("fmt.Sprintf.a[0]")] - typ := TypeOf(j, arg) - - if c.Implements(j, typ, "fmt.Stringer") { - j.Errorf(call, "should use String() instead of fmt.Sprintf") - return true - } - - if typ.Underlying() == types.Universe.Lookup("string").Type() { - if typ == types.Universe.Lookup("string").Type() { - j.Errorf(call, "the argument is already a string, there's no need to use fmt.Sprintf") - } else { - j.Errorf(call, "the argument's underlying type is a string, should use a simple conversion instead of fmt.Sprintf") - } - } - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) LintErrorsNewSprintf(j *lint.Job) { - fn := func(node ast.Node) bool { - if !IsCallToAST(j, node, "errors.New") { - return true - } - call := node.(*ast.CallExpr) - if !IsCallToAST(j, call.Args[Arg("errors.New.text")], "fmt.Sprintf") { - return true - } - j.Errorf(node, "should use fmt.Errorf(...) instead of errors.New(fmt.Sprintf(...))") - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) LintRangeStringRunes(j *lint.Job) { - sharedcheck.CheckRangeStringRunes(j) -} - -func (c *Checker) LintNilCheckAroundRange(j *lint.Job) { - fn := func(node ast.Node) bool { - ifstmt, ok := node.(*ast.IfStmt) - if !ok { - return true - } - - cond, ok := ifstmt.Cond.(*ast.BinaryExpr) - if !ok { - return true - } - - if cond.Op != token.NEQ || !IsNil(j, cond.Y) || len(ifstmt.Body.List) != 1 { - return true - } - - loop, ok := ifstmt.Body.List[0].(*ast.RangeStmt) - if !ok { - return true - } - ifXIdent, ok := cond.X.(*ast.Ident) - if !ok { - return true - } - rangeXIdent, ok := loop.X.(*ast.Ident) - if !ok { - return true - } - if ifXIdent.Obj != rangeXIdent.Obj { - return true - } - switch TypeOf(j, rangeXIdent).(type) { - case *types.Slice, *types.Map: - j.Errorf(node, "unnecessary nil check around range") - } - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func isPermissibleSort(j *lint.Job, node ast.Node) bool { - call := node.(*ast.CallExpr) - typeconv, ok := call.Args[0].(*ast.CallExpr) - if !ok { - return true - } - - sel, ok := typeconv.Fun.(*ast.SelectorExpr) - if !ok { - return true - } - name := SelectorName(j, sel) - switch name { - case "sort.IntSlice", "sort.Float64Slice", "sort.StringSlice": - default: - return true - } - - return false -} - -func (c *Checker) LintSortHelpers(j *lint.Job) { - fnFuncs := func(node ast.Node) bool { - var body *ast.BlockStmt - switch node := node.(type) { - case *ast.FuncLit: - body = node.Body - case *ast.FuncDecl: - body = node.Body - default: - return true - } - if body == nil { - return true - } - - type Error struct { - node lint.Positioner - msg string - } - var errors []Error - permissible := false - fnSorts := func(node ast.Node) bool { - if permissible { - return false - } - if !IsCallToAST(j, node, "sort.Sort") { - return true - } - if isPermissibleSort(j, node) { - permissible = true - return false - } - call := node.(*ast.CallExpr) - typeconv := call.Args[Arg("sort.Sort.data")].(*ast.CallExpr) - sel := typeconv.Fun.(*ast.SelectorExpr) - name := SelectorName(j, sel) - - switch name { - case "sort.IntSlice": - errors = append(errors, Error{node, "should use sort.Ints(...) instead of sort.Sort(sort.IntSlice(...))"}) - case "sort.Float64Slice": - errors = append(errors, Error{node, "should use sort.Float64s(...) instead of sort.Sort(sort.Float64Slice(...))"}) - case "sort.StringSlice": - errors = append(errors, Error{node, "should use sort.Strings(...) instead of sort.Sort(sort.StringSlice(...))"}) - } - return true - } - ast.Inspect(body, fnSorts) - - if permissible { - return false - } - for _, err := range errors { - j.Errorf(err.node, "%s", err.msg) - } - return false - } - - for _, f := range j.Program.Files { - ast.Inspect(f, fnFuncs) - } -} diff --git a/vendor/github.com/golangci/go-tools/ssa/ssautil/load.go b/vendor/github.com/golangci/go-tools/ssa/ssautil/load.go deleted file mode 100644 index c7832678b9b7..000000000000 --- a/vendor/github.com/golangci/go-tools/ssa/ssautil/load.go +++ /dev/null @@ -1,143 +0,0 @@ -// Copyright 2015 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssautil - -// This file defines utility functions for constructing programs in SSA form. - -import ( - "go/ast" - "go/token" - "go/types" - - "golang.org/x/tools/go/loader" - "golang.org/x/tools/go/packages" - "github.com/golangci/go-tools/ssa" -) - -// Packages creates an SSA program for a set of packages loaded from -// source syntax using the golang.org/x/tools/go/packages.Load function. -// It creates and returns an SSA package for each well-typed package in -// the initial list. The resulting list of packages has the same length -// as initial, and contains a nil if SSA could not be constructed for -// the corresponding initial package. -// -// Code for bodies of functions is not built until Build is called -// on the resulting Program. -// -// The mode parameter controls diagnostics and checking during SSA construction. -// -func Packages(initial []*packages.Package, mode ssa.BuilderMode) (*ssa.Program, []*ssa.Package) { - var fset *token.FileSet - if len(initial) > 0 { - fset = initial[0].Fset - } - - prog := ssa.NewProgram(fset, mode) - seen := make(map[*packages.Package]*ssa.Package) - var create func(p *packages.Package) *ssa.Package - create = func(p *packages.Package) *ssa.Package { - ssapkg, ok := seen[p] - if !ok { - if p.Types == nil || p.IllTyped { - // not well typed - seen[p] = nil - return nil - } - - ssapkg = prog.CreatePackage(p.Types, p.Syntax, p.TypesInfo, true) - seen[p] = ssapkg - - for _, imp := range p.Imports { - create(imp) - } - } - return ssapkg - } - - var ssapkgs []*ssa.Package - for _, p := range initial { - ssapkgs = append(ssapkgs, create(p)) - } - return prog, ssapkgs -} - -// CreateProgram returns a new program in SSA form, given a program -// loaded from source. An SSA package is created for each transitively -// error-free package of lprog. -// -// Code for bodies of functions is not built until Build is called -// on the result. -// -// mode controls diagnostics and checking during SSA construction. -// -func CreateProgram(lprog *loader.Program, mode ssa.BuilderMode) *ssa.Program { - prog := ssa.NewProgram(lprog.Fset, mode) - - for _, info := range lprog.AllPackages { - if info.TransitivelyErrorFree { - prog.CreatePackage(info.Pkg, info.Files, &info.Info, info.Importable) - } - } - - return prog -} - -// BuildPackage builds an SSA program with IR for a single package. -// -// It populates pkg by type-checking the specified file ASTs. All -// dependencies are loaded using the importer specified by tc, which -// typically loads compiler export data; SSA code cannot be built for -// those packages. BuildPackage then constructs an ssa.Program with all -// dependency packages created, and builds and returns the SSA package -// corresponding to pkg. -// -// The caller must have set pkg.Path() to the import path. -// -// The operation fails if there were any type-checking or import errors. -// -// See ../ssa/example_test.go for an example. -// -func BuildPackage(tc *types.Config, fset *token.FileSet, pkg *types.Package, files []*ast.File, mode ssa.BuilderMode) (*ssa.Package, *types.Info, error) { - if fset == nil { - panic("no token.FileSet") - } - if pkg.Path() == "" { - panic("package has no import path") - } - - info := &types.Info{ - Types: make(map[ast.Expr]types.TypeAndValue), - Defs: make(map[*ast.Ident]types.Object), - Uses: make(map[*ast.Ident]types.Object), - Implicits: make(map[ast.Node]types.Object), - Scopes: make(map[ast.Node]*types.Scope), - Selections: make(map[*ast.SelectorExpr]*types.Selection), - } - if err := types.NewChecker(tc, fset, pkg, info).Files(files); err != nil { - return nil, nil, err - } - - prog := ssa.NewProgram(fset, mode) - - // Create SSA packages for all imports. - // Order is not significant. - created := make(map[*types.Package]bool) - var createAll func(pkgs []*types.Package) - createAll = func(pkgs []*types.Package) { - for _, p := range pkgs { - if !created[p] { - created[p] = true - prog.CreatePackage(p, nil, nil, true) - createAll(p.Imports()) - } - } - } - createAll(pkg.Imports()) - - // Create and build the primary package. - ssapkg := prog.CreatePackage(pkg, files, info, false) - ssapkg.Build() - return ssapkg, info, nil -} diff --git a/vendor/github.com/golangci/go-tools/ssa/ssautil/switch.go b/vendor/github.com/golangci/go-tools/ssa/ssautil/switch.go deleted file mode 100644 index 05c388b2b439..000000000000 --- a/vendor/github.com/golangci/go-tools/ssa/ssautil/switch.go +++ /dev/null @@ -1,234 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssautil - -// This file implements discovery of switch and type-switch constructs -// from low-level control flow. -// -// Many techniques exist for compiling a high-level switch with -// constant cases to efficient machine code. The optimal choice will -// depend on the data type, the specific case values, the code in the -// body of each case, and the hardware. -// Some examples: -// - a lookup table (for a switch that maps constants to constants) -// - a computed goto -// - a binary tree -// - a perfect hash -// - a two-level switch (to partition constant strings by their first byte). - -import ( - "bytes" - "fmt" - "go/token" - "go/types" - - "github.com/golangci/go-tools/ssa" -) - -// A ConstCase represents a single constant comparison. -// It is part of a Switch. -type ConstCase struct { - Block *ssa.BasicBlock // block performing the comparison - Body *ssa.BasicBlock // body of the case - Value *ssa.Const // case comparand -} - -// A TypeCase represents a single type assertion. -// It is part of a Switch. -type TypeCase struct { - Block *ssa.BasicBlock // block performing the type assert - Body *ssa.BasicBlock // body of the case - Type types.Type // case type - Binding ssa.Value // value bound by this case -} - -// A Switch is a logical high-level control flow operation -// (a multiway branch) discovered by analysis of a CFG containing -// only if/else chains. It is not part of the ssa.Instruction set. -// -// One of ConstCases and TypeCases has length >= 2; -// the other is nil. -// -// In a value switch, the list of cases may contain duplicate constants. -// A type switch may contain duplicate types, or types assignable -// to an interface type also in the list. -// TODO(adonovan): eliminate such duplicates. -// -type Switch struct { - Start *ssa.BasicBlock // block containing start of if/else chain - X ssa.Value // the switch operand - ConstCases []ConstCase // ordered list of constant comparisons - TypeCases []TypeCase // ordered list of type assertions - Default *ssa.BasicBlock // successor if all comparisons fail -} - -func (sw *Switch) String() string { - // We represent each block by the String() of its - // first Instruction, e.g. "print(42:int)". - var buf bytes.Buffer - if sw.ConstCases != nil { - fmt.Fprintf(&buf, "switch %s {\n", sw.X.Name()) - for _, c := range sw.ConstCases { - fmt.Fprintf(&buf, "case %s: %s\n", c.Value, c.Body.Instrs[0]) - } - } else { - fmt.Fprintf(&buf, "switch %s.(type) {\n", sw.X.Name()) - for _, c := range sw.TypeCases { - fmt.Fprintf(&buf, "case %s %s: %s\n", - c.Binding.Name(), c.Type, c.Body.Instrs[0]) - } - } - if sw.Default != nil { - fmt.Fprintf(&buf, "default: %s\n", sw.Default.Instrs[0]) - } - fmt.Fprintf(&buf, "}") - return buf.String() -} - -// Switches examines the control-flow graph of fn and returns the -// set of inferred value and type switches. A value switch tests an -// ssa.Value for equality against two or more compile-time constant -// values. Switches involving link-time constants (addresses) are -// ignored. A type switch type-asserts an ssa.Value against two or -// more types. -// -// The switches are returned in dominance order. -// -// The resulting switches do not necessarily correspond to uses of the -// 'switch' keyword in the source: for example, a single source-level -// switch statement with non-constant cases may result in zero, one or -// many Switches, one per plural sequence of constant cases. -// Switches may even be inferred from if/else- or goto-based control flow. -// (In general, the control flow constructs of the source program -// cannot be faithfully reproduced from the SSA representation.) -// -func Switches(fn *ssa.Function) []Switch { - // Traverse the CFG in dominance order, so we don't - // enter an if/else-chain in the middle. - var switches []Switch - seen := make(map[*ssa.BasicBlock]bool) // TODO(adonovan): opt: use ssa.blockSet - for _, b := range fn.DomPreorder() { - if x, k := isComparisonBlock(b); x != nil { - // Block b starts a switch. - sw := Switch{Start: b, X: x} - valueSwitch(&sw, k, seen) - if len(sw.ConstCases) > 1 { - switches = append(switches, sw) - } - } - - if y, x, T := isTypeAssertBlock(b); y != nil { - // Block b starts a type switch. - sw := Switch{Start: b, X: x} - typeSwitch(&sw, y, T, seen) - if len(sw.TypeCases) > 1 { - switches = append(switches, sw) - } - } - } - return switches -} - -func valueSwitch(sw *Switch, k *ssa.Const, seen map[*ssa.BasicBlock]bool) { - b := sw.Start - x := sw.X - for x == sw.X { - if seen[b] { - break - } - seen[b] = true - - sw.ConstCases = append(sw.ConstCases, ConstCase{ - Block: b, - Body: b.Succs[0], - Value: k, - }) - b = b.Succs[1] - if len(b.Instrs) > 2 { - // Block b contains not just 'if x == k', - // so it may have side effects that - // make it unsafe to elide. - break - } - if len(b.Preds) != 1 { - // Block b has multiple predecessors, - // so it cannot be treated as a case. - break - } - x, k = isComparisonBlock(b) - } - sw.Default = b -} - -func typeSwitch(sw *Switch, y ssa.Value, T types.Type, seen map[*ssa.BasicBlock]bool) { - b := sw.Start - x := sw.X - for x == sw.X { - if seen[b] { - break - } - seen[b] = true - - sw.TypeCases = append(sw.TypeCases, TypeCase{ - Block: b, - Body: b.Succs[0], - Type: T, - Binding: y, - }) - b = b.Succs[1] - if len(b.Instrs) > 4 { - // Block b contains not just - // {TypeAssert; Extract #0; Extract #1; If} - // so it may have side effects that - // make it unsafe to elide. - break - } - if len(b.Preds) != 1 { - // Block b has multiple predecessors, - // so it cannot be treated as a case. - break - } - y, x, T = isTypeAssertBlock(b) - } - sw.Default = b -} - -// isComparisonBlock returns the operands (v, k) if a block ends with -// a comparison v==k, where k is a compile-time constant. -// -func isComparisonBlock(b *ssa.BasicBlock) (v ssa.Value, k *ssa.Const) { - if n := len(b.Instrs); n >= 2 { - if i, ok := b.Instrs[n-1].(*ssa.If); ok { - if binop, ok := i.Cond.(*ssa.BinOp); ok && binop.Block() == b && binop.Op == token.EQL { - if k, ok := binop.Y.(*ssa.Const); ok { - return binop.X, k - } - if k, ok := binop.X.(*ssa.Const); ok { - return binop.Y, k - } - } - } - } - return -} - -// isTypeAssertBlock returns the operands (y, x, T) if a block ends with -// a type assertion "if y, ok := x.(T); ok {". -// -func isTypeAssertBlock(b *ssa.BasicBlock) (y, x ssa.Value, T types.Type) { - if n := len(b.Instrs); n >= 4 { - if i, ok := b.Instrs[n-1].(*ssa.If); ok { - if ext1, ok := i.Cond.(*ssa.Extract); ok && ext1.Block() == b && ext1.Index == 1 { - if ta, ok := ext1.Tuple.(*ssa.TypeAssert); ok && ta.Block() == b { - // hack: relies upon instruction ordering. - if ext0, ok := b.Instrs[n-3].(*ssa.Extract); ok { - return ext0, ta.X, ta.AssertedType - } - } - } - } - } - return -} diff --git a/vendor/github.com/golangci/go-tools/ssa/ssautil/visit.go b/vendor/github.com/golangci/go-tools/ssa/ssautil/visit.go deleted file mode 100644 index 02b2aac20f2c..000000000000 --- a/vendor/github.com/golangci/go-tools/ssa/ssautil/visit.go +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssautil // import "github.com/golangci/go-tools/ssa/ssautil" - -import "github.com/golangci/go-tools/ssa" - -// This file defines utilities for visiting the SSA representation of -// a Program. -// -// TODO(adonovan): test coverage. - -// AllFunctions finds and returns the set of functions potentially -// needed by program prog, as determined by a simple linker-style -// reachability algorithm starting from the members and method-sets of -// each package. The result may include anonymous functions and -// synthetic wrappers. -// -// Precondition: all packages are built. -// -func AllFunctions(prog *ssa.Program) map[*ssa.Function]bool { - visit := visitor{ - prog: prog, - seen: make(map[*ssa.Function]bool), - } - visit.program() - return visit.seen -} - -type visitor struct { - prog *ssa.Program - seen map[*ssa.Function]bool -} - -func (visit *visitor) program() { - for _, pkg := range visit.prog.AllPackages() { - for _, mem := range pkg.Members { - if fn, ok := mem.(*ssa.Function); ok { - visit.function(fn) - } - } - } - for _, T := range visit.prog.RuntimeTypes() { - mset := visit.prog.MethodSets.MethodSet(T) - for i, n := 0, mset.Len(); i < n; i++ { - visit.function(visit.prog.MethodValue(mset.At(i))) - } - } -} - -func (visit *visitor) function(fn *ssa.Function) { - if !visit.seen[fn] { - visit.seen[fn] = true - var buf [10]*ssa.Value // avoid alloc in common case - for _, b := range fn.Blocks { - for _, instr := range b.Instrs { - for _, op := range instr.Operands(buf[:0]) { - if fn, ok := (*op).(*ssa.Function); ok { - visit.function(fn) - } - } - } - } - } -} - -// MainPackages returns the subset of the specified packages -// named "main" that define a main function. -// The result may include synthetic "testmain" packages. -func MainPackages(pkgs []*ssa.Package) []*ssa.Package { - var mains []*ssa.Package - for _, pkg := range pkgs { - if pkg.Pkg.Name() == "main" && pkg.Func("main") != nil { - mains = append(mains, pkg) - } - } - return mains -} diff --git a/vendor/github.com/golangci/go-tools/staticcheck/lint.go b/vendor/github.com/golangci/go-tools/staticcheck/lint.go deleted file mode 100644 index ef4c893e1a4c..000000000000 --- a/vendor/github.com/golangci/go-tools/staticcheck/lint.go +++ /dev/null @@ -1,2818 +0,0 @@ -// Package staticcheck contains a linter for Go source code. -package staticcheck // import "github.com/golangci/go-tools/staticcheck" - -import ( - "fmt" - "go/ast" - "go/constant" - "go/token" - "go/types" - htmltemplate "html/template" - "net/http" - "regexp" - "regexp/syntax" - "sort" - "strconv" - "strings" - "sync" - texttemplate "text/template" - - . "github.com/golangci/go-tools/arg" - "github.com/golangci/go-tools/deprecated" - "github.com/golangci/go-tools/functions" - "github.com/golangci/go-tools/internal/sharedcheck" - "github.com/golangci/go-tools/lint" - . "github.com/golangci/go-tools/lint/lintdsl" - "github.com/golangci/go-tools/ssa" - "github.com/golangci/go-tools/ssautil" - "github.com/golangci/go-tools/staticcheck/vrp" - - "golang.org/x/tools/go/ast/astutil" - "golang.org/x/tools/go/packages" -) - -func validRegexp(call *Call) { - arg := call.Args[0] - err := ValidateRegexp(arg.Value) - if err != nil { - arg.Invalid(err.Error()) - } -} - -type runeSlice []rune - -func (rs runeSlice) Len() int { return len(rs) } -func (rs runeSlice) Less(i int, j int) bool { return rs[i] < rs[j] } -func (rs runeSlice) Swap(i int, j int) { rs[i], rs[j] = rs[j], rs[i] } - -func utf8Cutset(call *Call) { - arg := call.Args[1] - if InvalidUTF8(arg.Value) { - arg.Invalid(MsgInvalidUTF8) - } -} - -func uniqueCutset(call *Call) { - arg := call.Args[1] - if !UniqueStringCutset(arg.Value) { - arg.Invalid(MsgNonUniqueCutset) - } -} - -func unmarshalPointer(name string, arg int) CallCheck { - return func(call *Call) { - if !Pointer(call.Args[arg].Value) { - call.Args[arg].Invalid(fmt.Sprintf("%s expects to unmarshal into a pointer, but the provided value is not a pointer", name)) - } - } -} - -func pointlessIntMath(call *Call) { - if ConvertedFromInt(call.Args[0].Value) { - call.Invalid(fmt.Sprintf("calling %s on a converted integer is pointless", CallName(call.Instr.Common()))) - } -} - -func checkValidHostPort(arg int) CallCheck { - return func(call *Call) { - if !ValidHostPort(call.Args[arg].Value) { - call.Args[arg].Invalid(MsgInvalidHostPort) - } - } -} - -var ( - checkRegexpRules = map[string]CallCheck{ - "regexp.MustCompile": validRegexp, - "regexp.Compile": validRegexp, - "regexp.Match": validRegexp, - "regexp.MatchReader": validRegexp, - "regexp.MatchString": validRegexp, - } - - checkTimeParseRules = map[string]CallCheck{ - "time.Parse": func(call *Call) { - arg := call.Args[Arg("time.Parse.layout")] - err := ValidateTimeLayout(arg.Value) - if err != nil { - arg.Invalid(err.Error()) - } - }, - } - - checkEncodingBinaryRules = map[string]CallCheck{ - "encoding/binary.Write": func(call *Call) { - arg := call.Args[Arg("encoding/binary.Write.data")] - if !CanBinaryMarshal(call.Job, arg.Value) { - arg.Invalid(fmt.Sprintf("value of type %s cannot be used with binary.Write", arg.Value.Value.Type())) - } - }, - } - - checkURLsRules = map[string]CallCheck{ - "net/url.Parse": func(call *Call) { - arg := call.Args[Arg("net/url.Parse.rawurl")] - err := ValidateURL(arg.Value) - if err != nil { - arg.Invalid(err.Error()) - } - }, - } - - checkSyncPoolValueRules = map[string]CallCheck{ - "(*sync.Pool).Put": func(call *Call) { - arg := call.Args[Arg("(*sync.Pool).Put.x")] - typ := arg.Value.Value.Type() - if !IsPointerLike(typ) { - arg.Invalid("argument should be pointer-like to avoid allocations") - } - }, - } - - checkRegexpFindAllRules = map[string]CallCheck{ - "(*regexp.Regexp).FindAll": RepeatZeroTimes("a FindAll method", 1), - "(*regexp.Regexp).FindAllIndex": RepeatZeroTimes("a FindAll method", 1), - "(*regexp.Regexp).FindAllString": RepeatZeroTimes("a FindAll method", 1), - "(*regexp.Regexp).FindAllStringIndex": RepeatZeroTimes("a FindAll method", 1), - "(*regexp.Regexp).FindAllStringSubmatch": RepeatZeroTimes("a FindAll method", 1), - "(*regexp.Regexp).FindAllStringSubmatchIndex": RepeatZeroTimes("a FindAll method", 1), - "(*regexp.Regexp).FindAllSubmatch": RepeatZeroTimes("a FindAll method", 1), - "(*regexp.Regexp).FindAllSubmatchIndex": RepeatZeroTimes("a FindAll method", 1), - } - - checkUTF8CutsetRules = map[string]CallCheck{ - "strings.IndexAny": utf8Cutset, - "strings.LastIndexAny": utf8Cutset, - "strings.ContainsAny": utf8Cutset, - "strings.Trim": utf8Cutset, - "strings.TrimLeft": utf8Cutset, - "strings.TrimRight": utf8Cutset, - } - - checkUniqueCutsetRules = map[string]CallCheck{ - "strings.Trim": uniqueCutset, - "strings.TrimLeft": uniqueCutset, - "strings.TrimRight": uniqueCutset, - } - - checkUnmarshalPointerRules = map[string]CallCheck{ - "encoding/xml.Unmarshal": unmarshalPointer("xml.Unmarshal", 1), - "(*encoding/xml.Decoder).Decode": unmarshalPointer("Decode", 0), - "(*encoding/xml.Decoder).DecodeElement": unmarshalPointer("DecodeElement", 0), - "encoding/json.Unmarshal": unmarshalPointer("json.Unmarshal", 1), - "(*encoding/json.Decoder).Decode": unmarshalPointer("Decode", 0), - } - - checkUnbufferedSignalChanRules = map[string]CallCheck{ - "os/signal.Notify": func(call *Call) { - arg := call.Args[Arg("os/signal.Notify.c")] - if UnbufferedChannel(arg.Value) { - arg.Invalid("the channel used with signal.Notify should be buffered") - } - }, - } - - checkMathIntRules = map[string]CallCheck{ - "math.Ceil": pointlessIntMath, - "math.Floor": pointlessIntMath, - "math.IsNaN": pointlessIntMath, - "math.Trunc": pointlessIntMath, - "math.IsInf": pointlessIntMath, - } - - checkStringsReplaceZeroRules = map[string]CallCheck{ - "strings.Replace": RepeatZeroTimes("strings.Replace", 3), - "bytes.Replace": RepeatZeroTimes("bytes.Replace", 3), - } - - checkListenAddressRules = map[string]CallCheck{ - "net/http.ListenAndServe": checkValidHostPort(0), - "net/http.ListenAndServeTLS": checkValidHostPort(0), - } - - checkBytesEqualIPRules = map[string]CallCheck{ - "bytes.Equal": func(call *Call) { - if ConvertedFrom(call.Args[Arg("bytes.Equal.a")].Value, "net.IP") && - ConvertedFrom(call.Args[Arg("bytes.Equal.b")].Value, "net.IP") { - call.Invalid("use net.IP.Equal to compare net.IPs, not bytes.Equal") - } - }, - } - - checkRegexpMatchLoopRules = map[string]CallCheck{ - "regexp.Match": loopedRegexp("regexp.Match"), - "regexp.MatchReader": loopedRegexp("regexp.MatchReader"), - "regexp.MatchString": loopedRegexp("regexp.MatchString"), - } -) - -type Checker struct { - CheckGenerated bool - funcDescs *functions.Descriptions - deprecatedObjs map[types.Object]string -} - -func NewChecker() *Checker { - return &Checker{} -} - -func (*Checker) Name() string { return "staticcheck" } -func (*Checker) Prefix() string { return "SA" } - -func (c *Checker) Checks() []lint.Check { - return []lint.Check{ - {ID: "SA1000", FilterGenerated: false, Fn: c.callChecker(checkRegexpRules)}, - {ID: "SA1001", FilterGenerated: false, Fn: c.CheckTemplate}, - {ID: "SA1002", FilterGenerated: false, Fn: c.callChecker(checkTimeParseRules)}, - {ID: "SA1003", FilterGenerated: false, Fn: c.callChecker(checkEncodingBinaryRules)}, - {ID: "SA1004", FilterGenerated: false, Fn: c.CheckTimeSleepConstant}, - {ID: "SA1005", FilterGenerated: false, Fn: c.CheckExec}, - {ID: "SA1006", FilterGenerated: false, Fn: c.CheckUnsafePrintf}, - {ID: "SA1007", FilterGenerated: false, Fn: c.callChecker(checkURLsRules)}, - {ID: "SA1008", FilterGenerated: false, Fn: c.CheckCanonicalHeaderKey}, - {ID: "SA1010", FilterGenerated: false, Fn: c.callChecker(checkRegexpFindAllRules)}, - {ID: "SA1011", FilterGenerated: false, Fn: c.callChecker(checkUTF8CutsetRules)}, - {ID: "SA1012", FilterGenerated: false, Fn: c.CheckNilContext}, - {ID: "SA1013", FilterGenerated: false, Fn: c.CheckSeeker}, - {ID: "SA1014", FilterGenerated: false, Fn: c.callChecker(checkUnmarshalPointerRules)}, - {ID: "SA1015", FilterGenerated: false, Fn: c.CheckLeakyTimeTick}, - {ID: "SA1016", FilterGenerated: false, Fn: c.CheckUntrappableSignal}, - {ID: "SA1017", FilterGenerated: false, Fn: c.callChecker(checkUnbufferedSignalChanRules)}, - {ID: "SA1018", FilterGenerated: false, Fn: c.callChecker(checkStringsReplaceZeroRules)}, - {ID: "SA1019", FilterGenerated: false, Fn: c.CheckDeprecated}, - {ID: "SA1020", FilterGenerated: false, Fn: c.callChecker(checkListenAddressRules)}, - {ID: "SA1021", FilterGenerated: false, Fn: c.callChecker(checkBytesEqualIPRules)}, - {ID: "SA1023", FilterGenerated: false, Fn: c.CheckWriterBufferModified}, - {ID: "SA1024", FilterGenerated: false, Fn: c.callChecker(checkUniqueCutsetRules)}, - {ID: "SA1025", FilterGenerated: false, Fn: c.CheckTimerResetReturnValue}, - - {ID: "SA2000", FilterGenerated: false, Fn: c.CheckWaitgroupAdd}, - {ID: "SA2001", FilterGenerated: false, Fn: c.CheckEmptyCriticalSection}, - {ID: "SA2002", FilterGenerated: false, Fn: c.CheckConcurrentTesting}, - {ID: "SA2003", FilterGenerated: false, Fn: c.CheckDeferLock}, - - {ID: "SA3000", FilterGenerated: false, Fn: c.CheckTestMainExit}, - {ID: "SA3001", FilterGenerated: false, Fn: c.CheckBenchmarkN}, - - {ID: "SA4000", FilterGenerated: false, Fn: c.CheckLhsRhsIdentical}, - {ID: "SA4001", FilterGenerated: false, Fn: c.CheckIneffectiveCopy}, - {ID: "SA4002", FilterGenerated: false, Fn: c.CheckDiffSizeComparison}, - {ID: "SA4003", FilterGenerated: false, Fn: c.CheckUnsignedComparison}, - {ID: "SA4004", FilterGenerated: false, Fn: c.CheckIneffectiveLoop}, - {ID: "SA4006", FilterGenerated: false, Fn: c.CheckUnreadVariableValues}, - {ID: "SA4008", FilterGenerated: false, Fn: c.CheckLoopCondition}, - {ID: "SA4009", FilterGenerated: false, Fn: c.CheckArgOverwritten}, - {ID: "SA4010", FilterGenerated: false, Fn: c.CheckIneffectiveAppend}, - {ID: "SA4011", FilterGenerated: false, Fn: c.CheckScopedBreak}, - {ID: "SA4012", FilterGenerated: false, Fn: c.CheckNaNComparison}, - {ID: "SA4013", FilterGenerated: false, Fn: c.CheckDoubleNegation}, - {ID: "SA4014", FilterGenerated: false, Fn: c.CheckRepeatedIfElse}, - {ID: "SA4015", FilterGenerated: false, Fn: c.callChecker(checkMathIntRules)}, - {ID: "SA4016", FilterGenerated: false, Fn: c.CheckSillyBitwiseOps}, - {ID: "SA4017", FilterGenerated: false, Fn: c.CheckPureFunctions}, - {ID: "SA4018", FilterGenerated: true, Fn: c.CheckSelfAssignment}, - {ID: "SA4019", FilterGenerated: true, Fn: c.CheckDuplicateBuildConstraints}, - - {ID: "SA5000", FilterGenerated: false, Fn: c.CheckNilMaps}, - {ID: "SA5001", FilterGenerated: false, Fn: c.CheckEarlyDefer}, - {ID: "SA5002", FilterGenerated: false, Fn: c.CheckInfiniteEmptyLoop}, - {ID: "SA5003", FilterGenerated: false, Fn: c.CheckDeferInInfiniteLoop}, - {ID: "SA5004", FilterGenerated: false, Fn: c.CheckLoopEmptyDefault}, - {ID: "SA5005", FilterGenerated: false, Fn: c.CheckCyclicFinalizer}, - {ID: "SA5007", FilterGenerated: false, Fn: c.CheckInfiniteRecursion}, - - {ID: "SA6000", FilterGenerated: false, Fn: c.callChecker(checkRegexpMatchLoopRules)}, - {ID: "SA6001", FilterGenerated: false, Fn: c.CheckMapBytesKey}, - {ID: "SA6002", FilterGenerated: false, Fn: c.callChecker(checkSyncPoolValueRules)}, - {ID: "SA6003", FilterGenerated: false, Fn: c.CheckRangeStringRunes}, - // {ID: "SA6004", FilterGenerated: false, Fn: c.CheckSillyRegexp}, - - {ID: "SA9001", FilterGenerated: false, Fn: c.CheckDubiousDeferInChannelRangeLoop}, - {ID: "SA9002", FilterGenerated: false, Fn: c.CheckNonOctalFileMode}, - {ID: "SA9003", FilterGenerated: false, Fn: c.CheckEmptyBranch}, - {ID: "SA9004", FilterGenerated: false, Fn: c.CheckMissingEnumTypesInDeclaration}, - } - - // "SA5006": c.CheckSliceOutOfBounds, - // "SA4007": c.CheckPredeterminedBooleanExprs, -} - -func (c *Checker) findDeprecated(prog *lint.Program) { - var docs []*ast.CommentGroup - var names []*ast.Ident - - doDocs := func(pkg *packages.Package, names []*ast.Ident, docs []*ast.CommentGroup) { - var alt string - for _, doc := range docs { - if doc == nil { - continue - } - parts := strings.Split(doc.Text(), "\n\n") - last := parts[len(parts)-1] - if !strings.HasPrefix(last, "Deprecated: ") { - continue - } - alt = last[len("Deprecated: "):] - alt = strings.Replace(alt, "\n", " ", -1) - break - } - if alt == "" { - return - } - - for _, name := range names { - obj := pkg.TypesInfo.ObjectOf(name) - c.deprecatedObjs[obj] = alt - } - } - - for _, pkg := range prog.AllPackages { - for _, f := range pkg.Syntax { - fn := func(node ast.Node) bool { - if node == nil { - return true - } - var ret bool - switch node := node.(type) { - case *ast.GenDecl: - switch node.Tok { - case token.TYPE, token.CONST, token.VAR: - docs = append(docs, node.Doc) - return true - default: - return false - } - case *ast.FuncDecl: - docs = append(docs, node.Doc) - names = []*ast.Ident{node.Name} - ret = false - case *ast.TypeSpec: - docs = append(docs, node.Doc) - names = []*ast.Ident{node.Name} - ret = true - case *ast.ValueSpec: - docs = append(docs, node.Doc) - names = node.Names - ret = false - case *ast.File: - return true - case *ast.StructType: - for _, field := range node.Fields.List { - doDocs(pkg, field.Names, []*ast.CommentGroup{field.Doc}) - } - return false - case *ast.InterfaceType: - for _, field := range node.Methods.List { - doDocs(pkg, field.Names, []*ast.CommentGroup{field.Doc}) - } - return false - default: - return false - } - if len(names) == 0 || len(docs) == 0 { - return ret - } - doDocs(pkg, names, docs) - - docs = docs[:0] - names = nil - return ret - } - ast.Inspect(f, fn) - } - } -} - -func (c *Checker) Init(prog *lint.Program) { - wg := &sync.WaitGroup{} - wg.Add(2) - go func() { - c.funcDescs = functions.NewDescriptions(prog.SSA) - for _, fn := range prog.AllFunctions { - if fn.Blocks != nil { - applyStdlibKnowledge(fn) - ssa.OptimizeBlocks(fn) - } - } - wg.Done() - }() - - go func() { - c.deprecatedObjs = map[types.Object]string{} - c.findDeprecated(prog) - wg.Done() - }() - - wg.Wait() -} - -func (c *Checker) isInLoop(b *ssa.BasicBlock) bool { - sets := c.funcDescs.Get(b.Parent()).Loops - for _, set := range sets { - if set[b] { - return true - } - } - return false -} - -func applyStdlibKnowledge(fn *ssa.Function) { - if len(fn.Blocks) == 0 { - return - } - - // comma-ok receiving from a time.Tick channel will never return - // ok == false, so any branching on the value of ok can be - // replaced with an unconditional jump. This will primarily match - // `for range time.Tick(x)` loops, but it can also match - // user-written code. - for _, block := range fn.Blocks { - if len(block.Instrs) < 3 { - continue - } - if len(block.Succs) != 2 { - continue - } - var instrs []*ssa.Instruction - for i, ins := range block.Instrs { - if _, ok := ins.(*ssa.DebugRef); ok { - continue - } - instrs = append(instrs, &block.Instrs[i]) - } - - for i, ins := range instrs { - unop, ok := (*ins).(*ssa.UnOp) - if !ok || unop.Op != token.ARROW { - continue - } - call, ok := unop.X.(*ssa.Call) - if !ok { - continue - } - if !IsCallTo(call.Common(), "time.Tick") { - continue - } - ex, ok := (*instrs[i+1]).(*ssa.Extract) - if !ok || ex.Tuple != unop || ex.Index != 1 { - continue - } - - ifstmt, ok := (*instrs[i+2]).(*ssa.If) - if !ok || ifstmt.Cond != ex { - continue - } - - *instrs[i+2] = ssa.NewJump(block) - succ := block.Succs[1] - block.Succs = block.Succs[0:1] - succ.RemovePred(block) - } - } -} - -func hasType(j *lint.Job, expr ast.Expr, name string) bool { - T := TypeOf(j, expr) - return IsType(T, name) -} - -func (c *Checker) CheckUntrappableSignal(j *lint.Job) { - fn := func(node ast.Node) bool { - call, ok := node.(*ast.CallExpr) - if !ok { - return true - } - if !IsCallToAnyAST(j, call, - "os/signal.Ignore", "os/signal.Notify", "os/signal.Reset") { - return true - } - for _, arg := range call.Args { - if conv, ok := arg.(*ast.CallExpr); ok && isName(j, conv.Fun, "os.Signal") { - arg = conv.Args[0] - } - - if isName(j, arg, "os.Kill") || isName(j, arg, "syscall.SIGKILL") { - j.Errorf(arg, "%s cannot be trapped (did you mean syscall.SIGTERM?)", Render(j, arg)) - } - if isName(j, arg, "syscall.SIGSTOP") { - j.Errorf(arg, "%s signal cannot be trapped", Render(j, arg)) - } - } - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) CheckTemplate(j *lint.Job) { - fn := func(node ast.Node) bool { - call, ok := node.(*ast.CallExpr) - if !ok { - return true - } - var kind string - if IsCallToAST(j, call, "(*text/template.Template).Parse") { - kind = "text" - } else if IsCallToAST(j, call, "(*html/template.Template).Parse") { - kind = "html" - } else { - return true - } - sel := call.Fun.(*ast.SelectorExpr) - if !IsCallToAST(j, sel.X, "text/template.New") && - !IsCallToAST(j, sel.X, "html/template.New") { - // TODO(dh): this is a cheap workaround for templates with - // different delims. A better solution with less false - // negatives would use data flow analysis to see where the - // template comes from and where it has been - return true - } - s, ok := ExprToString(j, call.Args[Arg("(*text/template.Template).Parse.text")]) - if !ok { - return true - } - var err error - switch kind { - case "text": - _, err = texttemplate.New("").Parse(s) - case "html": - _, err = htmltemplate.New("").Parse(s) - } - if err != nil { - // TODO(dominikh): whitelist other parse errors, if any - if strings.Contains(err.Error(), "unexpected") { - j.Errorf(call.Args[Arg("(*text/template.Template).Parse.text")], "%s", err) - } - } - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) CheckTimeSleepConstant(j *lint.Job) { - fn := func(node ast.Node) bool { - call, ok := node.(*ast.CallExpr) - if !ok { - return true - } - if !IsCallToAST(j, call, "time.Sleep") { - return true - } - lit, ok := call.Args[Arg("time.Sleep.d")].(*ast.BasicLit) - if !ok { - return true - } - n, err := strconv.Atoi(lit.Value) - if err != nil { - return true - } - if n == 0 || n > 120 { - // time.Sleep(0) is a seldom used pattern in concurrency - // tests. >120 might be intentional. 120 was chosen - // because the user could've meant 2 minutes. - return true - } - recommendation := "time.Sleep(time.Nanosecond)" - if n != 1 { - recommendation = fmt.Sprintf("time.Sleep(%d * time.Nanosecond)", n) - } - j.Errorf(call.Args[Arg("time.Sleep.d")], - "sleeping for %d nanoseconds is probably a bug. Be explicit if it isn't: %s", n, recommendation) - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) CheckWaitgroupAdd(j *lint.Job) { - fn := func(node ast.Node) bool { - g, ok := node.(*ast.GoStmt) - if !ok { - return true - } - fun, ok := g.Call.Fun.(*ast.FuncLit) - if !ok { - return true - } - if len(fun.Body.List) == 0 { - return true - } - stmt, ok := fun.Body.List[0].(*ast.ExprStmt) - if !ok { - return true - } - call, ok := stmt.X.(*ast.CallExpr) - if !ok { - return true - } - sel, ok := call.Fun.(*ast.SelectorExpr) - if !ok { - return true - } - fn, ok := ObjectOf(j, sel.Sel).(*types.Func) - if !ok { - return true - } - if fn.FullName() == "(*sync.WaitGroup).Add" { - j.Errorf(sel, "should call %s before starting the goroutine to avoid a race", - Render(j, stmt)) - } - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) CheckInfiniteEmptyLoop(j *lint.Job) { - fn := func(node ast.Node) bool { - loop, ok := node.(*ast.ForStmt) - if !ok || len(loop.Body.List) != 0 || loop.Post != nil { - return true - } - - if loop.Init != nil { - // TODO(dh): this isn't strictly necessary, it just makes - // the check easier. - return true - } - // An empty loop is bad news in two cases: 1) The loop has no - // condition. In that case, it's just a loop that spins - // forever and as fast as it can, keeping a core busy. 2) The - // loop condition only consists of variable or field reads and - // operators on those. The only way those could change their - // value is with unsynchronised access, which constitutes a - // data race. - // - // If the condition contains any function calls, its behaviour - // is dynamic and the loop might terminate. Similarly for - // channel receives. - - if loop.Cond != nil && hasSideEffects(loop.Cond) { - return true - } - - j.Errorf(loop, "this loop will spin, using 100%% CPU") - if loop.Cond != nil { - j.Errorf(loop, "loop condition never changes or has a race condition") - } - - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) CheckDeferInInfiniteLoop(j *lint.Job) { - fn := func(node ast.Node) bool { - mightExit := false - var defers []ast.Stmt - loop, ok := node.(*ast.ForStmt) - if !ok || loop.Cond != nil { - return true - } - fn2 := func(node ast.Node) bool { - switch stmt := node.(type) { - case *ast.ReturnStmt: - mightExit = true - case *ast.BranchStmt: - // TODO(dominikh): if this sees a break in a switch or - // select, it doesn't check if it breaks the loop or - // just the select/switch. This causes some false - // negatives. - if stmt.Tok == token.BREAK { - mightExit = true - } - case *ast.DeferStmt: - defers = append(defers, stmt) - case *ast.FuncLit: - // Don't look into function bodies - return false - } - return true - } - ast.Inspect(loop.Body, fn2) - if mightExit { - return true - } - for _, stmt := range defers { - j.Errorf(stmt, "defers in this infinite loop will never run") - } - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) CheckDubiousDeferInChannelRangeLoop(j *lint.Job) { - fn := func(node ast.Node) bool { - loop, ok := node.(*ast.RangeStmt) - if !ok { - return true - } - typ := TypeOf(j, loop.X) - _, ok = typ.Underlying().(*types.Chan) - if !ok { - return true - } - fn2 := func(node ast.Node) bool { - switch stmt := node.(type) { - case *ast.DeferStmt: - j.Errorf(stmt, "defers in this range loop won't run unless the channel gets closed") - case *ast.FuncLit: - // Don't look into function bodies - return false - } - return true - } - ast.Inspect(loop.Body, fn2) - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) CheckTestMainExit(j *lint.Job) { - fn := func(node ast.Node) bool { - if !isTestMain(j, node) { - return true - } - - arg := ObjectOf(j, node.(*ast.FuncDecl).Type.Params.List[0].Names[0]) - callsRun := false - fn2 := func(node ast.Node) bool { - call, ok := node.(*ast.CallExpr) - if !ok { - return true - } - sel, ok := call.Fun.(*ast.SelectorExpr) - if !ok { - return true - } - ident, ok := sel.X.(*ast.Ident) - if !ok { - return true - } - if arg != ObjectOf(j, ident) { - return true - } - if sel.Sel.Name == "Run" { - callsRun = true - return false - } - return true - } - ast.Inspect(node.(*ast.FuncDecl).Body, fn2) - - callsExit := false - fn3 := func(node ast.Node) bool { - if IsCallToAST(j, node, "os.Exit") { - callsExit = true - return false - } - return true - } - ast.Inspect(node.(*ast.FuncDecl).Body, fn3) - if !callsExit && callsRun { - j.Errorf(node, "TestMain should call os.Exit to set exit code") - } - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func isTestMain(j *lint.Job, node ast.Node) bool { - decl, ok := node.(*ast.FuncDecl) - if !ok { - return false - } - if decl.Name.Name != "TestMain" { - return false - } - if len(decl.Type.Params.List) != 1 { - return false - } - arg := decl.Type.Params.List[0] - if len(arg.Names) != 1 { - return false - } - return IsOfType(j, arg.Type, "*testing.M") -} - -func (c *Checker) CheckExec(j *lint.Job) { - fn := func(node ast.Node) bool { - call, ok := node.(*ast.CallExpr) - if !ok { - return true - } - if !IsCallToAST(j, call, "os/exec.Command") { - return true - } - val, ok := ExprToString(j, call.Args[Arg("os/exec.Command.name")]) - if !ok { - return true - } - if !strings.Contains(val, " ") || strings.Contains(val, `\`) || strings.Contains(val, "/") { - return true - } - j.Errorf(call.Args[Arg("os/exec.Command.name")], - "first argument to exec.Command looks like a shell command, but a program name or path are expected") - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) CheckLoopEmptyDefault(j *lint.Job) { - fn := func(node ast.Node) bool { - loop, ok := node.(*ast.ForStmt) - if !ok || len(loop.Body.List) != 1 || loop.Cond != nil || loop.Init != nil { - return true - } - sel, ok := loop.Body.List[0].(*ast.SelectStmt) - if !ok { - return true - } - for _, c := range sel.Body.List { - if comm, ok := c.(*ast.CommClause); ok && comm.Comm == nil && len(comm.Body) == 0 { - j.Errorf(comm, "should not have an empty default case in a for+select loop. The loop will spin.") - } - } - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) CheckLhsRhsIdentical(j *lint.Job) { - fn := func(node ast.Node) bool { - op, ok := node.(*ast.BinaryExpr) - if !ok { - return true - } - switch op.Op { - case token.EQL, token.NEQ: - if basic, ok := TypeOf(j, op.X).(*types.Basic); ok { - if kind := basic.Kind(); kind == types.Float32 || kind == types.Float64 { - // f == f and f != f might be used to check for NaN - return true - } - } - case token.SUB, token.QUO, token.AND, token.REM, token.OR, token.XOR, token.AND_NOT, - token.LAND, token.LOR, token.LSS, token.GTR, token.LEQ, token.GEQ: - default: - // For some ops, such as + and *, it can make sense to - // have identical operands - return true - } - - if Render(j, op.X) != Render(j, op.Y) { - return true - } - j.Errorf(op, "identical expressions on the left and right side of the '%s' operator", op.Op) - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) CheckScopedBreak(j *lint.Job) { - fn := func(node ast.Node) bool { - var body *ast.BlockStmt - switch node := node.(type) { - case *ast.ForStmt: - body = node.Body - case *ast.RangeStmt: - body = node.Body - default: - return true - } - for _, stmt := range body.List { - var blocks [][]ast.Stmt - switch stmt := stmt.(type) { - case *ast.SwitchStmt: - for _, c := range stmt.Body.List { - blocks = append(blocks, c.(*ast.CaseClause).Body) - } - case *ast.SelectStmt: - for _, c := range stmt.Body.List { - blocks = append(blocks, c.(*ast.CommClause).Body) - } - default: - continue - } - - for _, body := range blocks { - if len(body) == 0 { - continue - } - lasts := []ast.Stmt{body[len(body)-1]} - // TODO(dh): unfold all levels of nested block - // statements, not just a single level if statement - if ifs, ok := lasts[0].(*ast.IfStmt); ok { - if len(ifs.Body.List) == 0 { - continue - } - lasts[0] = ifs.Body.List[len(ifs.Body.List)-1] - - if block, ok := ifs.Else.(*ast.BlockStmt); ok { - if len(block.List) != 0 { - lasts = append(lasts, block.List[len(block.List)-1]) - } - } - } - for _, last := range lasts { - branch, ok := last.(*ast.BranchStmt) - if !ok || branch.Tok != token.BREAK || branch.Label != nil { - continue - } - j.Errorf(branch, "ineffective break statement. Did you mean to break out of the outer loop?") - } - } - } - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) CheckUnsafePrintf(j *lint.Job) { - fn := func(node ast.Node) bool { - call, ok := node.(*ast.CallExpr) - if !ok { - return true - } - if !IsCallToAnyAST(j, call, "fmt.Printf", "fmt.Sprintf", "log.Printf") { - return true - } - if len(call.Args) != 1 { - return true - } - switch call.Args[Arg("fmt.Printf.format")].(type) { - case *ast.CallExpr, *ast.Ident: - default: - return true - } - j.Errorf(call.Args[Arg("fmt.Printf.format")], - "printf-style function with dynamic first argument and no further arguments should use print-style function instead") - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) CheckEarlyDefer(j *lint.Job) { - fn := func(node ast.Node) bool { - block, ok := node.(*ast.BlockStmt) - if !ok { - return true - } - if len(block.List) < 2 { - return true - } - for i, stmt := range block.List { - if i == len(block.List)-1 { - break - } - assign, ok := stmt.(*ast.AssignStmt) - if !ok { - continue - } - if len(assign.Rhs) != 1 { - continue - } - if len(assign.Lhs) < 2 { - continue - } - if lhs, ok := assign.Lhs[len(assign.Lhs)-1].(*ast.Ident); ok && lhs.Name == "_" { - continue - } - call, ok := assign.Rhs[0].(*ast.CallExpr) - if !ok { - continue - } - sig, ok := TypeOf(j, call.Fun).(*types.Signature) - if !ok { - continue - } - if sig.Results().Len() < 2 { - continue - } - last := sig.Results().At(sig.Results().Len() - 1) - // FIXME(dh): check that it's error from universe, not - // another type of the same name - if last.Type().String() != "error" { - continue - } - lhs, ok := assign.Lhs[0].(*ast.Ident) - if !ok { - continue - } - def, ok := block.List[i+1].(*ast.DeferStmt) - if !ok { - continue - } - sel, ok := def.Call.Fun.(*ast.SelectorExpr) - if !ok { - continue - } - ident, ok := selectorX(sel).(*ast.Ident) - if !ok { - continue - } - if ident.Obj != lhs.Obj { - continue - } - if sel.Sel.Name != "Close" { - continue - } - j.Errorf(def, "should check returned error before deferring %s", Render(j, def.Call)) - } - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func selectorX(sel *ast.SelectorExpr) ast.Node { - switch x := sel.X.(type) { - case *ast.SelectorExpr: - return selectorX(x) - default: - return x - } -} - -func (c *Checker) CheckEmptyCriticalSection(j *lint.Job) { - // Initially it might seem like this check would be easier to - // implement in SSA. After all, we're only checking for two - // consecutive method calls. In reality, however, there may be any - // number of other instructions between the lock and unlock, while - // still constituting an empty critical section. For example, - // given `m.x().Lock(); m.x().Unlock()`, there will be a call to - // x(). In the AST-based approach, this has a tiny potential for a - // false positive (the second call to x might be doing work that - // is protected by the mutex). In an SSA-based approach, however, - // it would miss a lot of real bugs. - - mutexParams := func(s ast.Stmt) (x ast.Expr, funcName string, ok bool) { - expr, ok := s.(*ast.ExprStmt) - if !ok { - return nil, "", false - } - call, ok := expr.X.(*ast.CallExpr) - if !ok { - return nil, "", false - } - sel, ok := call.Fun.(*ast.SelectorExpr) - if !ok { - return nil, "", false - } - - fn, ok := ObjectOf(j, sel.Sel).(*types.Func) - if !ok { - return nil, "", false - } - sig := fn.Type().(*types.Signature) - if sig.Params().Len() != 0 || sig.Results().Len() != 0 { - return nil, "", false - } - - return sel.X, fn.Name(), true - } - - fn := func(node ast.Node) bool { - block, ok := node.(*ast.BlockStmt) - if !ok { - return true - } - if len(block.List) < 2 { - return true - } - for i := range block.List[:len(block.List)-1] { - sel1, method1, ok1 := mutexParams(block.List[i]) - sel2, method2, ok2 := mutexParams(block.List[i+1]) - - if !ok1 || !ok2 || Render(j, sel1) != Render(j, sel2) { - continue - } - if (method1 == "Lock" && method2 == "Unlock") || - (method1 == "RLock" && method2 == "RUnlock") { - j.Errorf(block.List[i+1], "empty critical section") - } - } - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -// cgo produces code like fn(&*_Cvar_kSomeCallbacks) which we don't -// want to flag. -var cgoIdent = regexp.MustCompile(`^_C(func|var)_.+$`) - -func (c *Checker) CheckIneffectiveCopy(j *lint.Job) { - fn := func(node ast.Node) bool { - if unary, ok := node.(*ast.UnaryExpr); ok { - if star, ok := unary.X.(*ast.StarExpr); ok && unary.Op == token.AND { - ident, ok := star.X.(*ast.Ident) - if !ok || !cgoIdent.MatchString(ident.Name) { - j.Errorf(unary, "&*x will be simplified to x. It will not copy x.") - } - } - } - - if star, ok := node.(*ast.StarExpr); ok { - if unary, ok := star.X.(*ast.UnaryExpr); ok && unary.Op == token.AND { - j.Errorf(star, "*&x will be simplified to x. It will not copy x.") - } - } - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) CheckDiffSizeComparison(j *lint.Job) { - for _, ssafn := range j.Program.InitialFunctions { - for _, b := range ssafn.Blocks { - for _, ins := range b.Instrs { - binop, ok := ins.(*ssa.BinOp) - if !ok { - continue - } - if binop.Op != token.EQL && binop.Op != token.NEQ { - continue - } - _, ok1 := binop.X.(*ssa.Slice) - _, ok2 := binop.Y.(*ssa.Slice) - if !ok1 && !ok2 { - continue - } - r := c.funcDescs.Get(ssafn).Ranges - r1, ok1 := r.Get(binop.X).(vrp.StringInterval) - r2, ok2 := r.Get(binop.Y).(vrp.StringInterval) - if !ok1 || !ok2 { - continue - } - if r1.Length.Intersection(r2.Length).Empty() { - j.Errorf(binop, "comparing strings of different sizes for equality will always return false") - } - } - } - } -} - -func (c *Checker) CheckCanonicalHeaderKey(j *lint.Job) { - fn := func(node ast.Node) bool { - assign, ok := node.(*ast.AssignStmt) - if ok { - // TODO(dh): This risks missing some Header reads, for - // example in `h1["foo"] = h2["foo"]` – these edge - // cases are probably rare enough to ignore for now. - for _, expr := range assign.Lhs { - op, ok := expr.(*ast.IndexExpr) - if !ok { - continue - } - if hasType(j, op.X, "net/http.Header") { - return false - } - } - return true - } - op, ok := node.(*ast.IndexExpr) - if !ok { - return true - } - if !hasType(j, op.X, "net/http.Header") { - return true - } - s, ok := ExprToString(j, op.Index) - if !ok { - return true - } - if s == http.CanonicalHeaderKey(s) { - return true - } - j.Errorf(op, "keys in http.Header are canonicalized, %q is not canonical; fix the constant or use http.CanonicalHeaderKey", s) - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) CheckBenchmarkN(j *lint.Job) { - fn := func(node ast.Node) bool { - assign, ok := node.(*ast.AssignStmt) - if !ok { - return true - } - if len(assign.Lhs) != 1 || len(assign.Rhs) != 1 { - return true - } - sel, ok := assign.Lhs[0].(*ast.SelectorExpr) - if !ok { - return true - } - if sel.Sel.Name != "N" { - return true - } - if !hasType(j, sel.X, "*testing.B") { - return true - } - j.Errorf(assign, "should not assign to %s", Render(j, sel)) - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) CheckUnreadVariableValues(j *lint.Job) { - for _, ssafn := range j.Program.InitialFunctions { - if IsExample(ssafn) { - continue - } - node := ssafn.Syntax() - if node == nil { - continue - } - - ast.Inspect(node, func(node ast.Node) bool { - assign, ok := node.(*ast.AssignStmt) - if !ok { - return true - } - if len(assign.Lhs) > 1 && len(assign.Rhs) == 1 { - // Either a function call with multiple return values, - // or a comma-ok assignment - - val, _ := ssafn.ValueForExpr(assign.Rhs[0]) - if val == nil { - return true - } - refs := val.Referrers() - if refs == nil { - return true - } - for _, ref := range *refs { - ex, ok := ref.(*ssa.Extract) - if !ok { - continue - } - exrefs := ex.Referrers() - if exrefs == nil { - continue - } - if len(FilterDebug(*exrefs)) == 0 { - lhs := assign.Lhs[ex.Index] - if ident, ok := lhs.(*ast.Ident); !ok || ok && ident.Name == "_" { - continue - } - j.Errorf(lhs, "this value of %s is never used", lhs) - } - } - return true - } - for i, lhs := range assign.Lhs { - rhs := assign.Rhs[i] - if ident, ok := lhs.(*ast.Ident); !ok || ok && ident.Name == "_" { - continue - } - val, _ := ssafn.ValueForExpr(rhs) - if val == nil { - continue - } - - refs := val.Referrers() - if refs == nil { - // TODO investigate why refs can be nil - return true - } - if len(FilterDebug(*refs)) == 0 { - j.Errorf(lhs, "this value of %s is never used", lhs) - } - } - return true - }) - } -} - -func (c *Checker) CheckPredeterminedBooleanExprs(j *lint.Job) { - for _, ssafn := range j.Program.InitialFunctions { - for _, block := range ssafn.Blocks { - for _, ins := range block.Instrs { - ssabinop, ok := ins.(*ssa.BinOp) - if !ok { - continue - } - switch ssabinop.Op { - case token.GTR, token.LSS, token.EQL, token.NEQ, token.LEQ, token.GEQ: - default: - continue - } - - xs, ok1 := consts(ssabinop.X, nil, nil) - ys, ok2 := consts(ssabinop.Y, nil, nil) - if !ok1 || !ok2 || len(xs) == 0 || len(ys) == 0 { - continue - } - - trues := 0 - for _, x := range xs { - for _, y := range ys { - if x.Value == nil { - if y.Value == nil { - trues++ - } - continue - } - if constant.Compare(x.Value, ssabinop.Op, y.Value) { - trues++ - } - } - } - b := trues != 0 - if trues == 0 || trues == len(xs)*len(ys) { - j.Errorf(ssabinop, "binary expression is always %t for all possible values (%s %s %s)", - b, xs, ssabinop.Op, ys) - } - } - } - } -} - -func (c *Checker) CheckNilMaps(j *lint.Job) { - for _, ssafn := range j.Program.InitialFunctions { - for _, block := range ssafn.Blocks { - for _, ins := range block.Instrs { - mu, ok := ins.(*ssa.MapUpdate) - if !ok { - continue - } - c, ok := mu.Map.(*ssa.Const) - if !ok { - continue - } - if c.Value != nil { - continue - } - j.Errorf(mu, "assignment to nil map") - } - } - } -} - -func (c *Checker) CheckUnsignedComparison(j *lint.Job) { - fn := func(node ast.Node) bool { - expr, ok := node.(*ast.BinaryExpr) - if !ok { - return true - } - tx := TypeOf(j, expr.X) - basic, ok := tx.Underlying().(*types.Basic) - if !ok { - return true - } - if (basic.Info() & types.IsUnsigned) == 0 { - return true - } - lit, ok := expr.Y.(*ast.BasicLit) - if !ok || lit.Value != "0" { - return true - } - switch expr.Op { - case token.GEQ: - j.Errorf(expr, "unsigned values are always >= 0") - case token.LSS: - j.Errorf(expr, "unsigned values are never < 0") - } - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func consts(val ssa.Value, out []*ssa.Const, visitedPhis map[string]bool) ([]*ssa.Const, bool) { - if visitedPhis == nil { - visitedPhis = map[string]bool{} - } - var ok bool - switch val := val.(type) { - case *ssa.Phi: - if visitedPhis[val.Name()] { - break - } - visitedPhis[val.Name()] = true - vals := val.Operands(nil) - for _, phival := range vals { - out, ok = consts(*phival, out, visitedPhis) - if !ok { - return nil, false - } - } - case *ssa.Const: - out = append(out, val) - case *ssa.Convert: - out, ok = consts(val.X, out, visitedPhis) - if !ok { - return nil, false - } - default: - return nil, false - } - if len(out) < 2 { - return out, true - } - uniq := []*ssa.Const{out[0]} - for _, val := range out[1:] { - if val.Value == uniq[len(uniq)-1].Value { - continue - } - uniq = append(uniq, val) - } - return uniq, true -} - -func (c *Checker) CheckLoopCondition(j *lint.Job) { - for _, ssafn := range j.Program.InitialFunctions { - fn := func(node ast.Node) bool { - loop, ok := node.(*ast.ForStmt) - if !ok { - return true - } - if loop.Init == nil || loop.Cond == nil || loop.Post == nil { - return true - } - init, ok := loop.Init.(*ast.AssignStmt) - if !ok || len(init.Lhs) != 1 || len(init.Rhs) != 1 { - return true - } - cond, ok := loop.Cond.(*ast.BinaryExpr) - if !ok { - return true - } - x, ok := cond.X.(*ast.Ident) - if !ok { - return true - } - lhs, ok := init.Lhs[0].(*ast.Ident) - if !ok { - return true - } - if x.Obj != lhs.Obj { - return true - } - if _, ok := loop.Post.(*ast.IncDecStmt); !ok { - return true - } - - v, isAddr := ssafn.ValueForExpr(cond.X) - if v == nil || isAddr { - return true - } - switch v := v.(type) { - case *ssa.Phi: - ops := v.Operands(nil) - if len(ops) != 2 { - return true - } - _, ok := (*ops[0]).(*ssa.Const) - if !ok { - return true - } - sigma, ok := (*ops[1]).(*ssa.Sigma) - if !ok { - return true - } - if sigma.X != v { - return true - } - case *ssa.UnOp: - return true - } - j.Errorf(cond, "variable in loop condition never changes") - - return true - } - Inspect(ssafn.Syntax(), fn) - } -} - -func (c *Checker) CheckArgOverwritten(j *lint.Job) { - for _, ssafn := range j.Program.InitialFunctions { - fn := func(node ast.Node) bool { - var typ *ast.FuncType - var body *ast.BlockStmt - switch fn := node.(type) { - case *ast.FuncDecl: - typ = fn.Type - body = fn.Body - case *ast.FuncLit: - typ = fn.Type - body = fn.Body - } - if body == nil { - return true - } - if len(typ.Params.List) == 0 { - return true - } - for _, field := range typ.Params.List { - for _, arg := range field.Names { - obj := ObjectOf(j, arg) - var ssaobj *ssa.Parameter - for _, param := range ssafn.Params { - if param.Object() == obj { - ssaobj = param - break - } - } - if ssaobj == nil { - continue - } - refs := ssaobj.Referrers() - if refs == nil { - continue - } - if len(FilterDebug(*refs)) != 0 { - continue - } - - assigned := false - ast.Inspect(body, func(node ast.Node) bool { - assign, ok := node.(*ast.AssignStmt) - if !ok { - return true - } - for _, lhs := range assign.Lhs { - ident, ok := lhs.(*ast.Ident) - if !ok { - continue - } - if ObjectOf(j, ident) == obj { - assigned = true - return false - } - } - return true - }) - if assigned { - j.Errorf(arg, "argument %s is overwritten before first use", arg) - } - } - } - return true - } - Inspect(ssafn.Syntax(), fn) - } -} - -func (c *Checker) CheckIneffectiveLoop(j *lint.Job) { - // This check detects some, but not all unconditional loop exits. - // We give up in the following cases: - // - // - a goto anywhere in the loop. The goto might skip over our - // return, and we don't check that it doesn't. - // - // - any nested, unlabelled continue, even if it is in another - // loop or closure. - fn := func(node ast.Node) bool { - var body *ast.BlockStmt - switch fn := node.(type) { - case *ast.FuncDecl: - body = fn.Body - case *ast.FuncLit: - body = fn.Body - default: - return true - } - if body == nil { - return true - } - labels := map[*ast.Object]ast.Stmt{} - ast.Inspect(body, func(node ast.Node) bool { - label, ok := node.(*ast.LabeledStmt) - if !ok { - return true - } - labels[label.Label.Obj] = label.Stmt - return true - }) - - ast.Inspect(body, func(node ast.Node) bool { - var loop ast.Node - var body *ast.BlockStmt - switch node := node.(type) { - case *ast.ForStmt: - body = node.Body - loop = node - case *ast.RangeStmt: - typ := TypeOf(j, node.X) - if _, ok := typ.Underlying().(*types.Map); ok { - // looping once over a map is a valid pattern for - // getting an arbitrary element. - return true - } - body = node.Body - loop = node - default: - return true - } - if len(body.List) < 2 { - // avoid flagging the somewhat common pattern of using - // a range loop to get the first element in a slice, - // or the first rune in a string. - return true - } - var unconditionalExit ast.Node - hasBranching := false - for _, stmt := range body.List { - switch stmt := stmt.(type) { - case *ast.BranchStmt: - switch stmt.Tok { - case token.BREAK: - if stmt.Label == nil || labels[stmt.Label.Obj] == loop { - unconditionalExit = stmt - } - case token.CONTINUE: - if stmt.Label == nil || labels[stmt.Label.Obj] == loop { - unconditionalExit = nil - return false - } - } - case *ast.ReturnStmt: - unconditionalExit = stmt - case *ast.IfStmt, *ast.ForStmt, *ast.RangeStmt, *ast.SwitchStmt, *ast.SelectStmt: - hasBranching = true - } - } - if unconditionalExit == nil || !hasBranching { - return false - } - ast.Inspect(body, func(node ast.Node) bool { - if branch, ok := node.(*ast.BranchStmt); ok { - - switch branch.Tok { - case token.GOTO: - unconditionalExit = nil - return false - case token.CONTINUE: - if branch.Label != nil && labels[branch.Label.Obj] != loop { - return true - } - unconditionalExit = nil - return false - } - } - return true - }) - if unconditionalExit != nil { - j.Errorf(unconditionalExit, "the surrounding loop is unconditionally terminated") - } - return true - }) - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) CheckNilContext(j *lint.Job) { - fn := func(node ast.Node) bool { - call, ok := node.(*ast.CallExpr) - if !ok { - return true - } - if len(call.Args) == 0 { - return true - } - if typ, ok := TypeOf(j, call.Args[0]).(*types.Basic); !ok || typ.Kind() != types.UntypedNil { - return true - } - sig, ok := TypeOf(j, call.Fun).(*types.Signature) - if !ok { - return true - } - if sig.Params().Len() == 0 { - return true - } - if !IsType(sig.Params().At(0).Type(), "context.Context") { - return true - } - j.Errorf(call.Args[0], - "do not pass a nil Context, even if a function permits it; pass context.TODO if you are unsure about which Context to use") - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) CheckSeeker(j *lint.Job) { - fn := func(node ast.Node) bool { - call, ok := node.(*ast.CallExpr) - if !ok { - return true - } - sel, ok := call.Fun.(*ast.SelectorExpr) - if !ok { - return true - } - if sel.Sel.Name != "Seek" { - return true - } - if len(call.Args) != 2 { - return true - } - arg0, ok := call.Args[Arg("(io.Seeker).Seek.offset")].(*ast.SelectorExpr) - if !ok { - return true - } - switch arg0.Sel.Name { - case "SeekStart", "SeekCurrent", "SeekEnd": - default: - return true - } - pkg, ok := arg0.X.(*ast.Ident) - if !ok { - return true - } - if pkg.Name != "io" { - return true - } - j.Errorf(call, "the first argument of io.Seeker is the offset, but an io.Seek* constant is being used instead") - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) CheckIneffectiveAppend(j *lint.Job) { - isAppend := func(ins ssa.Value) bool { - call, ok := ins.(*ssa.Call) - if !ok { - return false - } - if call.Call.IsInvoke() { - return false - } - if builtin, ok := call.Call.Value.(*ssa.Builtin); !ok || builtin.Name() != "append" { - return false - } - return true - } - - for _, ssafn := range j.Program.InitialFunctions { - for _, block := range ssafn.Blocks { - for _, ins := range block.Instrs { - val, ok := ins.(ssa.Value) - if !ok || !isAppend(val) { - continue - } - - isUsed := false - visited := map[ssa.Instruction]bool{} - var walkRefs func(refs []ssa.Instruction) - walkRefs = func(refs []ssa.Instruction) { - loop: - for _, ref := range refs { - if visited[ref] { - continue - } - visited[ref] = true - if _, ok := ref.(*ssa.DebugRef); ok { - continue - } - switch ref := ref.(type) { - case *ssa.Phi: - walkRefs(*ref.Referrers()) - case *ssa.Sigma: - walkRefs(*ref.Referrers()) - case ssa.Value: - if !isAppend(ref) { - isUsed = true - } else { - walkRefs(*ref.Referrers()) - } - case ssa.Instruction: - isUsed = true - break loop - } - } - } - refs := val.Referrers() - if refs == nil { - continue - } - walkRefs(*refs) - if !isUsed { - j.Errorf(ins, "this result of append is never used, except maybe in other appends") - } - } - } - } -} - -func (c *Checker) CheckConcurrentTesting(j *lint.Job) { - for _, ssafn := range j.Program.InitialFunctions { - for _, block := range ssafn.Blocks { - for _, ins := range block.Instrs { - gostmt, ok := ins.(*ssa.Go) - if !ok { - continue - } - var fn *ssa.Function - switch val := gostmt.Call.Value.(type) { - case *ssa.Function: - fn = val - case *ssa.MakeClosure: - fn = val.Fn.(*ssa.Function) - default: - continue - } - if fn.Blocks == nil { - continue - } - for _, block := range fn.Blocks { - for _, ins := range block.Instrs { - call, ok := ins.(*ssa.Call) - if !ok { - continue - } - if call.Call.IsInvoke() { - continue - } - callee := call.Call.StaticCallee() - if callee == nil { - continue - } - recv := callee.Signature.Recv() - if recv == nil { - continue - } - if !IsType(recv.Type(), "*testing.common") { - continue - } - fn, ok := call.Call.StaticCallee().Object().(*types.Func) - if !ok { - continue - } - name := fn.Name() - switch name { - case "FailNow", "Fatal", "Fatalf", "SkipNow", "Skip", "Skipf": - default: - continue - } - j.Errorf(gostmt, "the goroutine calls T.%s, which must be called in the same goroutine as the test", name) - } - } - } - } - } -} - -func (c *Checker) CheckCyclicFinalizer(j *lint.Job) { - for _, ssafn := range j.Program.InitialFunctions { - node := c.funcDescs.CallGraph.CreateNode(ssafn) - for _, edge := range node.Out { - if edge.Callee.Func.RelString(nil) != "runtime.SetFinalizer" { - continue - } - arg0 := edge.Site.Common().Args[Arg("runtime.SetFinalizer.obj")] - if iface, ok := arg0.(*ssa.MakeInterface); ok { - arg0 = iface.X - } - unop, ok := arg0.(*ssa.UnOp) - if !ok { - continue - } - v, ok := unop.X.(*ssa.Alloc) - if !ok { - continue - } - arg1 := edge.Site.Common().Args[Arg("runtime.SetFinalizer.finalizer")] - if iface, ok := arg1.(*ssa.MakeInterface); ok { - arg1 = iface.X - } - mc, ok := arg1.(*ssa.MakeClosure) - if !ok { - continue - } - for _, b := range mc.Bindings { - if b == v { - pos := j.Program.DisplayPosition(mc.Fn.Pos()) - j.Errorf(edge.Site, "the finalizer closes over the object, preventing the finalizer from ever running (at %s)", pos) - } - } - } - } -} - -func (c *Checker) CheckSliceOutOfBounds(j *lint.Job) { - for _, ssafn := range j.Program.InitialFunctions { - for _, block := range ssafn.Blocks { - for _, ins := range block.Instrs { - ia, ok := ins.(*ssa.IndexAddr) - if !ok { - continue - } - if _, ok := ia.X.Type().Underlying().(*types.Slice); !ok { - continue - } - sr, ok1 := c.funcDescs.Get(ssafn).Ranges[ia.X].(vrp.SliceInterval) - idxr, ok2 := c.funcDescs.Get(ssafn).Ranges[ia.Index].(vrp.IntInterval) - if !ok1 || !ok2 || !sr.IsKnown() || !idxr.IsKnown() || sr.Length.Empty() || idxr.Empty() { - continue - } - if idxr.Lower.Cmp(sr.Length.Upper) >= 0 { - j.Errorf(ia, "index out of bounds") - } - } - } - } -} - -func (c *Checker) CheckDeferLock(j *lint.Job) { - for _, ssafn := range j.Program.InitialFunctions { - for _, block := range ssafn.Blocks { - instrs := FilterDebug(block.Instrs) - if len(instrs) < 2 { - continue - } - for i, ins := range instrs[:len(instrs)-1] { - call, ok := ins.(*ssa.Call) - if !ok { - continue - } - if !IsCallTo(call.Common(), "(*sync.Mutex).Lock") && !IsCallTo(call.Common(), "(*sync.RWMutex).RLock") { - continue - } - nins, ok := instrs[i+1].(*ssa.Defer) - if !ok { - continue - } - if !IsCallTo(&nins.Call, "(*sync.Mutex).Lock") && !IsCallTo(&nins.Call, "(*sync.RWMutex).RLock") { - continue - } - if call.Common().Args[0] != nins.Call.Args[0] { - continue - } - name := shortCallName(call.Common()) - alt := "" - switch name { - case "Lock": - alt = "Unlock" - case "RLock": - alt = "RUnlock" - } - j.Errorf(nins, "deferring %s right after having locked already; did you mean to defer %s?", name, alt) - } - } - } -} - -func (c *Checker) CheckNaNComparison(j *lint.Job) { - isNaN := func(v ssa.Value) bool { - call, ok := v.(*ssa.Call) - if !ok { - return false - } - return IsCallTo(call.Common(), "math.NaN") - } - for _, ssafn := range j.Program.InitialFunctions { - for _, block := range ssafn.Blocks { - for _, ins := range block.Instrs { - ins, ok := ins.(*ssa.BinOp) - if !ok { - continue - } - if isNaN(ins.X) || isNaN(ins.Y) { - j.Errorf(ins, "no value is equal to NaN, not even NaN itself") - } - } - } - } -} - -func (c *Checker) CheckInfiniteRecursion(j *lint.Job) { - for _, ssafn := range j.Program.InitialFunctions { - node := c.funcDescs.CallGraph.CreateNode(ssafn) - for _, edge := range node.Out { - if edge.Callee != node { - continue - } - if _, ok := edge.Site.(*ssa.Go); ok { - // Recursively spawning goroutines doesn't consume - // stack space infinitely, so don't flag it. - continue - } - - block := edge.Site.Block() - canReturn := false - for _, b := range ssafn.Blocks { - if block.Dominates(b) { - continue - } - if len(b.Instrs) == 0 { - continue - } - if _, ok := b.Instrs[len(b.Instrs)-1].(*ssa.Return); ok { - canReturn = true - break - } - } - if canReturn { - continue - } - j.Errorf(edge.Site, "infinite recursive call") - } - } -} - -func objectName(obj types.Object) string { - if obj == nil { - return "" - } - var name string - if obj.Pkg() != nil && obj.Pkg().Scope().Lookup(obj.Name()) == obj { - s := obj.Pkg().Path() - if s != "" { - name += s + "." - } - } - name += obj.Name() - return name -} - -func isName(j *lint.Job, expr ast.Expr, name string) bool { - var obj types.Object - switch expr := expr.(type) { - case *ast.Ident: - obj = ObjectOf(j, expr) - case *ast.SelectorExpr: - obj = ObjectOf(j, expr.Sel) - } - return objectName(obj) == name -} - -func (c *Checker) CheckLeakyTimeTick(j *lint.Job) { - for _, ssafn := range j.Program.InitialFunctions { - if IsInMain(j, ssafn) || IsInTest(j, ssafn) { - continue - } - for _, block := range ssafn.Blocks { - for _, ins := range block.Instrs { - call, ok := ins.(*ssa.Call) - if !ok || !IsCallTo(call.Common(), "time.Tick") { - continue - } - if c.funcDescs.Get(call.Parent()).Infinite { - continue - } - j.Errorf(call, "using time.Tick leaks the underlying ticker, consider using it only in endless functions, tests and the main package, and use time.NewTicker here") - } - } - } -} - -func (c *Checker) CheckDoubleNegation(j *lint.Job) { - fn := func(node ast.Node) bool { - unary1, ok := node.(*ast.UnaryExpr) - if !ok { - return true - } - unary2, ok := unary1.X.(*ast.UnaryExpr) - if !ok { - return true - } - if unary1.Op != token.NOT || unary2.Op != token.NOT { - return true - } - j.Errorf(unary1, "negating a boolean twice has no effect; is this a typo?") - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func hasSideEffects(node ast.Node) bool { - dynamic := false - ast.Inspect(node, func(node ast.Node) bool { - switch node := node.(type) { - case *ast.CallExpr: - dynamic = true - return false - case *ast.UnaryExpr: - if node.Op == token.ARROW { - dynamic = true - return false - } - } - return true - }) - return dynamic -} - -func (c *Checker) CheckRepeatedIfElse(j *lint.Job) { - seen := map[ast.Node]bool{} - - var collectConds func(ifstmt *ast.IfStmt, inits []ast.Stmt, conds []ast.Expr) ([]ast.Stmt, []ast.Expr) - collectConds = func(ifstmt *ast.IfStmt, inits []ast.Stmt, conds []ast.Expr) ([]ast.Stmt, []ast.Expr) { - seen[ifstmt] = true - if ifstmt.Init != nil { - inits = append(inits, ifstmt.Init) - } - conds = append(conds, ifstmt.Cond) - if elsestmt, ok := ifstmt.Else.(*ast.IfStmt); ok { - return collectConds(elsestmt, inits, conds) - } - return inits, conds - } - fn := func(node ast.Node) bool { - ifstmt, ok := node.(*ast.IfStmt) - if !ok { - return true - } - if seen[ifstmt] { - return true - } - inits, conds := collectConds(ifstmt, nil, nil) - if len(inits) > 0 { - return true - } - for _, cond := range conds { - if hasSideEffects(cond) { - return true - } - } - counts := map[string]int{} - for _, cond := range conds { - s := Render(j, cond) - counts[s]++ - if counts[s] == 2 { - j.Errorf(cond, "this condition occurs multiple times in this if/else if chain") - } - } - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) CheckSillyBitwiseOps(j *lint.Job) { - for _, ssafn := range j.Program.InitialFunctions { - for _, block := range ssafn.Blocks { - for _, ins := range block.Instrs { - ins, ok := ins.(*ssa.BinOp) - if !ok { - continue - } - - if c, ok := ins.Y.(*ssa.Const); !ok || c.Value == nil || c.Value.Kind() != constant.Int || c.Uint64() != 0 { - continue - } - switch ins.Op { - case token.AND, token.OR, token.XOR: - default: - // we do not flag shifts because too often, x<<0 is part - // of a pattern, x<<0, x<<8, x<<16, ... - continue - } - path, _ := astutil.PathEnclosingInterval(j.File(ins), ins.Pos(), ins.Pos()) - if len(path) == 0 { - continue - } - if node, ok := path[0].(*ast.BinaryExpr); !ok || !IsZero(node.Y) { - continue - } - - switch ins.Op { - case token.AND: - j.Errorf(ins, "x & 0 always equals 0") - case token.OR, token.XOR: - j.Errorf(ins, "x %s 0 always equals x", ins.Op) - } - } - } - } -} - -func (c *Checker) CheckNonOctalFileMode(j *lint.Job) { - fn := func(node ast.Node) bool { - call, ok := node.(*ast.CallExpr) - if !ok { - return true - } - sig, ok := TypeOf(j, call.Fun).(*types.Signature) - if !ok { - return true - } - n := sig.Params().Len() - var args []int - for i := 0; i < n; i++ { - typ := sig.Params().At(i).Type() - if IsType(typ, "os.FileMode") { - args = append(args, i) - } - } - for _, i := range args { - lit, ok := call.Args[i].(*ast.BasicLit) - if !ok { - continue - } - if len(lit.Value) == 3 && - lit.Value[0] != '0' && - lit.Value[0] >= '0' && lit.Value[0] <= '7' && - lit.Value[1] >= '0' && lit.Value[1] <= '7' && - lit.Value[2] >= '0' && lit.Value[2] <= '7' { - - v, err := strconv.ParseInt(lit.Value, 10, 64) - if err != nil { - continue - } - j.Errorf(call.Args[i], "file mode '%s' evaluates to %#o; did you mean '0%s'?", lit.Value, v, lit.Value) - } - } - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) CheckPureFunctions(j *lint.Job) { -fnLoop: - for _, ssafn := range j.Program.InitialFunctions { - if IsInTest(j, ssafn) { - params := ssafn.Signature.Params() - for i := 0; i < params.Len(); i++ { - param := params.At(i) - if IsType(param.Type(), "*testing.B") { - // Ignore discarded pure functions in code related - // to benchmarks. Instead of matching BenchmarkFoo - // functions, we match any function accepting a - // *testing.B. Benchmarks sometimes call generic - // functions for doing the actual work, and - // checking for the parameter is a lot easier and - // faster than analyzing call trees. - continue fnLoop - } - } - } - - for _, b := range ssafn.Blocks { - for _, ins := range b.Instrs { - ins, ok := ins.(*ssa.Call) - if !ok { - continue - } - refs := ins.Referrers() - if refs == nil || len(FilterDebug(*refs)) > 0 { - continue - } - callee := ins.Common().StaticCallee() - if callee == nil { - continue - } - if c.funcDescs.Get(callee).Pure && !c.funcDescs.Get(callee).Stub { - j.Errorf(ins, "%s is a pure function but its return value is ignored", callee.Name()) - continue - } - } - } - } -} - -func (c *Checker) isDeprecated(j *lint.Job, ident *ast.Ident) (bool, string) { - obj := ObjectOf(j, ident) - if obj.Pkg() == nil { - return false, "" - } - alt := c.deprecatedObjs[obj] - return alt != "", alt -} - -func (c *Checker) CheckDeprecated(j *lint.Job) { - // Selectors can appear outside of function literals, e.g. when - // declaring package level variables. - - var ssafn *ssa.Function - stack := 0 - fn := func(node ast.Node) bool { - if node == nil { - stack-- - } else { - stack++ - } - if stack == 1 { - ssafn = nil - } - if fn, ok := node.(*ast.FuncDecl); ok { - ssafn = j.Program.SSA.FuncValue(ObjectOf(j, fn.Name).(*types.Func)) - } - sel, ok := node.(*ast.SelectorExpr) - if !ok { - return true - } - - obj := ObjectOf(j, sel.Sel) - if obj.Pkg() == nil { - return true - } - nodePkg := j.NodePackage(node).Types - if nodePkg == obj.Pkg() || obj.Pkg().Path()+"_test" == nodePkg.Path() { - // Don't flag stuff in our own package - return true - } - if ok, alt := c.isDeprecated(j, sel.Sel); ok { - // Look for the first available alternative, not the first - // version something was deprecated in. If a function was - // deprecated in Go 1.6, an alternative has been available - // already in 1.0, and we're targeting 1.2, it still - // makes sense to use the alternative from 1.0, to be - // future-proof. - minVersion := deprecated.Stdlib[SelectorName(j, sel)].AlternativeAvailableSince - if !IsGoVersion(j, minVersion) { - return true - } - - if ssafn != nil { - if _, ok := c.deprecatedObjs[ssafn.Object()]; ok { - // functions that are deprecated may use deprecated - // symbols - return true - } - } - j.Errorf(sel, "%s is deprecated: %s", Render(j, sel), alt) - return true - } - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) callChecker(rules map[string]CallCheck) func(j *lint.Job) { - return func(j *lint.Job) { - c.checkCalls(j, rules) - } -} - -func (c *Checker) checkCalls(j *lint.Job, rules map[string]CallCheck) { - for _, ssafn := range j.Program.InitialFunctions { - node := c.funcDescs.CallGraph.CreateNode(ssafn) - for _, edge := range node.Out { - callee := edge.Callee.Func - obj, ok := callee.Object().(*types.Func) - if !ok { - continue - } - - r, ok := rules[obj.FullName()] - if !ok { - continue - } - var args []*Argument - ssaargs := edge.Site.Common().Args - if callee.Signature.Recv() != nil { - ssaargs = ssaargs[1:] - } - for _, arg := range ssaargs { - if iarg, ok := arg.(*ssa.MakeInterface); ok { - arg = iarg.X - } - vr := c.funcDescs.Get(edge.Site.Parent()).Ranges[arg] - args = append(args, &Argument{Value: Value{arg, vr}}) - } - call := &Call{ - Job: j, - Instr: edge.Site, - Args: args, - Checker: c, - Parent: edge.Site.Parent(), - } - r(call) - for idx, arg := range call.Args { - _ = idx - for _, e := range arg.invalids { - // path, _ := astutil.PathEnclosingInterval(f.File, edge.Site.Pos(), edge.Site.Pos()) - // if len(path) < 2 { - // continue - // } - // astcall, ok := path[0].(*ast.CallExpr) - // if !ok { - // continue - // } - // j.Errorf(astcall.Args[idx], "%s", e) - - j.Errorf(edge.Site, "%s", e) - } - } - for _, e := range call.invalids { - j.Errorf(call.Instr.Common(), "%s", e) - } - } - } -} - -func shortCallName(call *ssa.CallCommon) string { - if call.IsInvoke() { - return "" - } - switch v := call.Value.(type) { - case *ssa.Function: - fn, ok := v.Object().(*types.Func) - if !ok { - return "" - } - return fn.Name() - case *ssa.Builtin: - return v.Name() - } - return "" -} - -func (c *Checker) CheckWriterBufferModified(j *lint.Job) { - // TODO(dh): this might be a good candidate for taint analysis. - // Taint the argument as MUST_NOT_MODIFY, then propagate that - // through functions like bytes.Split - - for _, ssafn := range j.Program.InitialFunctions { - sig := ssafn.Signature - if ssafn.Name() != "Write" || sig.Recv() == nil || sig.Params().Len() != 1 || sig.Results().Len() != 2 { - continue - } - tArg, ok := sig.Params().At(0).Type().(*types.Slice) - if !ok { - continue - } - if basic, ok := tArg.Elem().(*types.Basic); !ok || basic.Kind() != types.Byte { - continue - } - if basic, ok := sig.Results().At(0).Type().(*types.Basic); !ok || basic.Kind() != types.Int { - continue - } - if named, ok := sig.Results().At(1).Type().(*types.Named); !ok || !IsType(named, "error") { - continue - } - - for _, block := range ssafn.Blocks { - for _, ins := range block.Instrs { - switch ins := ins.(type) { - case *ssa.Store: - addr, ok := ins.Addr.(*ssa.IndexAddr) - if !ok { - continue - } - if addr.X != ssafn.Params[1] { - continue - } - j.Errorf(ins, "io.Writer.Write must not modify the provided buffer, not even temporarily") - case *ssa.Call: - if !IsCallTo(ins.Common(), "append") { - continue - } - if ins.Common().Args[0] != ssafn.Params[1] { - continue - } - j.Errorf(ins, "io.Writer.Write must not modify the provided buffer, not even temporarily") - } - } - } - } -} - -func loopedRegexp(name string) CallCheck { - return func(call *Call) { - if len(extractConsts(call.Args[0].Value.Value)) == 0 { - return - } - if !call.Checker.isInLoop(call.Instr.Block()) { - return - } - call.Invalid(fmt.Sprintf("calling %s in a loop has poor performance, consider using regexp.Compile", name)) - } -} - -func (c *Checker) CheckEmptyBranch(j *lint.Job) { - for _, ssafn := range j.Program.InitialFunctions { - if ssafn.Syntax() == nil { - continue - } - if IsGenerated(j.File(ssafn.Syntax())) { - continue - } - if IsExample(ssafn) { - continue - } - fn := func(node ast.Node) bool { - ifstmt, ok := node.(*ast.IfStmt) - if !ok { - return true - } - if ifstmt.Else != nil { - b, ok := ifstmt.Else.(*ast.BlockStmt) - if !ok || len(b.List) != 0 { - return true - } - j.Errorf(ifstmt.Else, "empty branch") - } - if len(ifstmt.Body.List) != 0 { - return true - } - j.Errorf(ifstmt, "empty branch") - return true - } - Inspect(ssafn.Syntax(), fn) - } -} - -func (c *Checker) CheckMapBytesKey(j *lint.Job) { - for _, fn := range j.Program.InitialFunctions { - for _, b := range fn.Blocks { - insLoop: - for _, ins := range b.Instrs { - // find []byte -> string conversions - conv, ok := ins.(*ssa.Convert) - if !ok || conv.Type() != types.Universe.Lookup("string").Type() { - continue - } - if s, ok := conv.X.Type().(*types.Slice); !ok || s.Elem() != types.Universe.Lookup("byte").Type() { - continue - } - refs := conv.Referrers() - // need at least two (DebugRef) references: the - // conversion and the *ast.Ident - if refs == nil || len(*refs) < 2 { - continue - } - ident := false - // skip first reference, that's the conversion itself - for _, ref := range (*refs)[1:] { - switch ref := ref.(type) { - case *ssa.DebugRef: - if _, ok := ref.Expr.(*ast.Ident); !ok { - // the string seems to be used somewhere - // unexpected; the default branch should - // catch this already, but be safe - continue insLoop - } else { - ident = true - } - case *ssa.Lookup: - default: - // the string is used somewhere else than a - // map lookup - continue insLoop - } - } - - // the result of the conversion wasn't assigned to an - // identifier - if !ident { - continue - } - j.Errorf(conv, "m[string(key)] would be more efficient than k := string(key); m[k]") - } - } - } -} - -func (c *Checker) CheckRangeStringRunes(j *lint.Job) { - sharedcheck.CheckRangeStringRunes(j) -} - -func (c *Checker) CheckSelfAssignment(j *lint.Job) { - fn := func(node ast.Node) bool { - assign, ok := node.(*ast.AssignStmt) - if !ok { - return true - } - if assign.Tok != token.ASSIGN || len(assign.Lhs) != len(assign.Rhs) { - return true - } - for i, stmt := range assign.Lhs { - rlh := Render(j, stmt) - rrh := Render(j, assign.Rhs[i]) - if rlh == rrh { - j.Errorf(assign, "self-assignment of %s to %s", rrh, rlh) - } - } - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func buildTagsIdentical(s1, s2 []string) bool { - if len(s1) != len(s2) { - return false - } - s1s := make([]string, len(s1)) - copy(s1s, s1) - sort.Strings(s1s) - s2s := make([]string, len(s2)) - copy(s2s, s2) - sort.Strings(s2s) - for i, s := range s1s { - if s != s2s[i] { - return false - } - } - return true -} - -func (c *Checker) CheckDuplicateBuildConstraints(job *lint.Job) { - for _, f := range job.Program.Files { - constraints := buildTags(f) - for i, constraint1 := range constraints { - for j, constraint2 := range constraints { - if i >= j { - continue - } - if buildTagsIdentical(constraint1, constraint2) { - job.Errorf(f, "identical build constraints %q and %q", - strings.Join(constraint1, " "), - strings.Join(constraint2, " ")) - } - } - } - } -} - -func (c *Checker) CheckSillyRegexp(j *lint.Job) { - // We could use the rule checking engine for this, but the - // arguments aren't really invalid. - for _, fn := range j.Program.InitialFunctions { - for _, b := range fn.Blocks { - for _, ins := range b.Instrs { - call, ok := ins.(*ssa.Call) - if !ok { - continue - } - switch CallName(call.Common()) { - case "regexp.MustCompile", "regexp.Compile", "regexp.Match", "regexp.MatchReader", "regexp.MatchString": - default: - continue - } - c, ok := call.Common().Args[0].(*ssa.Const) - if !ok { - continue - } - s := constant.StringVal(c.Value) - re, err := syntax.Parse(s, 0) - if err != nil { - continue - } - if re.Op != syntax.OpLiteral && re.Op != syntax.OpEmptyMatch { - continue - } - j.Errorf(call, "regular expression does not contain any meta characters") - } - } - } -} - -func (c *Checker) CheckMissingEnumTypesInDeclaration(j *lint.Job) { - fn := func(node ast.Node) bool { - decl, ok := node.(*ast.GenDecl) - if !ok { - return true - } - if !decl.Lparen.IsValid() { - return true - } - if decl.Tok != token.CONST { - return true - } - - groups := GroupSpecs(j, decl.Specs) - groupLoop: - for _, group := range groups { - if len(group) < 2 { - continue - } - if group[0].(*ast.ValueSpec).Type == nil { - // first constant doesn't have a type - continue groupLoop - } - for i, spec := range group { - spec := spec.(*ast.ValueSpec) - if len(spec.Names) != 1 || len(spec.Values) != 1 { - continue groupLoop - } - switch v := spec.Values[0].(type) { - case *ast.BasicLit: - case *ast.UnaryExpr: - if _, ok := v.X.(*ast.BasicLit); !ok { - continue groupLoop - } - default: - // if it's not a literal it might be typed, such as - // time.Microsecond = 1000 * Nanosecond - continue groupLoop - } - if i == 0 { - continue - } - if spec.Type != nil { - continue groupLoop - } - } - j.Errorf(group[0], "only the first constant in this group has an explicit type") - } - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) - } -} - -func (c *Checker) CheckTimerResetReturnValue(j *lint.Job) { - for _, fn := range j.Program.InitialFunctions { - for _, block := range fn.Blocks { - for _, ins := range block.Instrs { - call, ok := ins.(*ssa.Call) - if !ok { - continue - } - if !IsCallTo(call.Common(), "(*time.Timer).Reset") { - continue - } - refs := call.Referrers() - if refs == nil { - continue - } - for _, ref := range FilterDebug(*refs) { - ifstmt, ok := ref.(*ssa.If) - if !ok { - continue - } - - found := false - for _, succ := range ifstmt.Block().Succs { - if len(succ.Preds) != 1 { - // Merge point, not a branch in the - // syntactical sense. - - // FIXME(dh): this is broken for if - // statements a la "if x || y" - continue - } - ssautil.Walk(succ, func(b *ssa.BasicBlock) bool { - if !succ.Dominates(b) { - // We've reached the end of the branch - return false - } - for _, ins := range b.Instrs { - // TODO(dh): we should check that - // we're receiving from the channel of - // a time.Timer to further reduce - // false positives. Not a key - // priority, considering the rarity of - // Reset and the tiny likeliness of a - // false positive - if ins, ok := ins.(*ssa.UnOp); ok && ins.Op == token.ARROW && IsType(ins.X.Type(), "<-chan time.Time") { - found = true - return false - } - } - return true - }) - } - - if found { - j.Errorf(call, "it is not possible to use Reset's return value correctly, as there is a race condition between draining the channel and the new timer expiring") - } - } - } - } - } -} diff --git a/vendor/github.com/golangci/go-tools/unused/unused.go b/vendor/github.com/golangci/go-tools/unused/unused.go deleted file mode 100644 index d80b66227d42..000000000000 --- a/vendor/github.com/golangci/go-tools/unused/unused.go +++ /dev/null @@ -1,1100 +0,0 @@ -package unused // import "github.com/golangci/go-tools/unused" - -import ( - "fmt" - "go/ast" - "go/token" - "go/types" - "io" - "path/filepath" - "strings" - - "github.com/golangci/go-tools/lint" - . "github.com/golangci/go-tools/lint/lintdsl" - - "golang.org/x/tools/go/packages" - "golang.org/x/tools/go/types/typeutil" -) - -func NewLintChecker(c *Checker) *LintChecker { - l := &LintChecker{ - c: c, - } - return l -} - -type LintChecker struct { - c *Checker -} - -func (*LintChecker) Name() string { return "unused" } -func (*LintChecker) Prefix() string { return "U" } - -func (l *LintChecker) Init(*lint.Program) {} -func (l *LintChecker) Checks() []lint.Check { - return []lint.Check{ - {ID: "U1000", FilterGenerated: true, Fn: l.Lint}, - } -} - -func typString(obj types.Object) string { - switch obj := obj.(type) { - case *types.Func: - return "func" - case *types.Var: - if obj.IsField() { - return "field" - } - return "var" - case *types.Const: - return "const" - case *types.TypeName: - return "type" - default: - // log.Printf("%T", obj) - return "identifier" - } -} - -func (l *LintChecker) Lint(j *lint.Job) { - unused := l.c.Check(j.Program) - for _, u := range unused { - name := u.Obj.Name() - if sig, ok := u.Obj.Type().(*types.Signature); ok && sig.Recv() != nil { - switch sig.Recv().Type().(type) { - case *types.Named, *types.Pointer: - typ := types.TypeString(sig.Recv().Type(), func(*types.Package) string { return "" }) - if len(typ) > 0 && typ[0] == '*' { - name = fmt.Sprintf("(%s).%s", typ, u.Obj.Name()) - } else if len(typ) > 0 { - name = fmt.Sprintf("%s.%s", typ, u.Obj.Name()) - } - } - } - j.Errorf(u.Obj, "%s %s is unused", typString(u.Obj), name) - } -} - -type graph struct { - roots []*graphNode - nodes map[interface{}]*graphNode -} - -func (g *graph) markUsedBy(obj, usedBy interface{}) { - objNode := g.getNode(obj) - usedByNode := g.getNode(usedBy) - if objNode.obj == usedByNode.obj { - return - } - usedByNode.uses[objNode] = struct{}{} -} - -var labelCounter = 1 - -func (g *graph) getNode(obj interface{}) *graphNode { - for { - if pt, ok := obj.(*types.Pointer); ok { - obj = pt.Elem() - } else { - break - } - } - _, ok := g.nodes[obj] - if !ok { - g.addObj(obj) - } - - return g.nodes[obj] -} - -func (g *graph) addObj(obj interface{}) { - if pt, ok := obj.(*types.Pointer); ok { - obj = pt.Elem() - } - node := &graphNode{obj: obj, uses: make(map[*graphNode]struct{}), n: labelCounter} - g.nodes[obj] = node - labelCounter++ - - if obj, ok := obj.(*types.Struct); ok { - n := obj.NumFields() - for i := 0; i < n; i++ { - field := obj.Field(i) - g.markUsedBy(obj, field) - } - } -} - -type graphNode struct { - obj interface{} - uses map[*graphNode]struct{} - used bool - quiet bool - n int -} - -type CheckMode int - -const ( - CheckConstants CheckMode = 1 << iota - CheckFields - CheckFunctions - CheckTypes - CheckVariables - - CheckAll = CheckConstants | CheckFields | CheckFunctions | CheckTypes | CheckVariables -) - -type Unused struct { - Obj types.Object - Position token.Position -} - -type Checker struct { - Mode CheckMode - WholeProgram bool - ConsiderReflection bool - Debug io.Writer - - graph *graph - - msCache typeutil.MethodSetCache - prog *lint.Program - topmostCache map[*types.Scope]*types.Scope - interfaces []*types.Interface -} - -func NewChecker(mode CheckMode) *Checker { - return &Checker{ - Mode: mode, - graph: &graph{ - nodes: make(map[interface{}]*graphNode), - }, - topmostCache: make(map[*types.Scope]*types.Scope), - } -} - -func (c *Checker) checkConstants() bool { return (c.Mode & CheckConstants) > 0 } -func (c *Checker) checkFields() bool { return (c.Mode & CheckFields) > 0 } -func (c *Checker) checkFunctions() bool { return (c.Mode & CheckFunctions) > 0 } -func (c *Checker) checkTypes() bool { return (c.Mode & CheckTypes) > 0 } -func (c *Checker) checkVariables() bool { return (c.Mode & CheckVariables) > 0 } - -func (c *Checker) markFields(typ types.Type) { - structType, ok := typ.Underlying().(*types.Struct) - if !ok { - return - } - n := structType.NumFields() - for i := 0; i < n; i++ { - field := structType.Field(i) - c.graph.markUsedBy(field, typ) - } -} - -type Error struct { - Errors map[string][]error -} - -func (e Error) Error() string { - return fmt.Sprintf("errors in %d packages", len(e.Errors)) -} - -func (c *Checker) Check(prog *lint.Program) []Unused { - var unused []Unused - c.prog = prog - if c.WholeProgram { - c.findExportedInterfaces() - } - for _, pkg := range prog.InitialPackages { - c.processDefs(pkg) - c.processUses(pkg) - c.processTypes(pkg) - c.processSelections(pkg) - c.processAST(pkg) - } - - for _, node := range c.graph.nodes { - obj, ok := node.obj.(types.Object) - if !ok { - continue - } - typNode, ok := c.graph.nodes[obj.Type()] - if !ok { - continue - } - node.uses[typNode] = struct{}{} - } - - roots := map[*graphNode]struct{}{} - for _, root := range c.graph.roots { - roots[root] = struct{}{} - } - markNodesUsed(roots) - c.markNodesQuiet() - c.deduplicate() - - if c.Debug != nil { - c.printDebugGraph(c.Debug) - } - - for _, node := range c.graph.nodes { - if node.used || node.quiet { - continue - } - obj, ok := node.obj.(types.Object) - if !ok { - continue - } - found := false - if !false { - for _, pkg := range prog.InitialPackages { - if pkg.Types == obj.Pkg() { - found = true - break - } - } - } - if !found { - continue - } - - pos := c.prog.Fset().Position(obj.Pos()) - if pos.Filename == "" || filepath.Base(pos.Filename) == "C" { - continue - } - - unused = append(unused, Unused{Obj: obj, Position: pos}) - } - - return unused -} - -// isNoCopyType reports whether a type represents the NoCopy sentinel -// type. The NoCopy type is a named struct with no fields and exactly -// one method `func Lock()` that is empty. -// -// FIXME(dh): currently we're not checking that the function body is -// empty. -func isNoCopyType(typ types.Type) bool { - st, ok := typ.Underlying().(*types.Struct) - if !ok { - return false - } - if st.NumFields() != 0 { - return false - } - - named, ok := typ.(*types.Named) - if !ok { - return false - } - if named.NumMethods() != 1 { - return false - } - meth := named.Method(0) - if meth.Name() != "Lock" { - return false - } - sig := meth.Type().(*types.Signature) - if sig.Params().Len() != 0 || sig.Results().Len() != 0 { - return false - } - return true -} - -func (c *Checker) useNoCopyFields(typ types.Type) { - if st, ok := typ.Underlying().(*types.Struct); ok { - n := st.NumFields() - for i := 0; i < n; i++ { - field := st.Field(i) - if isNoCopyType(field.Type()) { - c.graph.markUsedBy(field, typ) - c.graph.markUsedBy(field.Type().(*types.Named).Method(0), field.Type()) - } - } - } -} - -func (c *Checker) useExportedFields(typ types.Type, by types.Type) bool { - any := false - if st, ok := typ.Underlying().(*types.Struct); ok { - n := st.NumFields() - for i := 0; i < n; i++ { - field := st.Field(i) - if field.Anonymous() { - if c.useExportedFields(field.Type(), typ) { - c.graph.markUsedBy(field, typ) - } - } - if field.Exported() { - c.graph.markUsedBy(field, by) - any = true - } - } - } - return any -} - -func (c *Checker) useExportedMethods(typ types.Type) { - named, ok := typ.(*types.Named) - if !ok { - return - } - ms := typeutil.IntuitiveMethodSet(named, &c.msCache) - for i := 0; i < len(ms); i++ { - meth := ms[i].Obj() - if meth.Exported() { - c.graph.markUsedBy(meth, typ) - } - } - - st, ok := named.Underlying().(*types.Struct) - if !ok { - return - } - n := st.NumFields() - for i := 0; i < n; i++ { - field := st.Field(i) - if !field.Anonymous() { - continue - } - ms := typeutil.IntuitiveMethodSet(field.Type(), &c.msCache) - for j := 0; j < len(ms); j++ { - if ms[j].Obj().Exported() { - c.graph.markUsedBy(field, typ) - break - } - } - } -} - -func (c *Checker) processDefs(pkg *lint.Pkg) { - for _, obj := range pkg.TypesInfo.Defs { - if obj == nil { - continue - } - c.graph.getNode(obj) - - if obj, ok := obj.(*types.TypeName); ok { - c.graph.markUsedBy(obj.Type().Underlying(), obj.Type()) - c.graph.markUsedBy(obj.Type(), obj) // TODO is this needed? - c.graph.markUsedBy(obj, obj.Type()) - - // We mark all exported fields as used. For normal - // operation, we have to. The user may use these fields - // without us knowing. - // - // TODO(dh): In whole-program mode, however, we mark them - // as used because of reflection (such as JSON - // marshaling). Strictly speaking, we would only need to - // mark them used if an instance of the type was - // accessible via an interface value. - if !c.WholeProgram || c.ConsiderReflection { - c.useExportedFields(obj.Type(), obj.Type()) - } - - // TODO(dh): Traditionally we have not marked all exported - // methods as exported, even though they're strictly - // speaking accessible through reflection. We've done that - // because using methods just via reflection is rare, and - // not worth the false negatives. With the new -reflect - // flag, however, we should reconsider that choice. - if !c.WholeProgram { - c.useExportedMethods(obj.Type()) - } - } - - switch obj := obj.(type) { - case *types.Var, *types.Const, *types.Func, *types.TypeName: - if obj.Exported() { - // Exported variables and constants use their types, - // even if there's no expression using them in the - // checked program. - // - // Also operates on funcs and type names, but that's - // irrelevant/redundant. - c.graph.markUsedBy(obj.Type(), obj) - } - if obj.Name() == "_" { - node := c.graph.getNode(obj) - node.quiet = true - scope := c.topmostScope(pkg.Types.Scope().Innermost(obj.Pos()), pkg.Types) - if scope == pkg.Types.Scope() { - c.graph.roots = append(c.graph.roots, node) - } else { - c.graph.markUsedBy(obj, scope) - } - } else { - // Variables declared in functions are used. This is - // done so that arguments and return parameters are - // always marked as used. - if _, ok := obj.(*types.Var); ok { - if obj.Parent() != obj.Pkg().Scope() && obj.Parent() != nil { - c.graph.markUsedBy(obj, c.topmostScope(obj.Parent(), obj.Pkg())) - c.graph.markUsedBy(obj.Type(), obj) - } - } - } - } - - if fn, ok := obj.(*types.Func); ok { - // A function uses its signature - c.graph.markUsedBy(fn, fn.Type()) - - // A function uses its return types - sig := fn.Type().(*types.Signature) - res := sig.Results() - n := res.Len() - for i := 0; i < n; i++ { - c.graph.markUsedBy(res.At(i).Type(), fn) - } - } - - if obj, ok := obj.(interface { - Scope() *types.Scope - Pkg() *types.Package - }); ok { - scope := obj.Scope() - c.graph.markUsedBy(c.topmostScope(scope, obj.Pkg()), obj) - } - - if c.isRoot(obj) { - node := c.graph.getNode(obj) - c.graph.roots = append(c.graph.roots, node) - if obj, ok := obj.(*types.PkgName); ok { - scope := obj.Pkg().Scope() - c.graph.markUsedBy(scope, obj) - } - } - } -} - -func (c *Checker) processUses(pkg *lint.Pkg) { - for ident, usedObj := range pkg.TypesInfo.Uses { - if _, ok := usedObj.(*types.PkgName); ok { - continue - } - pos := ident.Pos() - scope := pkg.Types.Scope().Innermost(pos) - scope = c.topmostScope(scope, pkg.Types) - if scope != pkg.Types.Scope() { - c.graph.markUsedBy(usedObj, scope) - } - - switch usedObj.(type) { - case *types.Var, *types.Const: - c.graph.markUsedBy(usedObj.Type(), usedObj) - } - } -} - -func (c *Checker) findExportedInterfaces() { - c.interfaces = []*types.Interface{types.Universe.Lookup("error").Type().(*types.Named).Underlying().(*types.Interface)} - var pkgs []*packages.Package - if c.WholeProgram { - pkgs = append(pkgs, c.prog.AllPackages...) - } else { - for _, pkg := range c.prog.InitialPackages { - pkgs = append(pkgs, pkg.Package) - } - } - - for _, pkg := range pkgs { - for _, tv := range pkg.TypesInfo.Types { - iface, ok := tv.Type.(*types.Interface) - if !ok { - continue - } - if iface.NumMethods() == 0 { - continue - } - c.interfaces = append(c.interfaces, iface) - } - } -} - -func (c *Checker) processTypes(pkg *lint.Pkg) { - named := map[*types.Named]*types.Pointer{} - var interfaces []*types.Interface - for _, tv := range pkg.TypesInfo.Types { - if typ, ok := tv.Type.(interface { - Elem() types.Type - }); ok { - c.graph.markUsedBy(typ.Elem(), typ) - } - - switch obj := tv.Type.(type) { - case *types.Named: - named[obj] = types.NewPointer(obj) - c.graph.markUsedBy(obj, obj.Underlying()) - c.graph.markUsedBy(obj.Underlying(), obj) - case *types.Interface: - if obj.NumMethods() > 0 { - interfaces = append(interfaces, obj) - } - case *types.Struct: - c.useNoCopyFields(obj) - if pkg.Types.Name() != "main" && !c.WholeProgram { - c.useExportedFields(obj, obj) - } - } - } - - // Pretend that all types are meant to implement as many - // interfaces as possible. - // - // TODO(dh): For normal operations, that's the best we can do, as - // we have no idea what external users will do with our types. In - // whole-program mode, we could be more precise, in two ways: - // 1) Only consider interfaces if a type has been assigned to one - // 2) Use SSA and flow analysis and determine the exact set of - // interfaces that is relevant. - fn := func(iface *types.Interface) { - for i := 0; i < iface.NumEmbeddeds(); i++ { - c.graph.markUsedBy(iface.Embedded(i), iface) - } - namedLoop: - for obj, objPtr := range named { - switch obj.Underlying().(type) { - case *types.Interface: - // pointers to interfaces have no methods, only checking non-pointer - if !c.implements(obj, iface) { - continue namedLoop - } - default: - // pointer receivers include the method set of non-pointer receivers, - // only checking pointer - if !c.implements(objPtr, iface) { - continue namedLoop - } - } - - ifaceMethods := make(map[string]struct{}, iface.NumMethods()) - n := iface.NumMethods() - for i := 0; i < n; i++ { - meth := iface.Method(i) - ifaceMethods[meth.Name()] = struct{}{} - } - for _, obj := range []types.Type{obj, objPtr} { - ms := c.msCache.MethodSet(obj) - n := ms.Len() - for i := 0; i < n; i++ { - sel := ms.At(i) - meth := sel.Obj().(*types.Func) - _, found := ifaceMethods[meth.Name()] - if !found { - continue - } - c.graph.markUsedBy(meth.Type().(*types.Signature).Recv().Type(), obj) // embedded receiver - if len(sel.Index()) > 1 { - f := getField(obj, sel.Index()[0]) - c.graph.markUsedBy(f, obj) // embedded receiver - } - c.graph.markUsedBy(meth, obj) - } - } - } - } - - for _, iface := range interfaces { - fn(iface) - } - for _, iface := range c.interfaces { - fn(iface) - } -} - -func (c *Checker) processSelections(pkg *lint.Pkg) { - fn := func(expr *ast.SelectorExpr, sel *types.Selection, offset int) { - scope := pkg.Types.Scope().Innermost(expr.Pos()) - c.graph.markUsedBy(sel, c.topmostScope(scope, pkg.Types)) - c.graph.markUsedBy(sel.Obj(), sel) - if len(sel.Index()) > 1 { - typ := sel.Recv() - indices := sel.Index() - for _, idx := range indices[:len(indices)-offset] { - obj := getField(typ, idx) - typ = obj.Type() - c.graph.markUsedBy(obj, sel) - } - } - } - - for expr, sel := range pkg.TypesInfo.Selections { - switch sel.Kind() { - case types.FieldVal: - fn(expr, sel, 0) - case types.MethodVal: - fn(expr, sel, 1) - } - } -} - -func dereferenceType(typ types.Type) types.Type { - if typ, ok := typ.(*types.Pointer); ok { - return typ.Elem() - } - return typ -} - -// processConversion marks fields as used if they're part of a type conversion. -func (c *Checker) processConversion(pkg *lint.Pkg, node ast.Node) { - if node, ok := node.(*ast.CallExpr); ok { - callTyp := pkg.TypesInfo.TypeOf(node.Fun) - var typDst *types.Struct - var ok bool - switch typ := callTyp.(type) { - case *types.Named: - typDst, ok = typ.Underlying().(*types.Struct) - case *types.Pointer: - typDst, ok = typ.Elem().Underlying().(*types.Struct) - default: - return - } - if !ok { - return - } - - if typ, ok := pkg.TypesInfo.TypeOf(node.Args[0]).(*types.Basic); ok && typ.Kind() == types.UnsafePointer { - // This is an unsafe conversion. Assume that all the - // fields are relevant (they are, because of memory - // layout) - n := typDst.NumFields() - for i := 0; i < n; i++ { - c.graph.markUsedBy(typDst.Field(i), typDst) - } - return - } - - typSrc, ok := dereferenceType(pkg.TypesInfo.TypeOf(node.Args[0])).Underlying().(*types.Struct) - if !ok { - return - } - - // When we convert from type t1 to t2, were t1 and t2 are - // structs, all fields are relevant, as otherwise the - // conversion would fail. - // - // We mark t2's fields as used by t1's fields, and vice - // versa. That way, if no code actually refers to a field - // in either type, it's still correctly marked as unused. - // If a field is used in either struct, it's implicitly - // relevant in the other one, too. - // - // It works in a similar way for conversions between types - // of two packages, only that the extra information in the - // graph is redundant unless we're in whole program mode. - n := typDst.NumFields() - for i := 0; i < n; i++ { - fDst := typDst.Field(i) - fSrc := typSrc.Field(i) - c.graph.markUsedBy(fDst, fSrc) - c.graph.markUsedBy(fSrc, fDst) - } - } -} - -// processCompositeLiteral marks fields as used if the struct is used -// in a composite literal. -func (c *Checker) processCompositeLiteral(pkg *lint.Pkg, node ast.Node) { - // XXX how does this actually work? wouldn't it match t{}? - if node, ok := node.(*ast.CompositeLit); ok { - typ := pkg.TypesInfo.TypeOf(node) - if _, ok := typ.(*types.Named); ok { - typ = typ.Underlying() - } - if _, ok := typ.(*types.Struct); !ok { - return - } - - if isBasicStruct(node.Elts) { - c.markFields(typ) - } - } -} - -// processCgoExported marks functions as used if they're being -// exported to cgo. -func (c *Checker) processCgoExported(pkg *lint.Pkg, node ast.Node) { - if node, ok := node.(*ast.FuncDecl); ok { - if node.Doc == nil { - return - } - for _, cmt := range node.Doc.List { - if !strings.HasPrefix(cmt.Text, "//go:cgo_export_") { - return - } - obj := pkg.TypesInfo.ObjectOf(node.Name) - c.graph.roots = append(c.graph.roots, c.graph.getNode(obj)) - } - } -} - -func (c *Checker) processVariableDeclaration(pkg *lint.Pkg, node ast.Node) { - if decl, ok := node.(*ast.GenDecl); ok { - for _, spec := range decl.Specs { - spec, ok := spec.(*ast.ValueSpec) - if !ok { - continue - } - for i, name := range spec.Names { - if i >= len(spec.Values) { - break - } - value := spec.Values[i] - fn := func(node ast.Node) bool { - if node3, ok := node.(*ast.Ident); ok { - obj := pkg.TypesInfo.ObjectOf(node3) - if _, ok := obj.(*types.PkgName); ok { - return true - } - c.graph.markUsedBy(obj, pkg.TypesInfo.ObjectOf(name)) - } - return true - } - ast.Inspect(value, fn) - } - } - } -} - -func (c *Checker) processArrayConstants(pkg *lint.Pkg, node ast.Node) { - if decl, ok := node.(*ast.ArrayType); ok { - ident, ok := decl.Len.(*ast.Ident) - if !ok { - return - } - c.graph.markUsedBy(pkg.TypesInfo.ObjectOf(ident), pkg.TypesInfo.TypeOf(decl)) - } -} - -func (c *Checker) processKnownReflectMethodCallers(pkg *lint.Pkg, node ast.Node) { - call, ok := node.(*ast.CallExpr) - if !ok { - return - } - sel, ok := call.Fun.(*ast.SelectorExpr) - if !ok { - return - } - if !IsType(pkg.TypesInfo.TypeOf(sel.X), "*net/rpc.Server") { - x, ok := sel.X.(*ast.Ident) - if !ok { - return - } - pkgname, ok := pkg.TypesInfo.ObjectOf(x).(*types.PkgName) - if !ok { - return - } - if pkgname.Imported().Path() != "net/rpc" { - return - } - } - - var arg ast.Expr - switch sel.Sel.Name { - case "Register": - if len(call.Args) != 1 { - return - } - arg = call.Args[0] - case "RegisterName": - if len(call.Args) != 2 { - return - } - arg = call.Args[1] - } - typ := pkg.TypesInfo.TypeOf(arg) - ms := types.NewMethodSet(typ) - for i := 0; i < ms.Len(); i++ { - c.graph.markUsedBy(ms.At(i).Obj(), typ) - } -} - -func (c *Checker) processAST(pkg *lint.Pkg) { - fn := func(node ast.Node) bool { - c.processConversion(pkg, node) - c.processKnownReflectMethodCallers(pkg, node) - c.processCompositeLiteral(pkg, node) - c.processCgoExported(pkg, node) - c.processVariableDeclaration(pkg, node) - c.processArrayConstants(pkg, node) - return true - } - for _, file := range pkg.Syntax { - ast.Inspect(file, fn) - } -} - -func isBasicStruct(elts []ast.Expr) bool { - for _, elt := range elts { - if _, ok := elt.(*ast.KeyValueExpr); !ok { - return true - } - } - return false -} - -func isPkgScope(obj types.Object) bool { - return obj.Parent() == obj.Pkg().Scope() -} - -func isMain(obj types.Object) bool { - if obj.Pkg().Name() != "main" { - return false - } - if obj.Name() != "main" { - return false - } - if !isPkgScope(obj) { - return false - } - if !isFunction(obj) { - return false - } - if isMethod(obj) { - return false - } - return true -} - -func isFunction(obj types.Object) bool { - _, ok := obj.(*types.Func) - return ok -} - -func isMethod(obj types.Object) bool { - if !isFunction(obj) { - return false - } - return obj.(*types.Func).Type().(*types.Signature).Recv() != nil -} - -func isVariable(obj types.Object) bool { - _, ok := obj.(*types.Var) - return ok -} - -func isConstant(obj types.Object) bool { - _, ok := obj.(*types.Const) - return ok -} - -func isType(obj types.Object) bool { - _, ok := obj.(*types.TypeName) - return ok -} - -func isField(obj types.Object) bool { - if obj, ok := obj.(*types.Var); ok && obj.IsField() { - return true - } - return false -} - -func (c *Checker) checkFlags(v interface{}) bool { - obj, ok := v.(types.Object) - if !ok { - return false - } - if isFunction(obj) && !c.checkFunctions() { - return false - } - if isVariable(obj) && !c.checkVariables() { - return false - } - if isConstant(obj) && !c.checkConstants() { - return false - } - if isType(obj) && !c.checkTypes() { - return false - } - if isField(obj) && !c.checkFields() { - return false - } - return true -} - -func (c *Checker) isRoot(obj types.Object) bool { - // - in local mode, main, init, tests, and non-test, non-main exported are roots - // - in global mode (not yet implemented), main, init and tests are roots - - if _, ok := obj.(*types.PkgName); ok { - return true - } - - if isMain(obj) || (isFunction(obj) && !isMethod(obj) && obj.Name() == "init") { - return true - } - if obj.Exported() { - f := c.prog.Fset().Position(obj.Pos()).Filename - if strings.HasSuffix(f, "_test.go") { - return strings.HasPrefix(obj.Name(), "Test") || - strings.HasPrefix(obj.Name(), "Benchmark") || - strings.HasPrefix(obj.Name(), "Example") - } - - // Package-level are used, except in package main - if isPkgScope(obj) && obj.Pkg().Name() != "main" && !c.WholeProgram { - return true - } - } - return false -} - -func markNodesUsed(nodes map[*graphNode]struct{}) { - for node := range nodes { - wasUsed := node.used - node.used = true - if !wasUsed { - markNodesUsed(node.uses) - } - } -} - -// deduplicate merges objects based on their positions. This is done -// to work around packages existing multiple times in go/packages. -func (c *Checker) deduplicate() { - m := map[token.Position]struct{ used, quiet bool }{} - for _, node := range c.graph.nodes { - obj, ok := node.obj.(types.Object) - if !ok { - continue - } - pos := c.prog.Fset().Position(obj.Pos()) - m[pos] = struct{ used, quiet bool }{ - m[pos].used || node.used, - m[pos].quiet || node.quiet, - } - } - - for _, node := range c.graph.nodes { - obj, ok := node.obj.(types.Object) - if !ok { - continue - } - pos := c.prog.Fset().Position(obj.Pos()) - node.used = m[pos].used - node.quiet = m[pos].quiet - } -} - -func (c *Checker) markNodesQuiet() { - for _, node := range c.graph.nodes { - if node.used { - continue - } - if obj, ok := node.obj.(types.Object); ok && !c.checkFlags(obj) { - node.quiet = true - continue - } - c.markObjQuiet(node.obj) - } -} - -func (c *Checker) markObjQuiet(obj interface{}) { - switch obj := obj.(type) { - case *types.Named: - n := obj.NumMethods() - for i := 0; i < n; i++ { - meth := obj.Method(i) - node := c.graph.getNode(meth) - node.quiet = true - c.markObjQuiet(meth.Scope()) - } - case *types.Struct: - n := obj.NumFields() - for i := 0; i < n; i++ { - field := obj.Field(i) - c.graph.nodes[field].quiet = true - } - case *types.Func: - c.markObjQuiet(obj.Scope()) - case *types.Scope: - if obj == nil { - return - } - if obj.Parent() == types.Universe { - return - } - for _, name := range obj.Names() { - v := obj.Lookup(name) - if n, ok := c.graph.nodes[v]; ok { - n.quiet = true - } - } - n := obj.NumChildren() - for i := 0; i < n; i++ { - c.markObjQuiet(obj.Child(i)) - } - } -} - -func getField(typ types.Type, idx int) *types.Var { - switch obj := typ.(type) { - case *types.Pointer: - return getField(obj.Elem(), idx) - case *types.Named: - switch v := obj.Underlying().(type) { - case *types.Struct: - return v.Field(idx) - case *types.Pointer: - return getField(v.Elem(), idx) - default: - panic(fmt.Sprintf("unexpected type %s", typ)) - } - case *types.Struct: - return obj.Field(idx) - } - return nil -} - -func (c *Checker) topmostScope(scope *types.Scope, pkg *types.Package) (ret *types.Scope) { - if top, ok := c.topmostCache[scope]; ok { - return top - } - defer func() { - c.topmostCache[scope] = ret - }() - if scope == pkg.Scope() { - return scope - } - if scope.Parent().Parent() == pkg.Scope() { - return scope - } - return c.topmostScope(scope.Parent(), pkg) -} - -func (c *Checker) printDebugGraph(w io.Writer) { - fmt.Fprintln(w, "digraph {") - fmt.Fprintln(w, "n0 [label = roots]") - for _, node := range c.graph.nodes { - s := fmt.Sprintf("%s (%T)", node.obj, node.obj) - s = strings.Replace(s, "\n", "", -1) - s = strings.Replace(s, `"`, "", -1) - fmt.Fprintf(w, `n%d [label = %q]`, node.n, s) - color := "black" - switch { - case node.used: - color = "green" - case node.quiet: - color = "orange" - case !c.checkFlags(node.obj): - color = "purple" - default: - color = "red" - } - fmt.Fprintf(w, "[color = %s]", color) - fmt.Fprintln(w) - } - - for _, node1 := range c.graph.nodes { - for node2 := range node1.uses { - fmt.Fprintf(w, "n%d -> n%d\n", node1.n, node2.n) - } - } - for _, root := range c.graph.roots { - fmt.Fprintf(w, "n0 -> n%d\n", root.n) - } - fmt.Fprintln(w, "}") -} diff --git a/vendor/github.com/golangci/go-tools/version/version.go b/vendor/github.com/golangci/go-tools/version/version.go deleted file mode 100644 index 511fb0bdae6d..000000000000 --- a/vendor/github.com/golangci/go-tools/version/version.go +++ /dev/null @@ -1,17 +0,0 @@ -package version - -import ( - "fmt" - "os" - "path/filepath" -) - -const Version = "2019.1.1" - -func Print() { - if Version == "devel" { - fmt.Printf("%s (no version)\n", filepath.Base(os.Args[0])) - } else { - fmt.Printf("%s %s\n", filepath.Base(os.Args[0]), Version) - } -} diff --git a/vendor/golang.org/x/tools/go/packages/golist.go b/vendor/golang.org/x/tools/go/packages/golist.go index ed851ef6a3ff..add7784ef7c5 100644 --- a/vendor/golang.org/x/tools/go/packages/golist.go +++ b/vendor/golang.org/x/tools/go/packages/golist.go @@ -102,7 +102,7 @@ func goListDriver(cfg *Config, patterns ...string) (*driverResponse, error) { var sizes types.Sizes var sizeserr error var sizeswg sync.WaitGroup - if cfg.Mode&NeedTypesSizes != 0 || cfg.Mode&NeedTypes != 0 { + if cfg.Mode&NeedTypesSizes != 0 { sizeswg.Add(1) go func() { sizes, sizeserr = getSizes(cfg) @@ -840,13 +840,16 @@ func absJoin(dir string, fileses ...[]string) (res []string) { } func golistargs(cfg *Config, words []string) []string { - const findFlags = NeedImports | NeedTypes | NeedSyntax | NeedTypesInfo + const findFlags = NeedImports | NeedTypes | NeedSyntax fullargs := []string{ "list", "-e", "-json", fmt.Sprintf("-compiled=%t", cfg.Mode&(NeedCompiledGoFiles|NeedSyntax|NeedTypesInfo|NeedTypesSizes) != 0), fmt.Sprintf("-test=%t", cfg.Tests), fmt.Sprintf("-export=%t", usesExportData(cfg)), - fmt.Sprintf("-deps=%t", cfg.Mode&NeedImports != 0), + + // Obtain package information about each dependency if needed + fmt.Sprintf("-deps=%t", loadsDeps(cfg)), + // go list doesn't let you pass -test and -find together, // probably because you'd just get the TestMain. fmt.Sprintf("-find=%t", !cfg.Tests && cfg.Mode&findFlags == 0), diff --git a/vendor/golang.org/x/tools/go/packages/packages.go b/vendor/golang.org/x/tools/go/packages/packages.go index 50b93c859bfc..c87babc65965 100644 --- a/vendor/golang.org/x/tools/go/packages/packages.go +++ b/vendor/golang.org/x/tools/go/packages/packages.go @@ -398,12 +398,13 @@ func (p *Package) String() string { return p.ID } // loaderPackage augments Package with state used during the loading phase type loaderPackage struct { *Package - importErrors map[string]error // maps each bad import to its error - loadOnce sync.Once - color uint8 // for cycle detection - needsrc bool // load from source (Mode >= LoadTypes) - needtypes bool // type information is either requested or depended on - initial bool // package was matched by a pattern + importErrors map[string]error // maps each bad import to its error + loadOnce sync.Once + color uint8 // for cycle detection + needsyntax bool // fill syntax trees + needtypes bool // basic type information is either requested or depended on (export data is enough) + needtypesinfo bool // full type information is either requested or depended on (need to load from source) + initial bool // package was matched by a pattern } // loader holds the working state of a single call to load. @@ -500,12 +501,26 @@ func (ld *loader) refine(roots []string, list ...*Package) ([]*Package, error) { if i, found := rootMap[pkg.ID]; found { rootIndex = i } + + // For root packages (rootIndex >= 0) load types if they were requested by NeedTypes. + // For all other packages (dependencies) load types only if types were requested (NeedTypes) for dependecies (NeedDeps). + explicitlyNeedTypes := ld.Mode&NeedTypes != 0 && (rootIndex >= 0 || ld.Mode&NeedDeps != 0) + + explicitlyNeedTypesInfo := ld.Mode&NeedTypesInfo != 0 && (rootIndex >= 0 || // load from source all root packages + ld.Mode&NeedDeps != 0) // load from source all dependencies if needed + hasValidExportData := (pkg.ExportFile != "" || pkg.PkgPath == "unsafe") && + // overlays can invalidate export data. TODO(matloob): make this check fine-grained based on dependencies on overlaid files + len(ld.Overlay) == 0 + needTypesInfo := explicitlyNeedTypesInfo || (ld.Mode&NeedTypes != 0 && !hasValidExportData) + + explicitlyNeedSyntax := ld.Mode&NeedSyntax != 0 && (rootIndex >= 0 || ld.Mode&NeedDeps != 0) + needSyntax := explicitlyNeedSyntax || needTypesInfo // types info loading requires syntax trees building + lpkg := &loaderPackage{ - Package: pkg, - needtypes: (ld.Mode&(NeedTypes|NeedTypesInfo) != 0 && ld.Mode&NeedDeps != 0 && rootIndex < 0) || rootIndex >= 0, - needsrc: (ld.Mode&(NeedSyntax|NeedTypesInfo) != 0 && ld.Mode&NeedDeps != 0 && rootIndex < 0) || rootIndex >= 0 || - len(ld.Overlay) > 0 || // Overlays can invalidate export data. TODO(matloob): make this check fine-grained based on dependencies on overlaid files - pkg.ExportFile == "" && pkg.PkgPath != "unsafe", + Package: pkg, + needtypes: explicitlyNeedTypes, + needtypesinfo: needTypesInfo, + needsyntax: needSyntax, } ld.pkgs[lpkg.ID] = lpkg if rootIndex >= 0 { @@ -519,6 +534,19 @@ func (ld *loader) refine(roots []string, list ...*Package) ([]*Package, error) { } } + // Build loader packages for imported packages when no deeps are needed + if ld.Mode&NeedDeps == 0 { + for _, pkg := range list { + for _, ipkg := range pkg.Imports { + if imp := ld.pkgs[ipkg.ID]; imp == nil { + ld.pkgs[ipkg.ID] = &loaderPackage{ + Package: ipkg, + } + } + } + } + } + // Materialize the import graph. const ( @@ -534,16 +562,16 @@ func (ld *loader) refine(roots []string, list ...*Package) ([]*Package, error) { // Invalid imports (cycles and missing nodes) are saved in the importErrors map. // Thus, even in the presence of both kinds of errors, the Import graph remains a DAG. // - // visit returns whether the package needs src or has a transitive + // visit returns whether the package needs types info or has a transitive // dependency on a package that does. These are the only packages // for which we load source code. var stack []*loaderPackage var visit func(lpkg *loaderPackage) bool - var srcPkgs []*loaderPackage + var typesInfoPkgs []*loaderPackage visit = func(lpkg *loaderPackage) bool { switch lpkg.color { case black: - return lpkg.needsrc + return lpkg.needtypesinfo case grey: panic("internal error: grey node") } @@ -570,14 +598,18 @@ func (ld *loader) refine(roots []string, list ...*Package) ([]*Package, error) { continue } - if visit(imp) { - lpkg.needsrc = true + // If don't need deps, just fill Imports for the root. No need to recurse further. + if loadsDeps(&ld.Config) { + if visit(imp) { + lpkg.needtypesinfo = true + lpkg.needsyntax = true // types info loading (needtypesinfo) requires syntax trees building + } } - lpkg.Imports[importPath] = imp.Package + lpkg.Imports[importPath] = imp.Package // deduplicate imported package } } - if lpkg.needsrc { - srcPkgs = append(srcPkgs, lpkg) + if lpkg.needtypesinfo { + typesInfoPkgs = append(typesInfoPkgs, lpkg) } if ld.Mode&NeedTypesSizes != 0 { lpkg.TypesSizes = ld.sizes @@ -585,7 +617,7 @@ func (ld *loader) refine(roots []string, list ...*Package) ([]*Package, error) { stack = stack[:len(stack)-1] // pop lpkg.color = black - return lpkg.needsrc + return lpkg.needtypesinfo } if ld.Mode&NeedImports == 0 { @@ -599,16 +631,18 @@ func (ld *loader) refine(roots []string, list ...*Package) ([]*Package, error) { visit(lpkg) } } - if ld.Mode&NeedImports != 0 && ld.Mode&NeedTypes != 0 { - for _, lpkg := range srcPkgs { - // Complete type information is required for the - // immediate dependencies of each source package. - for _, ipkg := range lpkg.Imports { - imp := ld.pkgs[ipkg.ID] - imp.needtypes = true - } + + // Set needtypes for immediate dependencies if need types info + for _, lpkg := range typesInfoPkgs { + // Complete type information is required for the + // immediate dependencies of packages for which + // we need types info. + for _, ipkg := range lpkg.Imports { + imp := ld.pkgs[ipkg.ID] + imp.needtypes = true } } + // Load type data if needed, starting at // the initial packages (roots of the import DAG). if ld.Mode&NeedTypes != 0 { @@ -713,13 +747,6 @@ func (ld *loader) loadPackage(lpkg *loaderPackage) { // which would then require that such created packages be explicitly // inserted back into the Import graph as a final step after export data loading. // The Diamond test exercises this case. - if !lpkg.needtypes { - return - } - if !lpkg.needsrc { - ld.loadFromExportData(lpkg) - return // not a source package, don't get syntax trees - } appendError := func(err error) { // Convert various error types into the one true Error. @@ -770,6 +797,36 @@ func (ld *loader) loadPackage(lpkg *loaderPackage) { lpkg.Errors = append(lpkg.Errors, errs...) } + if lpkg.needsyntax { + files, errs := ld.parseFiles(lpkg.CompiledGoFiles) + for _, err := range errs { + appendError(err) + } + + lpkg.Syntax = files + } else if lpkg.needtypesinfo { + log.Fatalf("Internal error: can't load package %s types info without loading syntax trees", lpkg.ID) + } + + if !lpkg.needtypesinfo { + if !lpkg.needtypes { + // Need just syntax trees + return + } + + _, err := ld.loadFromExportData(lpkg) + if err == nil { + // Successfully types loaded from export data + return + } + + log.Fatalf("Failed to load package %s from export data: %s", lpkg.ID, err) + } + + if !lpkg.needtypes { + log.Fatal("Internal error: types will be loaded with types info") + } + if len(lpkg.CompiledGoFiles) == 0 && lpkg.ExportFile != "" { // The config requested loading sources and types, but sources are missing. // Add an error to the package and fall back to loading from export data. @@ -778,13 +835,6 @@ func (ld *loader) loadPackage(lpkg *loaderPackage) { return // can't get syntax trees for this package } - files, errs := ld.parseFiles(lpkg.CompiledGoFiles) - for _, err := range errs { - appendError(err) - } - - lpkg.Syntax = files - lpkg.TypesInfo = &types.Info{ Types: make(map[ast.Expr]types.TypeAndValue), Defs: make(map[*ast.Ident]types.Object), @@ -824,7 +874,7 @@ func (ld *loader) loadPackage(lpkg *loaderPackage) { tc := &types.Config{ Importer: importer, - // Type-check bodies of functions only in non-initial packages. + // Type-check bodies of functions only in initial packages. // Example: for import graph A->B->C and initial packages {A,C}, // we can ignore function bodies in B. IgnoreFuncBodies: ld.Mode&NeedDeps == 0 && !lpkg.initial, @@ -1089,16 +1139,49 @@ func (ld *loader) loadFromExportData(lpkg *loaderPackage) (*types.Package, error return tpkg, nil } +func usesExportData(cfg *Config) bool { + return cfg.Mode&NeedExportsFile != 0 || + // If NeedTypes but not NeedTypesInfo we won't typecheck using sources, so we need export data. + (cfg.Mode&NeedTypes != 0 && cfg.Mode&NeedTypesInfo == 0) || + // If NeedTypesInfo but not NeedDeps, we're typechecking a package using its sources plus its dependencies' export data + (cfg.Mode&NeedTypesInfo != 0 && cfg.Mode&NeedDeps == 0) +} + +func loadsDeps(cfg *Config) bool { + return cfg.Mode&NeedDeps != 0 || + // Immediate dependencies information (at least, export data) is required to do typechecking + // on sources, which is required for the TypesInfo. In such cases we could load packages + // without deps and then call go list again for immediate dependecies, but it's typically + // much slower than running go list -deps=true once. + cfg.Mode&NeedTypesInfo != 0 +} + // impliedLoadMode returns loadMode with it's dependencies func impliedLoadMode(loadMode LoadMode) LoadMode { + if loadMode&NeedTypesInfo != 0 && loadMode&NeedSyntax == 0 { + // When NeedTypesInfo is set we load types info from source code. + // For parsing the source code we need NeedSyntax. + loadMode |= NeedSyntax + } + if loadMode&NeedTypesInfo != 0 && loadMode&NeedImports == 0 { - // If NeedTypesInfo, go/packages needs to do typechecking itself so it can - // associate type info with the AST. To do so, we need the export data - // for dependencies, which means we need to ask for the direct dependencies. - // NeedImports is used to ask for the direct dependencies. + // When NeedTypesInfo is set we load types info from source code. + // We need immediate dependencies types information for that. + // NeedImports handles processing of immediate dependencies. loadMode |= NeedImports } + if loadMode&NeedTypesInfo != 0 && loadMode&NeedTypes == 0 { + // When NeedTypesInfo is set we load types info from source code, + // this procedure also fills types. + loadMode |= NeedTypes + } + + if loadMode&NeedTypesInfo != 0 && loadMode&NeedTypesSizes == 0 { + // Types loading requires types sizes (set in types.Config). + loadMode |= NeedTypesSizes + } + if loadMode&NeedDeps != 0 && loadMode&NeedImports == 0 { // With NeedDeps we need to load at least direct dependencies. // NeedImports is used to ask for the direct dependencies. @@ -1107,7 +1190,3 @@ func impliedLoadMode(loadMode LoadMode) LoadMode { return loadMode } - -func usesExportData(cfg *Config) bool { - return cfg.Mode&NeedExportsFile != 0 || cfg.Mode&NeedTypes != 0 && cfg.Mode&NeedDeps == 0 -} diff --git a/vendor/golang.org/x/tools/go/ssa/func.go b/vendor/golang.org/x/tools/go/ssa/func.go index b21ff4e521ee..0b99bc9ba16b 100644 --- a/vendor/golang.org/x/tools/go/ssa/func.go +++ b/vendor/golang.org/x/tools/go/ssa/func.go @@ -257,6 +257,10 @@ func (f *Function) createSyntacticParams(recv *ast.FieldList, functype *ast.Func } } +type setNumable interface { + setNum(int) +} + // numberRegisters assigns numbers to all SSA registers // (value-defining Instructions) in f, to aid debugging. // (Non-Instruction Values are named at construction.) @@ -267,9 +271,7 @@ func numberRegisters(f *Function) { for _, instr := range b.Instrs { switch instr.(type) { case Value: - instr.(interface { - setNum(int) - }).setNum(v) + instr.(setNumable).setNum(v) v++ } } diff --git a/vendor/golang.org/x/tools/go/types/objectpath/objectpath.go b/vendor/golang.org/x/tools/go/types/objectpath/objectpath.go new file mode 100644 index 000000000000..882e3b3d8a96 --- /dev/null +++ b/vendor/golang.org/x/tools/go/types/objectpath/objectpath.go @@ -0,0 +1,523 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package objectpath defines a naming scheme for types.Objects +// (that is, named entities in Go programs) relative to their enclosing +// package. +// +// Type-checker objects are canonical, so they are usually identified by +// their address in memory (a pointer), but a pointer has meaning only +// within one address space. By contrast, objectpath names allow the +// identity of an object to be sent from one program to another, +// establishing a correspondence between types.Object variables that are +// distinct but logically equivalent. +// +// A single object may have multiple paths. In this example, +// type A struct{ X int } +// type B A +// the field X has two paths due to its membership of both A and B. +// The For(obj) function always returns one of these paths, arbitrarily +// but consistently. +package objectpath + +import ( + "fmt" + "strconv" + "strings" + + "go/types" +) + +// A Path is an opaque name that identifies a types.Object +// relative to its package. Conceptually, the name consists of a +// sequence of destructuring operations applied to the package scope +// to obtain the original object. +// The name does not include the package itself. +type Path string + +// Encoding +// +// An object path is a textual and (with training) human-readable encoding +// of a sequence of destructuring operators, starting from a types.Package. +// The sequences represent a path through the package/object/type graph. +// We classify these operators by their type: +// +// PO package->object Package.Scope.Lookup +// OT object->type Object.Type +// TT type->type Type.{Elem,Key,Params,Results,Underlying} [EKPRU] +// TO type->object Type.{At,Field,Method,Obj} [AFMO] +// +// All valid paths start with a package and end at an object +// and thus may be defined by the regular language: +// +// objectpath = PO (OT TT* TO)* +// +// The concrete encoding follows directly: +// - The only PO operator is Package.Scope.Lookup, which requires an identifier. +// - The only OT operator is Object.Type, +// which we encode as '.' because dot cannot appear in an identifier. +// - The TT operators are encoded as [EKPRU]. +// - The OT operators are encoded as [AFMO]; +// three of these (At,Field,Method) require an integer operand, +// which is encoded as a string of decimal digits. +// These indices are stable across different representations +// of the same package, even source and export data. +// +// In the example below, +// +// package p +// +// type T interface { +// f() (a string, b struct{ X int }) +// } +// +// field X has the path "T.UM0.RA1.F0", +// representing the following sequence of operations: +// +// p.Lookup("T") T +// .Type().Underlying().Method(0). f +// .Type().Results().At(1) b +// .Type().Field(0) X +// +// The encoding is not maximally compact---every R or P is +// followed by an A, for example---but this simplifies the +// encoder and decoder. +// +const ( + // object->type operators + opType = '.' // .Type() (Object) + + // type->type operators + opElem = 'E' // .Elem() (Pointer, Slice, Array, Chan, Map) + opKey = 'K' // .Key() (Map) + opParams = 'P' // .Params() (Signature) + opResults = 'R' // .Results() (Signature) + opUnderlying = 'U' // .Underlying() (Named) + + // type->object operators + opAt = 'A' // .At(i) (Tuple) + opField = 'F' // .Field(i) (Struct) + opMethod = 'M' // .Method(i) (Named or Interface; not Struct: "promoted" names are ignored) + opObj = 'O' // .Obj() (Named) +) + +// The For function returns the path to an object relative to its package, +// or an error if the object is not accessible from the package's Scope. +// +// The For function guarantees to return a path only for the following objects: +// - package-level types +// - exported package-level non-types +// - methods +// - parameter and result variables +// - struct fields +// These objects are sufficient to define the API of their package. +// The objects described by a package's export data are drawn from this set. +// +// For does not return a path for predeclared names, imported package +// names, local names, and unexported package-level names (except +// types). +// +// Example: given this definition, +// +// package p +// +// type T interface { +// f() (a string, b struct{ X int }) +// } +// +// For(X) would return a path that denotes the following sequence of operations: +// +// p.Scope().Lookup("T") (TypeName T) +// .Type().Underlying().Method(0). (method Func f) +// .Type().Results().At(1) (field Var b) +// .Type().Field(0) (field Var X) +// +// where p is the package (*types.Package) to which X belongs. +func For(obj types.Object) (Path, error) { + pkg := obj.Pkg() + + // This table lists the cases of interest. + // + // Object Action + // ------ ------ + // nil reject + // builtin reject + // pkgname reject + // label reject + // var + // package-level accept + // func param/result accept + // local reject + // struct field accept + // const + // package-level accept + // local reject + // func + // package-level accept + // init functions reject + // concrete method accept + // interface method accept + // type + // package-level accept + // local reject + // + // The only accessible package-level objects are members of pkg itself. + // + // The cases are handled in four steps: + // + // 1. reject nil and builtin + // 2. accept package-level objects + // 3. reject obviously invalid objects + // 4. search the API for the path to the param/result/field/method. + + // 1. reference to nil or builtin? + if pkg == nil { + return "", fmt.Errorf("predeclared %s has no path", obj) + } + scope := pkg.Scope() + + // 2. package-level object? + if scope.Lookup(obj.Name()) == obj { + // Only exported objects (and non-exported types) have a path. + // Non-exported types may be referenced by other objects. + if _, ok := obj.(*types.TypeName); !ok && !obj.Exported() { + return "", fmt.Errorf("no path for non-exported %v", obj) + } + return Path(obj.Name()), nil + } + + // 3. Not a package-level object. + // Reject obviously non-viable cases. + switch obj := obj.(type) { + case *types.Const, // Only package-level constants have a path. + *types.TypeName, // Only package-level types have a path. + *types.Label, // Labels are function-local. + *types.PkgName: // PkgNames are file-local. + return "", fmt.Errorf("no path for %v", obj) + + case *types.Var: + // Could be: + // - a field (obj.IsField()) + // - a func parameter or result + // - a local var. + // Sadly there is no way to distinguish + // a param/result from a local + // so we must proceed to the find. + + case *types.Func: + // A func, if not package-level, must be a method. + if recv := obj.Type().(*types.Signature).Recv(); recv == nil { + return "", fmt.Errorf("func is not a method: %v", obj) + } + // TODO(adonovan): opt: if the method is concrete, + // do a specialized version of the rest of this function so + // that it's O(1) not O(|scope|). Basically 'find' is needed + // only for struct fields and interface methods. + + default: + panic(obj) + } + + // 4. Search the API for the path to the var (field/param/result) or method. + + // First inspect package-level named types. + // In the presence of path aliases, these give + // the best paths because non-types may + // refer to types, but not the reverse. + empty := make([]byte, 0, 48) // initial space + for _, name := range scope.Names() { + o := scope.Lookup(name) + tname, ok := o.(*types.TypeName) + if !ok { + continue // handle non-types in second pass + } + + path := append(empty, name...) + path = append(path, opType) + + T := o.Type() + + if tname.IsAlias() { + // type alias + if r := find(obj, T, path); r != nil { + return Path(r), nil + } + } else { + // defined (named) type + if r := find(obj, T.Underlying(), append(path, opUnderlying)); r != nil { + return Path(r), nil + } + } + } + + // Then inspect everything else: + // non-types, and declared methods of defined types. + for _, name := range scope.Names() { + o := scope.Lookup(name) + path := append(empty, name...) + if _, ok := o.(*types.TypeName); !ok { + if o.Exported() { + // exported non-type (const, var, func) + if r := find(obj, o.Type(), append(path, opType)); r != nil { + return Path(r), nil + } + } + continue + } + + // Inspect declared methods of defined types. + if T, ok := o.Type().(*types.Named); ok { + path = append(path, opType) + for i := 0; i < T.NumMethods(); i++ { + m := T.Method(i) + path2 := appendOpArg(path, opMethod, i) + if m == obj { + return Path(path2), nil // found declared method + } + if r := find(obj, m.Type(), append(path2, opType)); r != nil { + return Path(r), nil + } + } + } + } + + return "", fmt.Errorf("can't find path for %v in %s", obj, pkg.Path()) +} + +func appendOpArg(path []byte, op byte, arg int) []byte { + path = append(path, op) + path = strconv.AppendInt(path, int64(arg), 10) + return path +} + +// find finds obj within type T, returning the path to it, or nil if not found. +func find(obj types.Object, T types.Type, path []byte) []byte { + switch T := T.(type) { + case *types.Basic, *types.Named: + // Named types belonging to pkg were handled already, + // so T must belong to another package. No path. + return nil + case *types.Pointer: + return find(obj, T.Elem(), append(path, opElem)) + case *types.Slice: + return find(obj, T.Elem(), append(path, opElem)) + case *types.Array: + return find(obj, T.Elem(), append(path, opElem)) + case *types.Chan: + return find(obj, T.Elem(), append(path, opElem)) + case *types.Map: + if r := find(obj, T.Key(), append(path, opKey)); r != nil { + return r + } + return find(obj, T.Elem(), append(path, opElem)) + case *types.Signature: + if r := find(obj, T.Params(), append(path, opParams)); r != nil { + return r + } + return find(obj, T.Results(), append(path, opResults)) + case *types.Struct: + for i := 0; i < T.NumFields(); i++ { + f := T.Field(i) + path2 := appendOpArg(path, opField, i) + if f == obj { + return path2 // found field var + } + if r := find(obj, f.Type(), append(path2, opType)); r != nil { + return r + } + } + return nil + case *types.Tuple: + for i := 0; i < T.Len(); i++ { + v := T.At(i) + path2 := appendOpArg(path, opAt, i) + if v == obj { + return path2 // found param/result var + } + if r := find(obj, v.Type(), append(path2, opType)); r != nil { + return r + } + } + return nil + case *types.Interface: + for i := 0; i < T.NumMethods(); i++ { + m := T.Method(i) + path2 := appendOpArg(path, opMethod, i) + if m == obj { + return path2 // found interface method + } + if r := find(obj, m.Type(), append(path2, opType)); r != nil { + return r + } + } + return nil + } + panic(T) +} + +// Object returns the object denoted by path p within the package pkg. +func Object(pkg *types.Package, p Path) (types.Object, error) { + if p == "" { + return nil, fmt.Errorf("empty path") + } + + pathstr := string(p) + var pkgobj, suffix string + if dot := strings.IndexByte(pathstr, opType); dot < 0 { + pkgobj = pathstr + } else { + pkgobj = pathstr[:dot] + suffix = pathstr[dot:] // suffix starts with "." + } + + obj := pkg.Scope().Lookup(pkgobj) + if obj == nil { + return nil, fmt.Errorf("package %s does not contain %q", pkg.Path(), pkgobj) + } + + // abstraction of *types.{Pointer,Slice,Array,Chan,Map} + type hasElem interface { + Elem() types.Type + } + // abstraction of *types.{Interface,Named} + type hasMethods interface { + Method(int) *types.Func + NumMethods() int + } + + // The loop state is the pair (t, obj), + // exactly one of which is non-nil, initially obj. + // All suffixes start with '.' (the only object->type operation), + // followed by optional type->type operations, + // then a type->object operation. + // The cycle then repeats. + var t types.Type + for suffix != "" { + code := suffix[0] + suffix = suffix[1:] + + // Codes [AFM] have an integer operand. + var index int + switch code { + case opAt, opField, opMethod: + rest := strings.TrimLeft(suffix, "0123456789") + numerals := suffix[:len(suffix)-len(rest)] + suffix = rest + i, err := strconv.Atoi(numerals) + if err != nil { + return nil, fmt.Errorf("invalid path: bad numeric operand %q for code %q", numerals, code) + } + index = int(i) + case opObj: + // no operand + default: + // The suffix must end with a type->object operation. + if suffix == "" { + return nil, fmt.Errorf("invalid path: ends with %q, want [AFMO]", code) + } + } + + if code == opType { + if t != nil { + return nil, fmt.Errorf("invalid path: unexpected %q in type context", opType) + } + t = obj.Type() + obj = nil + continue + } + + if t == nil { + return nil, fmt.Errorf("invalid path: code %q in object context", code) + } + + // Inv: t != nil, obj == nil + + switch code { + case opElem: + hasElem, ok := t.(hasElem) // Pointer, Slice, Array, Chan, Map + if !ok { + return nil, fmt.Errorf("cannot apply %q to %s (got %T, want pointer, slice, array, chan or map)", code, t, t) + } + t = hasElem.Elem() + + case opKey: + mapType, ok := t.(*types.Map) + if !ok { + return nil, fmt.Errorf("cannot apply %q to %s (got %T, want map)", code, t, t) + } + t = mapType.Key() + + case opParams: + sig, ok := t.(*types.Signature) + if !ok { + return nil, fmt.Errorf("cannot apply %q to %s (got %T, want signature)", code, t, t) + } + t = sig.Params() + + case opResults: + sig, ok := t.(*types.Signature) + if !ok { + return nil, fmt.Errorf("cannot apply %q to %s (got %T, want signature)", code, t, t) + } + t = sig.Results() + + case opUnderlying: + named, ok := t.(*types.Named) + if !ok { + return nil, fmt.Errorf("cannot apply %q to %s (got %s, want named)", code, t, t) + } + t = named.Underlying() + + case opAt: + tuple, ok := t.(*types.Tuple) + if !ok { + return nil, fmt.Errorf("cannot apply %q to %s (got %s, want tuple)", code, t, t) + } + if n := tuple.Len(); index >= n { + return nil, fmt.Errorf("tuple index %d out of range [0-%d)", index, n) + } + obj = tuple.At(index) + t = nil + + case opField: + structType, ok := t.(*types.Struct) + if !ok { + return nil, fmt.Errorf("cannot apply %q to %s (got %T, want struct)", code, t, t) + } + if n := structType.NumFields(); index >= n { + return nil, fmt.Errorf("field index %d out of range [0-%d)", index, n) + } + obj = structType.Field(index) + t = nil + + case opMethod: + hasMethods, ok := t.(hasMethods) // Interface or Named + if !ok { + return nil, fmt.Errorf("cannot apply %q to %s (got %s, want interface or named)", code, t, t) + } + if n := hasMethods.NumMethods(); index >= n { + return nil, fmt.Errorf("method index %d out of range [0-%d)", index, n) + } + obj = hasMethods.Method(index) + t = nil + + case opObj: + named, ok := t.(*types.Named) + if !ok { + return nil, fmt.Errorf("cannot apply %q to %s (got %s, want named)", code, t, t) + } + obj = named.Obj() + t = nil + + default: + return nil, fmt.Errorf("invalid path: unknown code %q", code) + } + } + + if obj.Pkg() != pkg { + return nil, fmt.Errorf("path denotes %s, which belongs to a different package", obj) + } + + return obj, nil // success +} diff --git a/vendor/github.com/golangci/go-tools/LICENSE b/vendor/honnef.co/go/tools/LICENSE similarity index 100% rename from vendor/github.com/golangci/go-tools/LICENSE rename to vendor/honnef.co/go/tools/LICENSE diff --git a/vendor/honnef.co/go/tools/LICENSE-THIRD-PARTY b/vendor/honnef.co/go/tools/LICENSE-THIRD-PARTY new file mode 100644 index 000000000000..7c241b71aef3 --- /dev/null +++ b/vendor/honnef.co/go/tools/LICENSE-THIRD-PARTY @@ -0,0 +1,226 @@ +Staticcheck and its related tools make use of third party projects, +either by reusing their code, or by statically linking them into +resulting binaries. These projects are: + +* The Go Programming Language - https://golang.org/ + + Copyright (c) 2009 The Go Authors. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + * Neither the name of Google Inc. nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +* github.com/BurntSushi/toml - https://github.com/BurntSushi/toml + + The MIT License (MIT) + + Copyright (c) 2013 TOML authors + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + + +* github.com/google/renameio - https://github.com/google/renameio + + Copyright 2018 Google Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + +* github.com/kisielk/gotool – https://github.com/kisielk/gotool + + Copyright (c) 2013 Kamil Kisiel + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + All the files in this distribution are covered under either the MIT + license (see the file LICENSE) except some files mentioned below. + + match.go, match_test.go: + + Copyright (c) 2009 The Go Authors. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + * Neither the name of Google Inc. nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +* github.com/rogpeppe/go-internal - https://github.com/rogpeppe/go-internal + + Copyright (c) 2018 The Go Authors. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + * Neither the name of Google Inc. nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +* golang.org/x/mod/module - https://github.com/golang/mod + + Copyright (c) 2009 The Go Authors. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + * Neither the name of Google Inc. nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +* golang.org/x/tools/go/analysis - https://github.com/golang/tools + + Copyright (c) 2009 The Go Authors. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + * Neither the name of Google Inc. nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + diff --git a/vendor/github.com/golangci/go-tools/arg/arg.go b/vendor/honnef.co/go/tools/arg/arg.go similarity index 77% rename from vendor/github.com/golangci/go-tools/arg/arg.go rename to vendor/honnef.co/go/tools/arg/arg.go index d9e42dbea7cd..1e7f30db42d4 100644 --- a/vendor/github.com/golangci/go-tools/arg/arg.go +++ b/vendor/honnef.co/go/tools/arg/arg.go @@ -1,6 +1,10 @@ package arg var args = map[string]int{ + "(*encoding/json.Decoder).Decode.v": 0, + "(*encoding/json.Encoder).Encode.v": 0, + "(*encoding/xml.Decoder).Decode.v": 0, + "(*encoding/xml.Encoder).Encode.v": 0, "(*sync.Pool).Put.x": 0, "(*text/template.Template).Parse.text": 0, "(io.Seeker).Seek.offset": 0, @@ -11,9 +15,12 @@ var args = map[string]int{ "bytes.Equal.b": 1, "encoding/binary.Write.data": 2, "errors.New.text": 0, + "fmt.Fprintf.format": 1, "fmt.Printf.format": 0, "fmt.Sprintf.a[0]": 1, "fmt.Sprintf.format": 0, + "json.Marshal.v": 0, + "json.Unmarshal.v": 1, "len.v": 0, "make.size[0]": 1, "make.size[1]": 2, @@ -28,6 +35,8 @@ var args = map[string]int{ "sort.Sort.data": 0, "time.Parse.layout": 0, "time.Sleep.d": 0, + "xml.Marshal.v": 0, + "xml.Unmarshal.v": 1, } func Arg(name string) int { diff --git a/vendor/github.com/golangci/go-tools/config/config.go b/vendor/honnef.co/go/tools/config/config.go similarity index 70% rename from vendor/github.com/golangci/go-tools/config/config.go rename to vendor/honnef.co/go/tools/config/config.go index 112980b498dd..c22093a6d982 100644 --- a/vendor/github.com/golangci/go-tools/config/config.go +++ b/vendor/honnef.co/go/tools/config/config.go @@ -1,12 +1,63 @@ package config import ( + "bytes" + "fmt" "os" "path/filepath" + "reflect" + "strings" "github.com/BurntSushi/toml" + "golang.org/x/tools/go/analysis" ) +var Analyzer = &analysis.Analyzer{ + Name: "config", + Doc: "loads configuration for the current package tree", + Run: func(pass *analysis.Pass) (interface{}, error) { + if len(pass.Files) == 0 { + cfg := DefaultConfig + return &cfg, nil + } + cache, err := os.UserCacheDir() + if err != nil { + cache = "" + } + var path string + for _, f := range pass.Files { + p := pass.Fset.PositionFor(f.Pos(), true).Filename + // FIXME(dh): using strings.HasPrefix isn't technically + // correct, but it should be good enough for now. + if cache != "" && strings.HasPrefix(p, cache) { + // File in the build cache of the standard Go build system + continue + } + path = p + break + } + + if path == "" { + // The package only consists of generated files. + cfg := DefaultConfig + return &cfg, nil + } + + dir := filepath.Dir(path) + cfg, err := Load(dir) + if err != nil { + return nil, fmt.Errorf("error loading staticcheck.conf: %s", err) + } + return &cfg, nil + }, + RunDespiteErrors: true, + ResultType: reflect.TypeOf((*Config)(nil)), +} + +func For(pass *analysis.Pass) *Config { + return pass.ResultOf[Analyzer].(*Config) +} + func mergeLists(a, b []string) []string { out := make([]string, 0, len(a)+len(b)) for _, el := range b { @@ -73,7 +124,18 @@ type Config struct { HTTPStatusCodeWhitelist []string `toml:"http_status_code_whitelist"` } -var defaultConfig = Config{ +func (c Config) String() string { + buf := &bytes.Buffer{} + + fmt.Fprintf(buf, "Checks: %#v\n", c.Checks) + fmt.Fprintf(buf, "Initialisms: %#v\n", c.Initialisms) + fmt.Fprintf(buf, "DotImportWhitelist: %#v\n", c.DotImportWhitelist) + fmt.Fprintf(buf, "HTTPStatusCodeWhitelist: %#v", c.HTTPStatusCodeWhitelist) + + return buf.String() +} + +var DefaultConfig = Config{ Checks: []string{"all", "-ST1000", "-ST1003", "-ST1016"}, Initialisms: []string{ "ACL", "API", "ASCII", "CPU", "CSS", "DNS", @@ -82,7 +144,7 @@ var defaultConfig = Config{ "SMTP", "SQL", "SSH", "TCP", "TLS", "TTL", "UDP", "UI", "GID", "UID", "UUID", "URI", "URL", "UTF8", "VM", "XML", "XMPP", "XSRF", - "XSS", + "XSS", "SIP", "RTP", }, DotImportWhitelist: []string{}, HTTPStatusCodeWhitelist: []string{"200", "400", "404", "500"}, @@ -120,7 +182,7 @@ func parseConfigs(dir string) ([]Config, error) { } dir = ndir } - out = append(out, defaultConfig) + out = append(out, DefaultConfig) if len(out) < 2 { return out, nil } diff --git a/vendor/github.com/golangci/go-tools/config/example.conf b/vendor/honnef.co/go/tools/config/example.conf similarity index 94% rename from vendor/github.com/golangci/go-tools/config/example.conf rename to vendor/honnef.co/go/tools/config/example.conf index 5ffc597f9964..a715a24d4fcf 100644 --- a/vendor/github.com/golangci/go-tools/config/example.conf +++ b/vendor/honnef.co/go/tools/config/example.conf @@ -5,6 +5,6 @@ initialisms = ["ACL", "API", "ASCII", "CPU", "CSS", "DNS", "SMTP", "SQL", "SSH", "TCP", "TLS", "TTL", "UDP", "UI", "GID", "UID", "UUID", "URI", "URL", "UTF8", "VM", "XML", "XMPP", "XSRF", - "XSS"] + "XSS", "SIP", "RTP"] dot_import_whitelist = [] http_status_code_whitelist = ["200", "400", "404", "500"] diff --git a/vendor/honnef.co/go/tools/deprecated/stdlib.go b/vendor/honnef.co/go/tools/deprecated/stdlib.go new file mode 100644 index 000000000000..5d8ce186b160 --- /dev/null +++ b/vendor/honnef.co/go/tools/deprecated/stdlib.go @@ -0,0 +1,112 @@ +package deprecated + +type Deprecation struct { + DeprecatedSince int + AlternativeAvailableSince int +} + +var Stdlib = map[string]Deprecation{ + "image/jpeg.Reader": {4, 0}, + // FIXME(dh): AllowBinary isn't being detected as deprecated + // because the comment has a newline right after "Deprecated:" + "go/build.AllowBinary": {7, 7}, + "(archive/zip.FileHeader).CompressedSize": {1, 1}, + "(archive/zip.FileHeader).UncompressedSize": {1, 1}, + "(archive/zip.FileHeader).ModifiedTime": {10, 10}, + "(archive/zip.FileHeader).ModifiedDate": {10, 10}, + "(*archive/zip.FileHeader).ModTime": {10, 10}, + "(*archive/zip.FileHeader).SetModTime": {10, 10}, + "(go/doc.Package).Bugs": {1, 1}, + "os.SEEK_SET": {7, 7}, + "os.SEEK_CUR": {7, 7}, + "os.SEEK_END": {7, 7}, + "(net.Dialer).Cancel": {7, 7}, + "runtime.CPUProfile": {9, 0}, + "compress/flate.ReadError": {6, 6}, + "compress/flate.WriteError": {6, 6}, + "path/filepath.HasPrefix": {0, 0}, + "(net/http.Transport).Dial": {7, 7}, + "(*net/http.Transport).CancelRequest": {6, 5}, + "net/http.ErrWriteAfterFlush": {7, 0}, + "net/http.ErrHeaderTooLong": {8, 0}, + "net/http.ErrShortBody": {8, 0}, + "net/http.ErrMissingContentLength": {8, 0}, + "net/http/httputil.ErrPersistEOF": {0, 0}, + "net/http/httputil.ErrClosed": {0, 0}, + "net/http/httputil.ErrPipeline": {0, 0}, + "net/http/httputil.ServerConn": {0, 0}, + "net/http/httputil.NewServerConn": {0, 0}, + "net/http/httputil.ClientConn": {0, 0}, + "net/http/httputil.NewClientConn": {0, 0}, + "net/http/httputil.NewProxyClientConn": {0, 0}, + "(net/http.Request).Cancel": {7, 7}, + "(text/template/parse.PipeNode).Line": {1, 1}, + "(text/template/parse.ActionNode).Line": {1, 1}, + "(text/template/parse.BranchNode).Line": {1, 1}, + "(text/template/parse.TemplateNode).Line": {1, 1}, + "database/sql/driver.ColumnConverter": {9, 9}, + "database/sql/driver.Execer": {8, 8}, + "database/sql/driver.Queryer": {8, 8}, + "(database/sql/driver.Conn).Begin": {8, 8}, + "(database/sql/driver.Stmt).Exec": {8, 8}, + "(database/sql/driver.Stmt).Query": {8, 8}, + "syscall.StringByteSlice": {1, 1}, + "syscall.StringBytePtr": {1, 1}, + "syscall.StringSlicePtr": {1, 1}, + "syscall.StringToUTF16": {1, 1}, + "syscall.StringToUTF16Ptr": {1, 1}, + "(*regexp.Regexp).Copy": {12, 12}, + "(archive/tar.Header).Xattrs": {10, 10}, + "archive/tar.TypeRegA": {11, 1}, + "go/types.NewInterface": {11, 11}, + "(*go/types.Interface).Embedded": {11, 11}, + "go/importer.For": {12, 12}, + "encoding/json.InvalidUTF8Error": {2, 2}, + "encoding/json.UnmarshalFieldError": {2, 2}, + "encoding/csv.ErrTrailingComma": {2, 2}, + "(encoding/csv.Reader).TrailingComma": {2, 2}, + "(net.Dialer).DualStack": {12, 12}, + "net/http.ErrUnexpectedTrailer": {12, 12}, + "net/http.CloseNotifier": {11, 7}, + "net/http.ProtocolError": {8, 8}, + "(crypto/x509.CertificateRequest).Attributes": {5, 3}, + // This function has no alternative, but also no purpose. + "(*crypto/rc4.Cipher).Reset": {12, 0}, + "(net/http/httptest.ResponseRecorder).HeaderMap": {11, 7}, + + // All of these have been deprecated in favour of external libraries + "syscall.AttachLsf": {7, 0}, + "syscall.DetachLsf": {7, 0}, + "syscall.LsfSocket": {7, 0}, + "syscall.SetLsfPromisc": {7, 0}, + "syscall.LsfJump": {7, 0}, + "syscall.LsfStmt": {7, 0}, + "syscall.BpfStmt": {7, 0}, + "syscall.BpfJump": {7, 0}, + "syscall.BpfBuflen": {7, 0}, + "syscall.SetBpfBuflen": {7, 0}, + "syscall.BpfDatalink": {7, 0}, + "syscall.SetBpfDatalink": {7, 0}, + "syscall.SetBpfPromisc": {7, 0}, + "syscall.FlushBpf": {7, 0}, + "syscall.BpfInterface": {7, 0}, + "syscall.SetBpfInterface": {7, 0}, + "syscall.BpfTimeout": {7, 0}, + "syscall.SetBpfTimeout": {7, 0}, + "syscall.BpfStats": {7, 0}, + "syscall.SetBpfImmediate": {7, 0}, + "syscall.SetBpf": {7, 0}, + "syscall.CheckBpfVersion": {7, 0}, + "syscall.BpfHeadercmpl": {7, 0}, + "syscall.SetBpfHeadercmpl": {7, 0}, + "syscall.RouteRIB": {8, 0}, + "syscall.RoutingMessage": {8, 0}, + "syscall.RouteMessage": {8, 0}, + "syscall.InterfaceMessage": {8, 0}, + "syscall.InterfaceAddrMessage": {8, 0}, + "syscall.ParseRoutingMessage": {8, 0}, + "syscall.ParseRoutingSockaddr": {8, 0}, + "InterfaceAnnounceMessage": {7, 0}, + "InterfaceMulticastAddrMessage": {7, 0}, + "syscall.FormatMessage": {5, 0}, +} diff --git a/vendor/honnef.co/go/tools/facts/deprecated.go b/vendor/honnef.co/go/tools/facts/deprecated.go new file mode 100644 index 000000000000..8587b0e0eaeb --- /dev/null +++ b/vendor/honnef.co/go/tools/facts/deprecated.go @@ -0,0 +1,144 @@ +package facts + +import ( + "go/ast" + "go/token" + "go/types" + "reflect" + "strings" + + "golang.org/x/tools/go/analysis" +) + +type IsDeprecated struct{ Msg string } + +func (*IsDeprecated) AFact() {} +func (d *IsDeprecated) String() string { return "Deprecated: " + d.Msg } + +type DeprecatedResult struct { + Objects map[types.Object]*IsDeprecated + Packages map[*types.Package]*IsDeprecated +} + +var Deprecated = &analysis.Analyzer{ + Name: "fact_deprecated", + Doc: "Mark deprecated objects", + Run: deprecated, + FactTypes: []analysis.Fact{(*IsDeprecated)(nil)}, + ResultType: reflect.TypeOf(DeprecatedResult{}), +} + +func deprecated(pass *analysis.Pass) (interface{}, error) { + var names []*ast.Ident + + extractDeprecatedMessage := func(docs []*ast.CommentGroup) string { + for _, doc := range docs { + if doc == nil { + continue + } + parts := strings.Split(doc.Text(), "\n\n") + last := parts[len(parts)-1] + if !strings.HasPrefix(last, "Deprecated: ") { + continue + } + alt := last[len("Deprecated: "):] + alt = strings.Replace(alt, "\n", " ", -1) + return alt + } + return "" + } + doDocs := func(names []*ast.Ident, docs []*ast.CommentGroup) { + alt := extractDeprecatedMessage(docs) + if alt == "" { + return + } + + for _, name := range names { + obj := pass.TypesInfo.ObjectOf(name) + pass.ExportObjectFact(obj, &IsDeprecated{alt}) + } + } + + var docs []*ast.CommentGroup + for _, f := range pass.Files { + docs = append(docs, f.Doc) + } + if alt := extractDeprecatedMessage(docs); alt != "" { + // Don't mark package syscall as deprecated, even though + // it is. A lot of people still use it for simple + // constants like SIGKILL, and I am not comfortable + // telling them to use x/sys for that. + if pass.Pkg.Path() != "syscall" { + pass.ExportPackageFact(&IsDeprecated{alt}) + } + } + + docs = docs[:0] + for _, f := range pass.Files { + fn := func(node ast.Node) bool { + if node == nil { + return true + } + var ret bool + switch node := node.(type) { + case *ast.GenDecl: + switch node.Tok { + case token.TYPE, token.CONST, token.VAR: + docs = append(docs, node.Doc) + return true + default: + return false + } + case *ast.FuncDecl: + docs = append(docs, node.Doc) + names = []*ast.Ident{node.Name} + ret = false + case *ast.TypeSpec: + docs = append(docs, node.Doc) + names = []*ast.Ident{node.Name} + ret = true + case *ast.ValueSpec: + docs = append(docs, node.Doc) + names = node.Names + ret = false + case *ast.File: + return true + case *ast.StructType: + for _, field := range node.Fields.List { + doDocs(field.Names, []*ast.CommentGroup{field.Doc}) + } + return false + case *ast.InterfaceType: + for _, field := range node.Methods.List { + doDocs(field.Names, []*ast.CommentGroup{field.Doc}) + } + return false + default: + return false + } + if len(names) == 0 || len(docs) == 0 { + return ret + } + doDocs(names, docs) + + docs = docs[:0] + names = nil + return ret + } + ast.Inspect(f, fn) + } + + out := DeprecatedResult{ + Objects: map[types.Object]*IsDeprecated{}, + Packages: map[*types.Package]*IsDeprecated{}, + } + + for _, fact := range pass.AllObjectFacts() { + out.Objects[fact.Object] = fact.Fact.(*IsDeprecated) + } + for _, fact := range pass.AllPackageFacts() { + out.Packages[fact.Package] = fact.Fact.(*IsDeprecated) + } + + return out, nil +} diff --git a/vendor/honnef.co/go/tools/facts/generated.go b/vendor/honnef.co/go/tools/facts/generated.go new file mode 100644 index 000000000000..1ed9563a30be --- /dev/null +++ b/vendor/honnef.co/go/tools/facts/generated.go @@ -0,0 +1,86 @@ +package facts + +import ( + "bufio" + "bytes" + "io" + "os" + "reflect" + "strings" + + "golang.org/x/tools/go/analysis" +) + +type Generator int + +// A list of known generators we can detect +const ( + Unknown Generator = iota + Goyacc + Cgo + Stringer +) + +var ( + // used by cgo before Go 1.11 + oldCgo = []byte("// Created by cgo - DO NOT EDIT") + prefix = []byte("// Code generated ") + suffix = []byte(" DO NOT EDIT.") + nl = []byte("\n") + crnl = []byte("\r\n") +) + +func isGenerated(path string) (Generator, bool) { + f, err := os.Open(path) + if err != nil { + return 0, false + } + defer f.Close() + br := bufio.NewReader(f) + for { + s, err := br.ReadBytes('\n') + if err != nil && err != io.EOF { + return 0, false + } + s = bytes.TrimSuffix(s, crnl) + s = bytes.TrimSuffix(s, nl) + if bytes.HasPrefix(s, prefix) && bytes.HasSuffix(s, suffix) { + text := string(s[len(prefix) : len(s)-len(suffix)]) + switch text { + case "by goyacc.": + return Goyacc, true + case "by cmd/cgo;": + return Cgo, true + } + if strings.HasPrefix(text, `by "stringer `) { + return Stringer, true + } + return Unknown, true + } + if bytes.Equal(s, oldCgo) { + return Cgo, true + } + if err == io.EOF { + break + } + } + return 0, false +} + +var Generated = &analysis.Analyzer{ + Name: "isgenerated", + Doc: "annotate file names that have been code generated", + Run: func(pass *analysis.Pass) (interface{}, error) { + m := map[string]Generator{} + for _, f := range pass.Files { + path := pass.Fset.PositionFor(f.Pos(), false).Filename + g, ok := isGenerated(path) + if ok { + m[path] = g + } + } + return m, nil + }, + RunDespiteErrors: true, + ResultType: reflect.TypeOf(map[string]Generator{}), +} diff --git a/vendor/honnef.co/go/tools/facts/purity.go b/vendor/honnef.co/go/tools/facts/purity.go new file mode 100644 index 000000000000..861ca41104ac --- /dev/null +++ b/vendor/honnef.co/go/tools/facts/purity.go @@ -0,0 +1,175 @@ +package facts + +import ( + "go/token" + "go/types" + "reflect" + + "golang.org/x/tools/go/analysis" + "honnef.co/go/tools/functions" + "honnef.co/go/tools/internal/passes/buildssa" + "honnef.co/go/tools/ssa" +) + +type IsPure struct{} + +func (*IsPure) AFact() {} +func (d *IsPure) String() string { return "is pure" } + +type PurityResult map[*types.Func]*IsPure + +var Purity = &analysis.Analyzer{ + Name: "fact_purity", + Doc: "Mark pure functions", + Run: purity, + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + FactTypes: []analysis.Fact{(*IsPure)(nil)}, + ResultType: reflect.TypeOf(PurityResult{}), +} + +var pureStdlib = map[string]struct{}{ + "errors.New": {}, + "fmt.Errorf": {}, + "fmt.Sprintf": {}, + "fmt.Sprint": {}, + "sort.Reverse": {}, + "strings.Map": {}, + "strings.Repeat": {}, + "strings.Replace": {}, + "strings.Title": {}, + "strings.ToLower": {}, + "strings.ToLowerSpecial": {}, + "strings.ToTitle": {}, + "strings.ToTitleSpecial": {}, + "strings.ToUpper": {}, + "strings.ToUpperSpecial": {}, + "strings.Trim": {}, + "strings.TrimFunc": {}, + "strings.TrimLeft": {}, + "strings.TrimLeftFunc": {}, + "strings.TrimPrefix": {}, + "strings.TrimRight": {}, + "strings.TrimRightFunc": {}, + "strings.TrimSpace": {}, + "strings.TrimSuffix": {}, + "(*net/http.Request).WithContext": {}, +} + +func purity(pass *analysis.Pass) (interface{}, error) { + seen := map[*ssa.Function]struct{}{} + ssapkg := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).Pkg + var check func(ssafn *ssa.Function) (ret bool) + check = func(ssafn *ssa.Function) (ret bool) { + if ssafn.Object() == nil { + // TODO(dh): support closures + return false + } + if pass.ImportObjectFact(ssafn.Object(), new(IsPure)) { + return true + } + if ssafn.Pkg != ssapkg { + // Function is in another package but wasn't marked as + // pure, ergo it isn't pure + return false + } + // Break recursion + if _, ok := seen[ssafn]; ok { + return false + } + + seen[ssafn] = struct{}{} + defer func() { + if ret { + pass.ExportObjectFact(ssafn.Object(), &IsPure{}) + } + }() + + if functions.IsStub(ssafn) { + return false + } + + if _, ok := pureStdlib[ssafn.Object().(*types.Func).FullName()]; ok { + return true + } + + if ssafn.Signature.Results().Len() == 0 { + // A function with no return values is empty or is doing some + // work we cannot see (for example because of build tags); + // don't consider it pure. + return false + } + + for _, param := range ssafn.Params { + if _, ok := param.Type().Underlying().(*types.Basic); !ok { + return false + } + } + + if ssafn.Blocks == nil { + return false + } + checkCall := func(common *ssa.CallCommon) bool { + if common.IsInvoke() { + return false + } + builtin, ok := common.Value.(*ssa.Builtin) + if !ok { + if common.StaticCallee() != ssafn { + if common.StaticCallee() == nil { + return false + } + if !check(common.StaticCallee()) { + return false + } + } + } else { + switch builtin.Name() { + case "len", "cap", "make", "new": + default: + return false + } + } + return true + } + for _, b := range ssafn.Blocks { + for _, ins := range b.Instrs { + switch ins := ins.(type) { + case *ssa.Call: + if !checkCall(ins.Common()) { + return false + } + case *ssa.Defer: + if !checkCall(&ins.Call) { + return false + } + case *ssa.Select: + return false + case *ssa.Send: + return false + case *ssa.Go: + return false + case *ssa.Panic: + return false + case *ssa.Store: + return false + case *ssa.FieldAddr: + return false + case *ssa.UnOp: + if ins.Op == token.MUL || ins.Op == token.AND { + return false + } + } + } + } + return true + } + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + check(ssafn) + } + + out := PurityResult{} + for _, fact := range pass.AllObjectFacts() { + out[fact.Object.(*types.Func)] = fact.Fact.(*IsPure) + } + return out, nil +} diff --git a/vendor/honnef.co/go/tools/facts/token.go b/vendor/honnef.co/go/tools/facts/token.go new file mode 100644 index 000000000000..26e76ff73d50 --- /dev/null +++ b/vendor/honnef.co/go/tools/facts/token.go @@ -0,0 +1,24 @@ +package facts + +import ( + "go/ast" + "go/token" + "reflect" + + "golang.org/x/tools/go/analysis" +) + +var TokenFile = &analysis.Analyzer{ + Name: "tokenfileanalyzer", + Doc: "creates a mapping of *token.File to *ast.File", + Run: func(pass *analysis.Pass) (interface{}, error) { + m := map[*token.File]*ast.File{} + for _, af := range pass.Files { + tf := pass.Fset.File(af.Pos()) + m[tf] = af + } + return m, nil + }, + RunDespiteErrors: true, + ResultType: reflect.TypeOf(map[*token.File]*ast.File{}), +} diff --git a/vendor/github.com/golangci/go-tools/functions/loops.go b/vendor/honnef.co/go/tools/functions/loops.go similarity index 76% rename from vendor/github.com/golangci/go-tools/functions/loops.go rename to vendor/honnef.co/go/tools/functions/loops.go index 377fab87c065..15877a2f96b8 100644 --- a/vendor/github.com/golangci/go-tools/functions/loops.go +++ b/vendor/honnef.co/go/tools/functions/loops.go @@ -1,10 +1,10 @@ package functions -import "github.com/golangci/go-tools/ssa" +import "honnef.co/go/tools/ssa" -type Loop map[*ssa.BasicBlock]bool +type Loop struct{ ssa.BlockSet } -func findLoops(fn *ssa.Function) []Loop { +func FindLoops(fn *ssa.Function) []Loop { if fn.Blocks == nil { return nil } @@ -18,12 +18,16 @@ func findLoops(fn *ssa.Function) []Loop { // n is a back-edge to h // h is the loop header if n == h { - sets = append(sets, Loop{n: true}) + set := Loop{} + set.Add(n) + sets = append(sets, set) continue } - set := Loop{h: true, n: true} + set := Loop{} + set.Add(h) + set.Add(n) for _, b := range allPredsBut(n, h, nil) { - set[b] = true + set.Add(b) } sets = append(sets, set) } diff --git a/vendor/honnef.co/go/tools/functions/pure.go b/vendor/honnef.co/go/tools/functions/pure.go new file mode 100644 index 000000000000..8bc5587713a8 --- /dev/null +++ b/vendor/honnef.co/go/tools/functions/pure.go @@ -0,0 +1,46 @@ +package functions + +import ( + "honnef.co/go/tools/ssa" +) + +func filterDebug(instr []ssa.Instruction) []ssa.Instruction { + var out []ssa.Instruction + for _, ins := range instr { + if _, ok := ins.(*ssa.DebugRef); !ok { + out = append(out, ins) + } + } + return out +} + +// IsStub reports whether a function is a stub. A function is +// considered a stub if it has no instructions or exactly one +// instruction, which must be either returning only constant values or +// a panic. +func IsStub(fn *ssa.Function) bool { + if len(fn.Blocks) == 0 { + return true + } + if len(fn.Blocks) > 1 { + return false + } + instrs := filterDebug(fn.Blocks[0].Instrs) + if len(instrs) != 1 { + return false + } + + switch instrs[0].(type) { + case *ssa.Return: + // Since this is the only instruction, the return value must + // be a constant. We consider all constants as stubs, not just + // the zero value. This does not, unfortunately, cover zero + // initialised structs, as these cause additional + // instructions. + return true + case *ssa.Panic: + return true + default: + return false + } +} diff --git a/vendor/github.com/golangci/go-tools/functions/terminates.go b/vendor/honnef.co/go/tools/functions/terminates.go similarity index 75% rename from vendor/github.com/golangci/go-tools/functions/terminates.go rename to vendor/honnef.co/go/tools/functions/terminates.go index 0674edadc57f..3e9c3a23f378 100644 --- a/vendor/github.com/golangci/go-tools/functions/terminates.go +++ b/vendor/honnef.co/go/tools/functions/terminates.go @@ -1,11 +1,11 @@ package functions -import "github.com/golangci/go-tools/ssa" +import "honnef.co/go/tools/ssa" -// terminates reports whether fn is supposed to return, that is if it +// Terminates reports whether fn is supposed to return, that is if it // has at least one theoretic path that returns from the function. // Explicit panics do not count as terminating. -func terminates(fn *ssa.Function) bool { +func Terminates(fn *ssa.Function) bool { if fn.Blocks == nil { // assuming that a function terminates is the conservative // choice diff --git a/vendor/honnef.co/go/tools/go/types/typeutil/callee.go b/vendor/honnef.co/go/tools/go/types/typeutil/callee.go new file mode 100644 index 000000000000..38f596daf9e2 --- /dev/null +++ b/vendor/honnef.co/go/tools/go/types/typeutil/callee.go @@ -0,0 +1,46 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package typeutil + +import ( + "go/ast" + "go/types" + + "golang.org/x/tools/go/ast/astutil" +) + +// Callee returns the named target of a function call, if any: +// a function, method, builtin, or variable. +func Callee(info *types.Info, call *ast.CallExpr) types.Object { + var obj types.Object + switch fun := astutil.Unparen(call.Fun).(type) { + case *ast.Ident: + obj = info.Uses[fun] // type, var, builtin, or declared func + case *ast.SelectorExpr: + if sel, ok := info.Selections[fun]; ok { + obj = sel.Obj() // method or field + } else { + obj = info.Uses[fun.Sel] // qualified identifier? + } + } + if _, ok := obj.(*types.TypeName); ok { + return nil // T(x) is a conversion, not a call + } + return obj +} + +// StaticCallee returns the target (function or method) of a static +// function call, if any. It returns nil for calls to builtins. +func StaticCallee(info *types.Info, call *ast.CallExpr) *types.Func { + if f, ok := Callee(info, call).(*types.Func); ok && !interfaceMethod(f) { + return f + } + return nil +} + +func interfaceMethod(f *types.Func) bool { + recv := f.Type().(*types.Signature).Recv() + return recv != nil && types.IsInterface(recv.Type()) +} diff --git a/vendor/honnef.co/go/tools/go/types/typeutil/identical.go b/vendor/honnef.co/go/tools/go/types/typeutil/identical.go new file mode 100644 index 000000000000..c0ca441c3277 --- /dev/null +++ b/vendor/honnef.co/go/tools/go/types/typeutil/identical.go @@ -0,0 +1,75 @@ +package typeutil + +import ( + "go/types" +) + +// Identical reports whether x and y are identical types. +// Unlike types.Identical, receivers of Signature types are not ignored. +// Unlike types.Identical, interfaces are compared via pointer equality (except for the empty interface, which gets deduplicated). +// Unlike types.Identical, structs are compared via pointer equality. +func Identical(x, y types.Type) (ret bool) { + if !types.Identical(x, y) { + return false + } + + switch x := x.(type) { + case *types.Struct: + y, ok := y.(*types.Struct) + if !ok { + // should be impossible + return true + } + return x == y + case *types.Interface: + // The issue with interfaces, typeutil.Map and types.Identical + // + // types.Identical, when comparing two interfaces, only looks at the set + // of all methods, not differentiating between implicit (embedded) and + // explicit methods. + // + // When we see the following two types, in source order + // + // type I1 interface { foo() } + // type I2 interface { I1 } + // + // then we will first correctly process I1 and its underlying type. When + // we get to I2, we will see that its underlying type is identical to + // that of I1 and not process it again. This, however, means that we will + // not record the fact that I2 embeds I1. If only I2 is reachable via the + // graph root, then I1 will not be considered used. + // + // We choose to be lazy and compare interfaces by their + // pointers. This will obviously miss identical interfaces, + // but this only has a runtime cost, it doesn't affect + // correctness. + y, ok := y.(*types.Interface) + if !ok { + // should be impossible + return true + } + if x.NumEmbeddeds() == 0 && + y.NumEmbeddeds() == 0 && + x.NumMethods() == 0 && + y.NumMethods() == 0 { + // all truly empty interfaces are the same + return true + } + return x == y + case *types.Signature: + y, ok := y.(*types.Signature) + if !ok { + // should be impossible + return true + } + if x.Recv() == y.Recv() { + return true + } + if x.Recv() == nil || y.Recv() == nil { + return false + } + return Identical(x.Recv().Type(), y.Recv().Type()) + default: + return true + } +} diff --git a/vendor/honnef.co/go/tools/go/types/typeutil/imports.go b/vendor/honnef.co/go/tools/go/types/typeutil/imports.go new file mode 100644 index 000000000000..9c441dba9c06 --- /dev/null +++ b/vendor/honnef.co/go/tools/go/types/typeutil/imports.go @@ -0,0 +1,31 @@ +// Copyright 2014 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package typeutil + +import "go/types" + +// Dependencies returns all dependencies of the specified packages. +// +// Dependent packages appear in topological order: if package P imports +// package Q, Q appears earlier than P in the result. +// The algorithm follows import statements in the order they +// appear in the source code, so the result is a total order. +// +func Dependencies(pkgs ...*types.Package) []*types.Package { + var result []*types.Package + seen := make(map[*types.Package]bool) + var visit func(pkgs []*types.Package) + visit = func(pkgs []*types.Package) { + for _, p := range pkgs { + if !seen[p] { + seen[p] = true + visit(p.Imports()) + result = append(result, p) + } + } + } + visit(pkgs) + return result +} diff --git a/vendor/honnef.co/go/tools/go/types/typeutil/map.go b/vendor/honnef.co/go/tools/go/types/typeutil/map.go new file mode 100644 index 000000000000..f929353ccbd1 --- /dev/null +++ b/vendor/honnef.co/go/tools/go/types/typeutil/map.go @@ -0,0 +1,319 @@ +// Copyright 2014 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package typeutil defines various utilities for types, such as Map, +// a mapping from types.Type to interface{} values. +package typeutil + +import ( + "bytes" + "fmt" + "go/types" + "reflect" +) + +// Map is a hash-table-based mapping from types (types.Type) to +// arbitrary interface{} values. The concrete types that implement +// the Type interface are pointers. Since they are not canonicalized, +// == cannot be used to check for equivalence, and thus we cannot +// simply use a Go map. +// +// Just as with map[K]V, a nil *Map is a valid empty map. +// +// Not thread-safe. +// +// This fork handles Signatures correctly, respecting method +// receivers. Furthermore, it doesn't deduplicate interfaces or +// structs. Interfaces aren't deduplicated as not to conflate implicit +// and explicit methods. Structs aren't deduplicated because we track +// fields of each type separately. +// +type Map struct { + hasher Hasher // shared by many Maps + table map[uint32][]entry // maps hash to bucket; entry.key==nil means unused + length int // number of map entries +} + +// entry is an entry (key/value association) in a hash bucket. +type entry struct { + key types.Type + value interface{} +} + +// SetHasher sets the hasher used by Map. +// +// All Hashers are functionally equivalent but contain internal state +// used to cache the results of hashing previously seen types. +// +// A single Hasher created by MakeHasher() may be shared among many +// Maps. This is recommended if the instances have many keys in +// common, as it will amortize the cost of hash computation. +// +// A Hasher may grow without bound as new types are seen. Even when a +// type is deleted from the map, the Hasher never shrinks, since other +// types in the map may reference the deleted type indirectly. +// +// Hashers are not thread-safe, and read-only operations such as +// Map.Lookup require updates to the hasher, so a full Mutex lock (not a +// read-lock) is require around all Map operations if a shared +// hasher is accessed from multiple threads. +// +// If SetHasher is not called, the Map will create a private hasher at +// the first call to Insert. +// +func (m *Map) SetHasher(hasher Hasher) { + m.hasher = hasher +} + +// Delete removes the entry with the given key, if any. +// It returns true if the entry was found. +// +func (m *Map) Delete(key types.Type) bool { + if m != nil && m.table != nil { + hash := m.hasher.Hash(key) + bucket := m.table[hash] + for i, e := range bucket { + if e.key != nil && Identical(key, e.key) { + // We can't compact the bucket as it + // would disturb iterators. + bucket[i] = entry{} + m.length-- + return true + } + } + } + return false +} + +// At returns the map entry for the given key. +// The result is nil if the entry is not present. +// +func (m *Map) At(key types.Type) interface{} { + if m != nil && m.table != nil { + for _, e := range m.table[m.hasher.Hash(key)] { + if e.key != nil && Identical(key, e.key) { + return e.value + } + } + } + return nil +} + +// Set sets the map entry for key to val, +// and returns the previous entry, if any. +func (m *Map) Set(key types.Type, value interface{}) (prev interface{}) { + if m.table != nil { + hash := m.hasher.Hash(key) + bucket := m.table[hash] + var hole *entry + for i, e := range bucket { + if e.key == nil { + hole = &bucket[i] + } else if Identical(key, e.key) { + prev = e.value + bucket[i].value = value + return + } + } + + if hole != nil { + *hole = entry{key, value} // overwrite deleted entry + } else { + m.table[hash] = append(bucket, entry{key, value}) + } + } else { + if m.hasher.memo == nil { + m.hasher = MakeHasher() + } + hash := m.hasher.Hash(key) + m.table = map[uint32][]entry{hash: {entry{key, value}}} + } + + m.length++ + return +} + +// Len returns the number of map entries. +func (m *Map) Len() int { + if m != nil { + return m.length + } + return 0 +} + +// Iterate calls function f on each entry in the map in unspecified order. +// +// If f should mutate the map, Iterate provides the same guarantees as +// Go maps: if f deletes a map entry that Iterate has not yet reached, +// f will not be invoked for it, but if f inserts a map entry that +// Iterate has not yet reached, whether or not f will be invoked for +// it is unspecified. +// +func (m *Map) Iterate(f func(key types.Type, value interface{})) { + if m != nil { + for _, bucket := range m.table { + for _, e := range bucket { + if e.key != nil { + f(e.key, e.value) + } + } + } + } +} + +// Keys returns a new slice containing the set of map keys. +// The order is unspecified. +func (m *Map) Keys() []types.Type { + keys := make([]types.Type, 0, m.Len()) + m.Iterate(func(key types.Type, _ interface{}) { + keys = append(keys, key) + }) + return keys +} + +func (m *Map) toString(values bool) string { + if m == nil { + return "{}" + } + var buf bytes.Buffer + fmt.Fprint(&buf, "{") + sep := "" + m.Iterate(func(key types.Type, value interface{}) { + fmt.Fprint(&buf, sep) + sep = ", " + fmt.Fprint(&buf, key) + if values { + fmt.Fprintf(&buf, ": %q", value) + } + }) + fmt.Fprint(&buf, "}") + return buf.String() +} + +// String returns a string representation of the map's entries. +// Values are printed using fmt.Sprintf("%v", v). +// Order is unspecified. +// +func (m *Map) String() string { + return m.toString(true) +} + +// KeysString returns a string representation of the map's key set. +// Order is unspecified. +// +func (m *Map) KeysString() string { + return m.toString(false) +} + +//////////////////////////////////////////////////////////////////////// +// Hasher + +// A Hasher maps each type to its hash value. +// For efficiency, a hasher uses memoization; thus its memory +// footprint grows monotonically over time. +// Hashers are not thread-safe. +// Hashers have reference semantics. +// Call MakeHasher to create a Hasher. +type Hasher struct { + memo map[types.Type]uint32 +} + +// MakeHasher returns a new Hasher instance. +func MakeHasher() Hasher { + return Hasher{make(map[types.Type]uint32)} +} + +// Hash computes a hash value for the given type t such that +// Identical(t, t') => Hash(t) == Hash(t'). +func (h Hasher) Hash(t types.Type) uint32 { + hash, ok := h.memo[t] + if !ok { + hash = h.hashFor(t) + h.memo[t] = hash + } + return hash +} + +// hashString computes the Fowler–Noll–Vo hash of s. +func hashString(s string) uint32 { + var h uint32 + for i := 0; i < len(s); i++ { + h ^= uint32(s[i]) + h *= 16777619 + } + return h +} + +// hashFor computes the hash of t. +func (h Hasher) hashFor(t types.Type) uint32 { + // See Identical for rationale. + switch t := t.(type) { + case *types.Basic: + return uint32(t.Kind()) + + case *types.Array: + return 9043 + 2*uint32(t.Len()) + 3*h.Hash(t.Elem()) + + case *types.Slice: + return 9049 + 2*h.Hash(t.Elem()) + + case *types.Struct: + var hash uint32 = 9059 + for i, n := 0, t.NumFields(); i < n; i++ { + f := t.Field(i) + if f.Anonymous() { + hash += 8861 + } + hash += hashString(t.Tag(i)) + hash += hashString(f.Name()) // (ignore f.Pkg) + hash += h.Hash(f.Type()) + } + return hash + + case *types.Pointer: + return 9067 + 2*h.Hash(t.Elem()) + + case *types.Signature: + var hash uint32 = 9091 + if t.Variadic() { + hash *= 8863 + } + return hash + 3*h.hashTuple(t.Params()) + 5*h.hashTuple(t.Results()) + + case *types.Interface: + var hash uint32 = 9103 + for i, n := 0, t.NumMethods(); i < n; i++ { + // See go/types.identicalMethods for rationale. + // Method order is not significant. + // Ignore m.Pkg(). + m := t.Method(i) + hash += 3*hashString(m.Name()) + 5*h.Hash(m.Type()) + } + return hash + + case *types.Map: + return 9109 + 2*h.Hash(t.Key()) + 3*h.Hash(t.Elem()) + + case *types.Chan: + return 9127 + 2*uint32(t.Dir()) + 3*h.Hash(t.Elem()) + + case *types.Named: + // Not safe with a copying GC; objects may move. + return uint32(reflect.ValueOf(t.Obj()).Pointer()) + + case *types.Tuple: + return h.hashTuple(t) + } + panic(t) +} + +func (h Hasher) hashTuple(tuple *types.Tuple) uint32 { + // See go/types.identicalTypes for rationale. + n := tuple.Len() + var hash uint32 = 9137 + 2*uint32(n) + for i := 0; i < n; i++ { + hash += 3 * h.Hash(tuple.At(i).Type()) + } + return hash +} diff --git a/vendor/honnef.co/go/tools/go/types/typeutil/methodsetcache.go b/vendor/honnef.co/go/tools/go/types/typeutil/methodsetcache.go new file mode 100644 index 000000000000..32084610f49a --- /dev/null +++ b/vendor/honnef.co/go/tools/go/types/typeutil/methodsetcache.go @@ -0,0 +1,72 @@ +// Copyright 2014 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// This file implements a cache of method sets. + +package typeutil + +import ( + "go/types" + "sync" +) + +// A MethodSetCache records the method set of each type T for which +// MethodSet(T) is called so that repeat queries are fast. +// The zero value is a ready-to-use cache instance. +type MethodSetCache struct { + mu sync.Mutex + named map[*types.Named]struct{ value, pointer *types.MethodSet } // method sets for named N and *N + others map[types.Type]*types.MethodSet // all other types +} + +// MethodSet returns the method set of type T. It is thread-safe. +// +// If cache is nil, this function is equivalent to types.NewMethodSet(T). +// Utility functions can thus expose an optional *MethodSetCache +// parameter to clients that care about performance. +// +func (cache *MethodSetCache) MethodSet(T types.Type) *types.MethodSet { + if cache == nil { + return types.NewMethodSet(T) + } + cache.mu.Lock() + defer cache.mu.Unlock() + + switch T := T.(type) { + case *types.Named: + return cache.lookupNamed(T).value + + case *types.Pointer: + if N, ok := T.Elem().(*types.Named); ok { + return cache.lookupNamed(N).pointer + } + } + + // all other types + // (The map uses pointer equivalence, not type identity.) + mset := cache.others[T] + if mset == nil { + mset = types.NewMethodSet(T) + if cache.others == nil { + cache.others = make(map[types.Type]*types.MethodSet) + } + cache.others[T] = mset + } + return mset +} + +func (cache *MethodSetCache) lookupNamed(named *types.Named) struct{ value, pointer *types.MethodSet } { + if cache.named == nil { + cache.named = make(map[*types.Named]struct{ value, pointer *types.MethodSet }) + } + // Avoid recomputing mset(*T) for each distinct Pointer + // instance whose underlying type is a named type. + msets, ok := cache.named[named] + if !ok { + msets.value = types.NewMethodSet(named) + msets.pointer = types.NewMethodSet(types.NewPointer(named)) + cache.named[named] = msets + } + return msets +} diff --git a/vendor/honnef.co/go/tools/go/types/typeutil/ui.go b/vendor/honnef.co/go/tools/go/types/typeutil/ui.go new file mode 100644 index 000000000000..9849c24cef3f --- /dev/null +++ b/vendor/honnef.co/go/tools/go/types/typeutil/ui.go @@ -0,0 +1,52 @@ +// Copyright 2014 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package typeutil + +// This file defines utilities for user interfaces that display types. + +import "go/types" + +// IntuitiveMethodSet returns the intuitive method set of a type T, +// which is the set of methods you can call on an addressable value of +// that type. +// +// The result always contains MethodSet(T), and is exactly MethodSet(T) +// for interface types and for pointer-to-concrete types. +// For all other concrete types T, the result additionally +// contains each method belonging to *T if there is no identically +// named method on T itself. +// +// This corresponds to user intuition about method sets; +// this function is intended only for user interfaces. +// +// The order of the result is as for types.MethodSet(T). +// +func IntuitiveMethodSet(T types.Type, msets *MethodSetCache) []*types.Selection { + isPointerToConcrete := func(T types.Type) bool { + ptr, ok := T.(*types.Pointer) + return ok && !types.IsInterface(ptr.Elem()) + } + + var result []*types.Selection + mset := msets.MethodSet(T) + if types.IsInterface(T) || isPointerToConcrete(T) { + for i, n := 0, mset.Len(); i < n; i++ { + result = append(result, mset.At(i)) + } + } else { + // T is some other concrete type. + // Report methods of T and *T, preferring those of T. + pmset := msets.MethodSet(types.NewPointer(T)) + for i, n := 0, pmset.Len(); i < n; i++ { + meth := pmset.At(i) + if m := mset.Lookup(meth.Obj().Pkg(), meth.Obj().Name()); m != nil { + meth = m + } + result = append(result, meth) + } + + } + return result +} diff --git a/vendor/honnef.co/go/tools/internal/cache/cache.go b/vendor/honnef.co/go/tools/internal/cache/cache.go new file mode 100644 index 000000000000..2b33ca10671b --- /dev/null +++ b/vendor/honnef.co/go/tools/internal/cache/cache.go @@ -0,0 +1,474 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package cache implements a build artifact cache. +// +// This package is a slightly modified fork of Go's +// cmd/go/internal/cache package. +package cache + +import ( + "bytes" + "crypto/sha256" + "encoding/hex" + "errors" + "fmt" + "io" + "io/ioutil" + "os" + "path/filepath" + "strconv" + "strings" + "time" + + "honnef.co/go/tools/internal/renameio" +) + +// An ActionID is a cache action key, the hash of a complete description of a +// repeatable computation (command line, environment variables, +// input file contents, executable contents). +type ActionID [HashSize]byte + +// An OutputID is a cache output key, the hash of an output of a computation. +type OutputID [HashSize]byte + +// A Cache is a package cache, backed by a file system directory tree. +type Cache struct { + dir string + now func() time.Time +} + +// Open opens and returns the cache in the given directory. +// +// It is safe for multiple processes on a single machine to use the +// same cache directory in a local file system simultaneously. +// They will coordinate using operating system file locks and may +// duplicate effort but will not corrupt the cache. +// +// However, it is NOT safe for multiple processes on different machines +// to share a cache directory (for example, if the directory were stored +// in a network file system). File locking is notoriously unreliable in +// network file systems and may not suffice to protect the cache. +// +func Open(dir string) (*Cache, error) { + info, err := os.Stat(dir) + if err != nil { + return nil, err + } + if !info.IsDir() { + return nil, &os.PathError{Op: "open", Path: dir, Err: fmt.Errorf("not a directory")} + } + for i := 0; i < 256; i++ { + name := filepath.Join(dir, fmt.Sprintf("%02x", i)) + if err := os.MkdirAll(name, 0777); err != nil { + return nil, err + } + } + c := &Cache{ + dir: dir, + now: time.Now, + } + return c, nil +} + +// fileName returns the name of the file corresponding to the given id. +func (c *Cache) fileName(id [HashSize]byte, key string) string { + return filepath.Join(c.dir, fmt.Sprintf("%02x", id[0]), fmt.Sprintf("%x", id)+"-"+key) +} + +var errMissing = errors.New("cache entry not found") + +const ( + // action entry file is "v1 \n" + hexSize = HashSize * 2 + entrySize = 2 + 1 + hexSize + 1 + hexSize + 1 + 20 + 1 + 20 + 1 +) + +// verify controls whether to run the cache in verify mode. +// In verify mode, the cache always returns errMissing from Get +// but then double-checks in Put that the data being written +// exactly matches any existing entry. This provides an easy +// way to detect program behavior that would have been different +// had the cache entry been returned from Get. +// +// verify is enabled by setting the environment variable +// GODEBUG=gocacheverify=1. +var verify = false + +// DebugTest is set when GODEBUG=gocachetest=1 is in the environment. +var DebugTest = false + +func init() { initEnv() } + +func initEnv() { + verify = false + debugHash = false + debug := strings.Split(os.Getenv("GODEBUG"), ",") + for _, f := range debug { + if f == "gocacheverify=1" { + verify = true + } + if f == "gocachehash=1" { + debugHash = true + } + if f == "gocachetest=1" { + DebugTest = true + } + } +} + +// Get looks up the action ID in the cache, +// returning the corresponding output ID and file size, if any. +// Note that finding an output ID does not guarantee that the +// saved file for that output ID is still available. +func (c *Cache) Get(id ActionID) (Entry, error) { + if verify { + return Entry{}, errMissing + } + return c.get(id) +} + +type Entry struct { + OutputID OutputID + Size int64 + Time time.Time +} + +// get is Get but does not respect verify mode, so that Put can use it. +func (c *Cache) get(id ActionID) (Entry, error) { + missing := func() (Entry, error) { + return Entry{}, errMissing + } + f, err := os.Open(c.fileName(id, "a")) + if err != nil { + return missing() + } + defer f.Close() + entry := make([]byte, entrySize+1) // +1 to detect whether f is too long + if n, err := io.ReadFull(f, entry); n != entrySize || err != io.ErrUnexpectedEOF { + return missing() + } + if entry[0] != 'v' || entry[1] != '1' || entry[2] != ' ' || entry[3+hexSize] != ' ' || entry[3+hexSize+1+hexSize] != ' ' || entry[3+hexSize+1+hexSize+1+20] != ' ' || entry[entrySize-1] != '\n' { + return missing() + } + eid, entry := entry[3:3+hexSize], entry[3+hexSize:] + eout, entry := entry[1:1+hexSize], entry[1+hexSize:] + esize, entry := entry[1:1+20], entry[1+20:] + //lint:ignore SA4006 See https://github.com/dominikh/go-tools/issues/465 + etime, entry := entry[1:1+20], entry[1+20:] + var buf [HashSize]byte + if _, err := hex.Decode(buf[:], eid); err != nil || buf != id { + return missing() + } + if _, err := hex.Decode(buf[:], eout); err != nil { + return missing() + } + i := 0 + for i < len(esize) && esize[i] == ' ' { + i++ + } + size, err := strconv.ParseInt(string(esize[i:]), 10, 64) + if err != nil || size < 0 { + return missing() + } + i = 0 + for i < len(etime) && etime[i] == ' ' { + i++ + } + tm, err := strconv.ParseInt(string(etime[i:]), 10, 64) + if err != nil || size < 0 { + return missing() + } + + c.used(c.fileName(id, "a")) + + return Entry{buf, size, time.Unix(0, tm)}, nil +} + +// GetFile looks up the action ID in the cache and returns +// the name of the corresponding data file. +func (c *Cache) GetFile(id ActionID) (file string, entry Entry, err error) { + entry, err = c.Get(id) + if err != nil { + return "", Entry{}, err + } + file = c.OutputFile(entry.OutputID) + info, err := os.Stat(file) + if err != nil || info.Size() != entry.Size { + return "", Entry{}, errMissing + } + return file, entry, nil +} + +// GetBytes looks up the action ID in the cache and returns +// the corresponding output bytes. +// GetBytes should only be used for data that can be expected to fit in memory. +func (c *Cache) GetBytes(id ActionID) ([]byte, Entry, error) { + entry, err := c.Get(id) + if err != nil { + return nil, entry, err + } + data, _ := ioutil.ReadFile(c.OutputFile(entry.OutputID)) + if sha256.Sum256(data) != entry.OutputID { + return nil, entry, errMissing + } + return data, entry, nil +} + +// OutputFile returns the name of the cache file storing output with the given OutputID. +func (c *Cache) OutputFile(out OutputID) string { + file := c.fileName(out, "d") + c.used(file) + return file +} + +// Time constants for cache expiration. +// +// We set the mtime on a cache file on each use, but at most one per mtimeInterval (1 hour), +// to avoid causing many unnecessary inode updates. The mtimes therefore +// roughly reflect "time of last use" but may in fact be older by at most an hour. +// +// We scan the cache for entries to delete at most once per trimInterval (1 day). +// +// When we do scan the cache, we delete entries that have not been used for +// at least trimLimit (5 days). Statistics gathered from a month of usage by +// Go developers found that essentially all reuse of cached entries happened +// within 5 days of the previous reuse. See golang.org/issue/22990. +const ( + mtimeInterval = 1 * time.Hour + trimInterval = 24 * time.Hour + trimLimit = 5 * 24 * time.Hour +) + +// used makes a best-effort attempt to update mtime on file, +// so that mtime reflects cache access time. +// +// Because the reflection only needs to be approximate, +// and to reduce the amount of disk activity caused by using +// cache entries, used only updates the mtime if the current +// mtime is more than an hour old. This heuristic eliminates +// nearly all of the mtime updates that would otherwise happen, +// while still keeping the mtimes useful for cache trimming. +func (c *Cache) used(file string) { + info, err := os.Stat(file) + if err == nil && c.now().Sub(info.ModTime()) < mtimeInterval { + return + } + os.Chtimes(file, c.now(), c.now()) +} + +// Trim removes old cache entries that are likely not to be reused. +func (c *Cache) Trim() { + now := c.now() + + // We maintain in dir/trim.txt the time of the last completed cache trim. + // If the cache has been trimmed recently enough, do nothing. + // This is the common case. + data, _ := ioutil.ReadFile(filepath.Join(c.dir, "trim.txt")) + t, err := strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64) + if err == nil && now.Sub(time.Unix(t, 0)) < trimInterval { + return + } + + // Trim each of the 256 subdirectories. + // We subtract an additional mtimeInterval + // to account for the imprecision of our "last used" mtimes. + cutoff := now.Add(-trimLimit - mtimeInterval) + for i := 0; i < 256; i++ { + subdir := filepath.Join(c.dir, fmt.Sprintf("%02x", i)) + c.trimSubdir(subdir, cutoff) + } + + // Ignore errors from here: if we don't write the complete timestamp, the + // cache will appear older than it is, and we'll trim it again next time. + renameio.WriteFile(filepath.Join(c.dir, "trim.txt"), []byte(fmt.Sprintf("%d", now.Unix()))) +} + +// trimSubdir trims a single cache subdirectory. +func (c *Cache) trimSubdir(subdir string, cutoff time.Time) { + // Read all directory entries from subdir before removing + // any files, in case removing files invalidates the file offset + // in the directory scan. Also, ignore error from f.Readdirnames, + // because we don't care about reporting the error and we still + // want to process any entries found before the error. + f, err := os.Open(subdir) + if err != nil { + return + } + names, _ := f.Readdirnames(-1) + f.Close() + + for _, name := range names { + // Remove only cache entries (xxxx-a and xxxx-d). + if !strings.HasSuffix(name, "-a") && !strings.HasSuffix(name, "-d") { + continue + } + entry := filepath.Join(subdir, name) + info, err := os.Stat(entry) + if err == nil && info.ModTime().Before(cutoff) { + os.Remove(entry) + } + } +} + +// putIndexEntry adds an entry to the cache recording that executing the action +// with the given id produces an output with the given output id (hash) and size. +func (c *Cache) putIndexEntry(id ActionID, out OutputID, size int64, allowVerify bool) error { + // Note: We expect that for one reason or another it may happen + // that repeating an action produces a different output hash + // (for example, if the output contains a time stamp or temp dir name). + // While not ideal, this is also not a correctness problem, so we + // don't make a big deal about it. In particular, we leave the action + // cache entries writable specifically so that they can be overwritten. + // + // Setting GODEBUG=gocacheverify=1 does make a big deal: + // in verify mode we are double-checking that the cache entries + // are entirely reproducible. As just noted, this may be unrealistic + // in some cases but the check is also useful for shaking out real bugs. + entry := []byte(fmt.Sprintf("v1 %x %x %20d %20d\n", id, out, size, time.Now().UnixNano())) + if verify && allowVerify { + old, err := c.get(id) + if err == nil && (old.OutputID != out || old.Size != size) { + // panic to show stack trace, so we can see what code is generating this cache entry. + msg := fmt.Sprintf("go: internal cache error: cache verify failed: id=%x changed:<<<\n%s\n>>>\nold: %x %d\nnew: %x %d", id, reverseHash(id), out, size, old.OutputID, old.Size) + panic(msg) + } + } + file := c.fileName(id, "a") + if err := ioutil.WriteFile(file, entry, 0666); err != nil { + // TODO(bcmills): This Remove potentially races with another go command writing to file. + // Can we eliminate it? + os.Remove(file) + return err + } + os.Chtimes(file, c.now(), c.now()) // mainly for tests + + return nil +} + +// Put stores the given output in the cache as the output for the action ID. +// It may read file twice. The content of file must not change between the two passes. +func (c *Cache) Put(id ActionID, file io.ReadSeeker) (OutputID, int64, error) { + return c.put(id, file, true) +} + +// PutNoVerify is like Put but disables the verify check +// when GODEBUG=goverifycache=1 is set. +// It is meant for data that is OK to cache but that we expect to vary slightly from run to run, +// like test output containing times and the like. +func (c *Cache) PutNoVerify(id ActionID, file io.ReadSeeker) (OutputID, int64, error) { + return c.put(id, file, false) +} + +func (c *Cache) put(id ActionID, file io.ReadSeeker, allowVerify bool) (OutputID, int64, error) { + // Compute output ID. + h := sha256.New() + if _, err := file.Seek(0, 0); err != nil { + return OutputID{}, 0, err + } + size, err := io.Copy(h, file) + if err != nil { + return OutputID{}, 0, err + } + var out OutputID + h.Sum(out[:0]) + + // Copy to cached output file (if not already present). + if err := c.copyFile(file, out, size); err != nil { + return out, size, err + } + + // Add to cache index. + return out, size, c.putIndexEntry(id, out, size, allowVerify) +} + +// PutBytes stores the given bytes in the cache as the output for the action ID. +func (c *Cache) PutBytes(id ActionID, data []byte) error { + _, _, err := c.Put(id, bytes.NewReader(data)) + return err +} + +// copyFile copies file into the cache, expecting it to have the given +// output ID and size, if that file is not present already. +func (c *Cache) copyFile(file io.ReadSeeker, out OutputID, size int64) error { + name := c.fileName(out, "d") + info, err := os.Stat(name) + if err == nil && info.Size() == size { + // Check hash. + if f, err := os.Open(name); err == nil { + h := sha256.New() + io.Copy(h, f) + f.Close() + var out2 OutputID + h.Sum(out2[:0]) + if out == out2 { + return nil + } + } + // Hash did not match. Fall through and rewrite file. + } + + // Copy file to cache directory. + mode := os.O_RDWR | os.O_CREATE + if err == nil && info.Size() > size { // shouldn't happen but fix in case + mode |= os.O_TRUNC + } + f, err := os.OpenFile(name, mode, 0666) + if err != nil { + return err + } + defer f.Close() + if size == 0 { + // File now exists with correct size. + // Only one possible zero-length file, so contents are OK too. + // Early return here makes sure there's a "last byte" for code below. + return nil + } + + // From here on, if any of the I/O writing the file fails, + // we make a best-effort attempt to truncate the file f + // before returning, to avoid leaving bad bytes in the file. + + // Copy file to f, but also into h to double-check hash. + if _, err := file.Seek(0, 0); err != nil { + f.Truncate(0) + return err + } + h := sha256.New() + w := io.MultiWriter(f, h) + if _, err := io.CopyN(w, file, size-1); err != nil { + f.Truncate(0) + return err + } + // Check last byte before writing it; writing it will make the size match + // what other processes expect to find and might cause them to start + // using the file. + buf := make([]byte, 1) + if _, err := file.Read(buf); err != nil { + f.Truncate(0) + return err + } + h.Write(buf) + sum := h.Sum(nil) + if !bytes.Equal(sum, out[:]) { + f.Truncate(0) + return fmt.Errorf("file content changed underfoot") + } + + // Commit cache file entry. + if _, err := f.Write(buf); err != nil { + f.Truncate(0) + return err + } + if err := f.Close(); err != nil { + // Data might not have been written, + // but file may look like it is the right size. + // To be extra careful, remove cached file. + os.Remove(name) + return err + } + os.Chtimes(name, c.now(), c.now()) // mainly for tests + + return nil +} diff --git a/vendor/honnef.co/go/tools/internal/cache/default.go b/vendor/honnef.co/go/tools/internal/cache/default.go new file mode 100644 index 000000000000..3034f76a538f --- /dev/null +++ b/vendor/honnef.co/go/tools/internal/cache/default.go @@ -0,0 +1,85 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cache + +import ( + "fmt" + "io/ioutil" + "log" + "os" + "path/filepath" + "sync" +) + +// Default returns the default cache to use. +func Default() (*Cache, error) { + defaultOnce.Do(initDefaultCache) + return defaultCache, defaultDirErr +} + +var ( + defaultOnce sync.Once + defaultCache *Cache +) + +// cacheREADME is a message stored in a README in the cache directory. +// Because the cache lives outside the normal Go trees, we leave the +// README as a courtesy to explain where it came from. +const cacheREADME = `This directory holds cached build artifacts from staticcheck. +` + +// initDefaultCache does the work of finding the default cache +// the first time Default is called. +func initDefaultCache() { + dir := DefaultDir() + if err := os.MkdirAll(dir, 0777); err != nil { + log.Fatalf("failed to initialize build cache at %s: %s\n", dir, err) + } + if _, err := os.Stat(filepath.Join(dir, "README")); err != nil { + // Best effort. + ioutil.WriteFile(filepath.Join(dir, "README"), []byte(cacheREADME), 0666) + } + + c, err := Open(dir) + if err != nil { + log.Fatalf("failed to initialize build cache at %s: %s\n", dir, err) + } + defaultCache = c +} + +var ( + defaultDirOnce sync.Once + defaultDir string + defaultDirErr error +) + +// DefaultDir returns the effective STATICCHECK_CACHE setting. +func DefaultDir() string { + // Save the result of the first call to DefaultDir for later use in + // initDefaultCache. cmd/go/main.go explicitly sets GOCACHE so that + // subprocesses will inherit it, but that means initDefaultCache can't + // otherwise distinguish between an explicit "off" and a UserCacheDir error. + + defaultDirOnce.Do(func() { + defaultDir = os.Getenv("STATICCHECK_CACHE") + if filepath.IsAbs(defaultDir) { + return + } + if defaultDir != "" { + defaultDirErr = fmt.Errorf("STATICCHECK_CACHE is not an absolute path") + return + } + + // Compute default location. + dir, err := os.UserCacheDir() + if err != nil { + defaultDirErr = fmt.Errorf("STATICCHECK_CACHE is not defined and %v", err) + return + } + defaultDir = filepath.Join(dir, "staticcheck") + }) + + return defaultDir +} diff --git a/vendor/honnef.co/go/tools/internal/cache/hash.go b/vendor/honnef.co/go/tools/internal/cache/hash.go new file mode 100644 index 000000000000..a53543ec5018 --- /dev/null +++ b/vendor/honnef.co/go/tools/internal/cache/hash.go @@ -0,0 +1,176 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cache + +import ( + "bytes" + "crypto/sha256" + "fmt" + "hash" + "io" + "os" + "sync" +) + +var debugHash = false // set when GODEBUG=gocachehash=1 + +// HashSize is the number of bytes in a hash. +const HashSize = 32 + +// A Hash provides access to the canonical hash function used to index the cache. +// The current implementation uses salted SHA256, but clients must not assume this. +type Hash struct { + h hash.Hash + name string // for debugging + buf *bytes.Buffer // for verify +} + +// hashSalt is a salt string added to the beginning of every hash +// created by NewHash. Using the Staticcheck version makes sure that different +// versions of the command do not address the same cache +// entries, so that a bug in one version does not affect the execution +// of other versions. This salt will result in additional ActionID files +// in the cache, but not additional copies of the large output files, +// which are still addressed by unsalted SHA256. +var hashSalt []byte + +func SetSalt(b []byte) { + hashSalt = b +} + +// Subkey returns an action ID corresponding to mixing a parent +// action ID with a string description of the subkey. +func Subkey(parent ActionID, desc string) ActionID { + h := sha256.New() + h.Write([]byte("subkey:")) + h.Write(parent[:]) + h.Write([]byte(desc)) + var out ActionID + h.Sum(out[:0]) + if debugHash { + fmt.Fprintf(os.Stderr, "HASH subkey %x %q = %x\n", parent, desc, out) + } + if verify { + hashDebug.Lock() + hashDebug.m[out] = fmt.Sprintf("subkey %x %q", parent, desc) + hashDebug.Unlock() + } + return out +} + +// NewHash returns a new Hash. +// The caller is expected to Write data to it and then call Sum. +func NewHash(name string) *Hash { + h := &Hash{h: sha256.New(), name: name} + if debugHash { + fmt.Fprintf(os.Stderr, "HASH[%s]\n", h.name) + } + h.Write(hashSalt) + if verify { + h.buf = new(bytes.Buffer) + } + return h +} + +// Write writes data to the running hash. +func (h *Hash) Write(b []byte) (int, error) { + if debugHash { + fmt.Fprintf(os.Stderr, "HASH[%s]: %q\n", h.name, b) + } + if h.buf != nil { + h.buf.Write(b) + } + return h.h.Write(b) +} + +// Sum returns the hash of the data written previously. +func (h *Hash) Sum() [HashSize]byte { + var out [HashSize]byte + h.h.Sum(out[:0]) + if debugHash { + fmt.Fprintf(os.Stderr, "HASH[%s]: %x\n", h.name, out) + } + if h.buf != nil { + hashDebug.Lock() + if hashDebug.m == nil { + hashDebug.m = make(map[[HashSize]byte]string) + } + hashDebug.m[out] = h.buf.String() + hashDebug.Unlock() + } + return out +} + +// In GODEBUG=gocacheverify=1 mode, +// hashDebug holds the input to every computed hash ID, +// so that we can work backward from the ID involved in a +// cache entry mismatch to a description of what should be there. +var hashDebug struct { + sync.Mutex + m map[[HashSize]byte]string +} + +// reverseHash returns the input used to compute the hash id. +func reverseHash(id [HashSize]byte) string { + hashDebug.Lock() + s := hashDebug.m[id] + hashDebug.Unlock() + return s +} + +var hashFileCache struct { + sync.Mutex + m map[string][HashSize]byte +} + +// FileHash returns the hash of the named file. +// It caches repeated lookups for a given file, +// and the cache entry for a file can be initialized +// using SetFileHash. +// The hash used by FileHash is not the same as +// the hash used by NewHash. +func FileHash(file string) ([HashSize]byte, error) { + hashFileCache.Lock() + out, ok := hashFileCache.m[file] + hashFileCache.Unlock() + + if ok { + return out, nil + } + + h := sha256.New() + f, err := os.Open(file) + if err != nil { + if debugHash { + fmt.Fprintf(os.Stderr, "HASH %s: %v\n", file, err) + } + return [HashSize]byte{}, err + } + _, err = io.Copy(h, f) + f.Close() + if err != nil { + if debugHash { + fmt.Fprintf(os.Stderr, "HASH %s: %v\n", file, err) + } + return [HashSize]byte{}, err + } + h.Sum(out[:0]) + if debugHash { + fmt.Fprintf(os.Stderr, "HASH %s: %x\n", file, out) + } + + SetFileHash(file, out) + return out, nil +} + +// SetFileHash sets the hash returned by FileHash for file. +func SetFileHash(file string, sum [HashSize]byte) { + hashFileCache.Lock() + if hashFileCache.m == nil { + hashFileCache.m = make(map[string][HashSize]byte) + } + hashFileCache.m[file] = sum + hashFileCache.Unlock() +} diff --git a/vendor/honnef.co/go/tools/internal/passes/buildssa/buildssa.go b/vendor/honnef.co/go/tools/internal/passes/buildssa/buildssa.go new file mode 100644 index 000000000000..fde918d12136 --- /dev/null +++ b/vendor/honnef.co/go/tools/internal/passes/buildssa/buildssa.go @@ -0,0 +1,116 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package buildssa defines an Analyzer that constructs the SSA +// representation of an error-free package and returns the set of all +// functions within it. It does not report any diagnostics itself but +// may be used as an input to other analyzers. +// +// THIS INTERFACE IS EXPERIMENTAL AND MAY BE SUBJECT TO INCOMPATIBLE CHANGE. +package buildssa + +import ( + "go/ast" + "go/types" + "reflect" + + "golang.org/x/tools/go/analysis" + "honnef.co/go/tools/ssa" +) + +var Analyzer = &analysis.Analyzer{ + Name: "buildssa", + Doc: "build SSA-form IR for later passes", + Run: run, + ResultType: reflect.TypeOf(new(SSA)), +} + +// SSA provides SSA-form intermediate representation for all the +// non-blank source functions in the current package. +type SSA struct { + Pkg *ssa.Package + SrcFuncs []*ssa.Function +} + +func run(pass *analysis.Pass) (interface{}, error) { + // Plundered from ssautil.BuildPackage. + + // We must create a new Program for each Package because the + // analysis API provides no place to hang a Program shared by + // all Packages. Consequently, SSA Packages and Functions do not + // have a canonical representation across an analysis session of + // multiple packages. This is unlikely to be a problem in + // practice because the analysis API essentially forces all + // packages to be analysed independently, so any given call to + // Analysis.Run on a package will see only SSA objects belonging + // to a single Program. + + mode := ssa.GlobalDebug + + prog := ssa.NewProgram(pass.Fset, mode) + + // Create SSA packages for all imports. + // Order is not significant. + created := make(map[*types.Package]bool) + var createAll func(pkgs []*types.Package) + createAll = func(pkgs []*types.Package) { + for _, p := range pkgs { + if !created[p] { + created[p] = true + prog.CreatePackage(p, nil, nil, true) + createAll(p.Imports()) + } + } + } + createAll(pass.Pkg.Imports()) + + // Create and build the primary package. + ssapkg := prog.CreatePackage(pass.Pkg, pass.Files, pass.TypesInfo, false) + ssapkg.Build() + + // Compute list of source functions, including literals, + // in source order. + var funcs []*ssa.Function + var addAnons func(f *ssa.Function) + addAnons = func(f *ssa.Function) { + funcs = append(funcs, f) + for _, anon := range f.AnonFuncs { + addAnons(anon) + } + } + addAnons(ssapkg.Members["init"].(*ssa.Function)) + for _, f := range pass.Files { + for _, decl := range f.Decls { + if fdecl, ok := decl.(*ast.FuncDecl); ok { + + // SSA will not build a Function + // for a FuncDecl named blank. + // That's arguably too strict but + // relaxing it would break uniqueness of + // names of package members. + if fdecl.Name.Name == "_" { + continue + } + + // (init functions have distinct Func + // objects named "init" and distinct + // ssa.Functions named "init#1", ...) + + fn := pass.TypesInfo.Defs[fdecl.Name].(*types.Func) + if fn == nil { + panic(fn) + } + + f := ssapkg.Prog.FuncValue(fn) + if f == nil { + panic(fn) + } + + addAnons(f) + } + } + } + + return &SSA{Pkg: ssapkg, SrcFuncs: funcs}, nil +} diff --git a/vendor/honnef.co/go/tools/internal/renameio/renameio.go b/vendor/honnef.co/go/tools/internal/renameio/renameio.go new file mode 100644 index 000000000000..3f3f1708fa48 --- /dev/null +++ b/vendor/honnef.co/go/tools/internal/renameio/renameio.go @@ -0,0 +1,83 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package renameio writes files atomically by renaming temporary files. +package renameio + +import ( + "bytes" + "io" + "io/ioutil" + "os" + "path/filepath" + "runtime" + "strings" + "time" +) + +const patternSuffix = "*.tmp" + +// Pattern returns a glob pattern that matches the unrenamed temporary files +// created when writing to filename. +func Pattern(filename string) string { + return filepath.Join(filepath.Dir(filename), filepath.Base(filename)+patternSuffix) +} + +// WriteFile is like ioutil.WriteFile, but first writes data to an arbitrary +// file in the same directory as filename, then renames it atomically to the +// final name. +// +// That ensures that the final location, if it exists, is always a complete file. +func WriteFile(filename string, data []byte) (err error) { + return WriteToFile(filename, bytes.NewReader(data)) +} + +// WriteToFile is a variant of WriteFile that accepts the data as an io.Reader +// instead of a slice. +func WriteToFile(filename string, data io.Reader) (err error) { + f, err := ioutil.TempFile(filepath.Dir(filename), filepath.Base(filename)+patternSuffix) + if err != nil { + return err + } + defer func() { + // Only call os.Remove on f.Name() if we failed to rename it: otherwise, + // some other process may have created a new file with the same name after + // that. + if err != nil { + f.Close() + os.Remove(f.Name()) + } + }() + + if _, err := io.Copy(f, data); err != nil { + return err + } + // Sync the file before renaming it: otherwise, after a crash the reader may + // observe a 0-length file instead of the actual contents. + // See https://golang.org/issue/22397#issuecomment-380831736. + if err := f.Sync(); err != nil { + return err + } + if err := f.Close(); err != nil { + return err + } + + var start time.Time + for { + err := os.Rename(f.Name(), filename) + if err == nil || runtime.GOOS != "windows" || !strings.HasSuffix(err.Error(), "Access is denied.") { + return err + } + + // Windows seems to occasionally trigger spurious "Access is denied" errors + // here (see golang.org/issue/31247). We're not sure why. It's probably + // worth a little extra latency to avoid propagating the spurious errors. + if start.IsZero() { + start = time.Now() + } else if time.Since(start) >= 500*time.Millisecond { + return err + } + time.Sleep(5 * time.Millisecond) + } +} diff --git a/vendor/github.com/golangci/go-tools/internal/sharedcheck/lint.go b/vendor/honnef.co/go/tools/internal/sharedcheck/lint.go similarity index 79% rename from vendor/github.com/golangci/go-tools/internal/sharedcheck/lint.go rename to vendor/honnef.co/go/tools/internal/sharedcheck/lint.go index 0b2e91b1eccf..affee6607261 100644 --- a/vendor/github.com/golangci/go-tools/internal/sharedcheck/lint.go +++ b/vendor/honnef.co/go/tools/internal/sharedcheck/lint.go @@ -4,13 +4,14 @@ import ( "go/ast" "go/types" - "github.com/golangci/go-tools/lint" - . "github.com/golangci/go-tools/lint/lintdsl" - "github.com/golangci/go-tools/ssa" + "golang.org/x/tools/go/analysis" + "honnef.co/go/tools/internal/passes/buildssa" + . "honnef.co/go/tools/lint/lintdsl" + "honnef.co/go/tools/ssa" ) -func CheckRangeStringRunes(j *lint.Job) { - for _, ssafn := range j.Program.InitialFunctions { +func CheckRangeStringRunes(pass *analysis.Pass) (interface{}, error) { + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { fn := func(node ast.Node) bool { rng, ok := node.(*ast.RangeStmt) if !ok || !IsBlank(rng.Key) { @@ -59,10 +60,11 @@ func CheckRangeStringRunes(j *lint.Job) { return true } - j.Errorf(rng, "should range over string, not []rune(string)") + pass.Reportf(rng.Pos(), "should range over string, not []rune(string)") return true } Inspect(ssafn.Syntax(), fn) } + return nil, nil } diff --git a/vendor/github.com/golangci/go-tools/lint/LICENSE b/vendor/honnef.co/go/tools/lint/LICENSE similarity index 100% rename from vendor/github.com/golangci/go-tools/lint/LICENSE rename to vendor/honnef.co/go/tools/lint/LICENSE diff --git a/vendor/honnef.co/go/tools/lint/lint.go b/vendor/honnef.co/go/tools/lint/lint.go new file mode 100644 index 000000000000..de5a8f1288d5 --- /dev/null +++ b/vendor/honnef.co/go/tools/lint/lint.go @@ -0,0 +1,491 @@ +// Package lint provides the foundation for tools like staticcheck +package lint // import "honnef.co/go/tools/lint" + +import ( + "bytes" + "fmt" + "go/scanner" + "go/token" + "go/types" + "path/filepath" + "sort" + "strings" + "sync" + "sync/atomic" + "unicode" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/packages" + "honnef.co/go/tools/config" +) + +type Documentation struct { + Title string + Text string + Since string + NonDefault bool + Options []string +} + +func (doc *Documentation) String() string { + b := &strings.Builder{} + fmt.Fprintf(b, "%s\n\n", doc.Title) + if doc.Text != "" { + fmt.Fprintf(b, "%s\n\n", doc.Text) + } + fmt.Fprint(b, "Available since\n ") + if doc.Since == "" { + fmt.Fprint(b, "unreleased") + } else { + fmt.Fprintf(b, "%s", doc.Since) + } + if doc.NonDefault { + fmt.Fprint(b, ", non-default") + } + fmt.Fprint(b, "\n") + if len(doc.Options) > 0 { + fmt.Fprintf(b, "\nOptions\n") + for _, opt := range doc.Options { + fmt.Fprintf(b, " %s", opt) + } + fmt.Fprint(b, "\n") + } + return b.String() +} + +type Ignore interface { + Match(p Problem) bool +} + +type LineIgnore struct { + File string + Line int + Checks []string + Matched bool + Pos token.Pos +} + +func (li *LineIgnore) Match(p Problem) bool { + pos := p.Pos + if pos.Filename != li.File || pos.Line != li.Line { + return false + } + for _, c := range li.Checks { + if m, _ := filepath.Match(c, p.Check); m { + li.Matched = true + return true + } + } + return false +} + +func (li *LineIgnore) String() string { + matched := "not matched" + if li.Matched { + matched = "matched" + } + return fmt.Sprintf("%s:%d %s (%s)", li.File, li.Line, strings.Join(li.Checks, ", "), matched) +} + +type FileIgnore struct { + File string + Checks []string +} + +func (fi *FileIgnore) Match(p Problem) bool { + if p.Pos.Filename != fi.File { + return false + } + for _, c := range fi.Checks { + if m, _ := filepath.Match(c, p.Check); m { + return true + } + } + return false +} + +type Severity uint8 + +const ( + Error Severity = iota + Warning + Ignored +) + +// Problem represents a problem in some source code. +type Problem struct { + Pos token.Position + End token.Position + Message string + Check string + Severity Severity +} + +func (p *Problem) String() string { + return fmt.Sprintf("%s (%s)", p.Message, p.Check) +} + +// A Linter lints Go source code. +type Linter struct { + Checkers []*analysis.Analyzer + CumulativeCheckers []CumulativeChecker + GoVersion int + Config config.Config + Stats Stats +} + +type CumulativeChecker interface { + Analyzer() *analysis.Analyzer + Result() []types.Object + ProblemObject(*token.FileSet, types.Object) Problem +} + +func (l *Linter) Lint(cfg *packages.Config, patterns []string) ([]Problem, error) { + var allAnalyzers []*analysis.Analyzer + allAnalyzers = append(allAnalyzers, l.Checkers...) + for _, cum := range l.CumulativeCheckers { + allAnalyzers = append(allAnalyzers, cum.Analyzer()) + } + + // The -checks command line flag overrules all configuration + // files, which means that for `-checks="foo"`, no check other + // than foo can ever be reported to the user. Make use of this + // fact to cull the list of analyses we need to run. + + // replace "inherit" with "all", as we don't want to base the + // list of all checks on the default configuration, which + // disables certain checks. + checks := make([]string, len(l.Config.Checks)) + copy(checks, l.Config.Checks) + for i, c := range checks { + if c == "inherit" { + checks[i] = "all" + } + } + + allowed := FilterChecks(allAnalyzers, checks) + var allowedAnalyzers []*analysis.Analyzer + for _, c := range l.Checkers { + if allowed[c.Name] { + allowedAnalyzers = append(allowedAnalyzers, c) + } + } + hasCumulative := false + for _, cum := range l.CumulativeCheckers { + a := cum.Analyzer() + if allowed[a.Name] { + hasCumulative = true + allowedAnalyzers = append(allowedAnalyzers, a) + } + } + + r, err := NewRunner(&l.Stats) + if err != nil { + return nil, err + } + r.goVersion = l.GoVersion + + pkgs, err := r.Run(cfg, patterns, allowedAnalyzers, hasCumulative) + if err != nil { + return nil, err + } + + tpkgToPkg := map[*types.Package]*Package{} + for _, pkg := range pkgs { + tpkgToPkg[pkg.Types] = pkg + + for _, e := range pkg.errs { + switch e := e.(type) { + case types.Error: + p := Problem{ + Pos: e.Fset.PositionFor(e.Pos, false), + Message: e.Msg, + Severity: Error, + Check: "compile", + } + pkg.problems = append(pkg.problems, p) + case packages.Error: + msg := e.Msg + if len(msg) != 0 && msg[0] == '\n' { + // TODO(dh): See https://github.com/golang/go/issues/32363 + msg = msg[1:] + } + + var pos token.Position + if e.Pos == "" { + // Under certain conditions (malformed package + // declarations, multiple packages in the same + // directory), go list emits an error on stderr + // instead of JSON. Those errors do not have + // associated position information in + // go/packages.Error, even though the output on + // stderr may contain it. + if p, n, err := parsePos(msg); err == nil { + if abs, err := filepath.Abs(p.Filename); err == nil { + p.Filename = abs + } + pos = p + msg = msg[n+2:] + } + } else { + var err error + pos, _, err = parsePos(e.Pos) + if err != nil { + panic(fmt.Sprintf("internal error: %s", e)) + } + } + p := Problem{ + Pos: pos, + Message: msg, + Severity: Error, + Check: "compile", + } + pkg.problems = append(pkg.problems, p) + case scanner.ErrorList: + for _, e := range e { + p := Problem{ + Pos: e.Pos, + Message: e.Msg, + Severity: Error, + Check: "compile", + } + pkg.problems = append(pkg.problems, p) + } + case error: + p := Problem{ + Pos: token.Position{}, + Message: e.Error(), + Severity: Error, + Check: "compile", + } + pkg.problems = append(pkg.problems, p) + } + } + } + + atomic.StoreUint32(&r.stats.State, StateCumulative) + var problems []Problem + for _, cum := range l.CumulativeCheckers { + for _, res := range cum.Result() { + pkg := tpkgToPkg[res.Pkg()] + allowedChecks := FilterChecks(allowedAnalyzers, pkg.cfg.Merge(l.Config).Checks) + if allowedChecks[cum.Analyzer().Name] { + pos := DisplayPosition(pkg.Fset, res.Pos()) + // FIXME(dh): why are we ignoring generated files + // here? Surely this is specific to 'unused', not all + // cumulative checkers + if _, ok := pkg.gen[pos.Filename]; ok { + continue + } + p := cum.ProblemObject(pkg.Fset, res) + problems = append(problems, p) + } + } + } + + for _, pkg := range pkgs { + for _, ig := range pkg.ignores { + for i := range pkg.problems { + p := &pkg.problems[i] + if ig.Match(*p) { + p.Severity = Ignored + } + } + for i := range problems { + p := &problems[i] + if ig.Match(*p) { + p.Severity = Ignored + } + } + } + + if pkg.cfg == nil { + // The package failed to load, otherwise we would have a + // valid config. Pass through all errors. + problems = append(problems, pkg.problems...) + } else { + for _, p := range pkg.problems { + allowedChecks := FilterChecks(allowedAnalyzers, pkg.cfg.Merge(l.Config).Checks) + allowedChecks["compile"] = true + if allowedChecks[p.Check] { + problems = append(problems, p) + } + } + } + + for _, ig := range pkg.ignores { + ig, ok := ig.(*LineIgnore) + if !ok { + continue + } + if ig.Matched { + continue + } + + couldveMatched := false + allowedChecks := FilterChecks(allowedAnalyzers, pkg.cfg.Merge(l.Config).Checks) + for _, c := range ig.Checks { + if !allowedChecks[c] { + continue + } + couldveMatched = true + break + } + + if !couldveMatched { + // The ignored checks were disabled for the containing package. + // Don't flag the ignore for not having matched. + continue + } + p := Problem{ + Pos: DisplayPosition(pkg.Fset, ig.Pos), + Message: "this linter directive didn't match anything; should it be removed?", + Check: "", + } + problems = append(problems, p) + } + } + + if len(problems) == 0 { + return nil, nil + } + + sort.Slice(problems, func(i, j int) bool { + pi := problems[i].Pos + pj := problems[j].Pos + + if pi.Filename != pj.Filename { + return pi.Filename < pj.Filename + } + if pi.Line != pj.Line { + return pi.Line < pj.Line + } + if pi.Column != pj.Column { + return pi.Column < pj.Column + } + + return problems[i].Message < problems[j].Message + }) + + var out []Problem + out = append(out, problems[0]) + for i, p := range problems[1:] { + // We may encounter duplicate problems because one file + // can be part of many packages. + if problems[i] != p { + out = append(out, p) + } + } + return out, nil +} + +func FilterChecks(allChecks []*analysis.Analyzer, checks []string) map[string]bool { + // OPT(dh): this entire computation could be cached per package + allowedChecks := map[string]bool{} + + for _, check := range checks { + b := true + if len(check) > 1 && check[0] == '-' { + b = false + check = check[1:] + } + if check == "*" || check == "all" { + // Match all + for _, c := range allChecks { + allowedChecks[c.Name] = b + } + } else if strings.HasSuffix(check, "*") { + // Glob + prefix := check[:len(check)-1] + isCat := strings.IndexFunc(prefix, func(r rune) bool { return unicode.IsNumber(r) }) == -1 + + for _, c := range allChecks { + idx := strings.IndexFunc(c.Name, func(r rune) bool { return unicode.IsNumber(r) }) + if isCat { + // Glob is S*, which should match S1000 but not SA1000 + cat := c.Name[:idx] + if prefix == cat { + allowedChecks[c.Name] = b + } + } else { + // Glob is S1* + if strings.HasPrefix(c.Name, prefix) { + allowedChecks[c.Name] = b + } + } + } + } else { + // Literal check name + allowedChecks[check] = b + } + } + return allowedChecks +} + +type Positioner interface { + Pos() token.Pos +} + +func DisplayPosition(fset *token.FileSet, p token.Pos) token.Position { + if p == token.NoPos { + return token.Position{} + } + + // Only use the adjusted position if it points to another Go file. + // This means we'll point to the original file for cgo files, but + // we won't point to a YACC grammar file. + pos := fset.PositionFor(p, false) + adjPos := fset.PositionFor(p, true) + + if filepath.Ext(adjPos.Filename) == ".go" { + return adjPos + } + return pos +} + +var bufferPool = &sync.Pool{ + New: func() interface{} { + buf := bytes.NewBuffer(nil) + buf.Grow(64) + return buf + }, +} + +func FuncName(f *types.Func) string { + buf := bufferPool.Get().(*bytes.Buffer) + buf.Reset() + if f.Type() != nil { + sig := f.Type().(*types.Signature) + if recv := sig.Recv(); recv != nil { + buf.WriteByte('(') + if _, ok := recv.Type().(*types.Interface); ok { + // gcimporter creates abstract methods of + // named interfaces using the interface type + // (not the named type) as the receiver. + // Don't print it in full. + buf.WriteString("interface") + } else { + types.WriteType(buf, recv.Type(), nil) + } + buf.WriteByte(')') + buf.WriteByte('.') + } else if f.Pkg() != nil { + writePackage(buf, f.Pkg()) + } + } + buf.WriteString(f.Name()) + s := buf.String() + bufferPool.Put(buf) + return s +} + +func writePackage(buf *bytes.Buffer, pkg *types.Package) { + if pkg == nil { + return + } + s := pkg.Path() + if s != "" { + buf.WriteString(s) + buf.WriteByte('.') + } +} diff --git a/vendor/github.com/golangci/go-tools/lint/lintdsl/lintdsl.go b/vendor/honnef.co/go/tools/lint/lintdsl/lintdsl.go similarity index 50% rename from vendor/github.com/golangci/go-tools/lint/lintdsl/lintdsl.go rename to vendor/honnef.co/go/tools/lint/lintdsl/lintdsl.go index f56eec6bdded..3b939e95f2ff 100644 --- a/vendor/github.com/golangci/go-tools/lint/lintdsl/lintdsl.go +++ b/vendor/honnef.co/go/tools/lint/lintdsl/lintdsl.go @@ -4,6 +4,7 @@ package lintdsl import ( "bytes" + "flag" "fmt" "go/ast" "go/constant" @@ -12,8 +13,10 @@ import ( "go/types" "strings" - "github.com/golangci/go-tools/lint" - "github.com/golangci/go-tools/ssa" + "golang.org/x/tools/go/analysis" + "honnef.co/go/tools/facts" + "honnef.co/go/tools/lint" + "honnef.co/go/tools/ssa" ) type packager interface { @@ -30,7 +33,7 @@ func CallName(call *ssa.CallCommon) string { if !ok { return "" } - return fn.FullName() + return lint.FuncName(fn) case *ssa.Builtin: return v.Name() } @@ -63,7 +66,7 @@ func IsExample(fn *ssa.Function) bool { func IsPointerLike(T types.Type) bool { switch T := T.Underlying().(type) { - case *types.Interface, *types.Chan, *types.Map, *types.Pointer: + case *types.Interface, *types.Chan, *types.Map, *types.Signature, *types.Pointer: return true case *types.Basic: return T.Kind() == types.UnsafePointer @@ -71,16 +74,6 @@ func IsPointerLike(T types.Type) bool { return false } -func IsGenerated(f *ast.File) bool { - comments := f.Comments - if len(comments) > 0 { - comment := comments[0].Text() - return strings.Contains(comment, "Code generated by") || - strings.Contains(comment, "DO NOT EDIT") - } - return false -} - func IsIdent(expr ast.Expr, ident string) bool { id, ok := expr.(*ast.Ident) return ok && id.Name == ident @@ -103,42 +96,26 @@ func IsZero(expr ast.Expr) bool { return IsIntLiteral(expr, "0") } -func TypeOf(j *lint.Job, expr ast.Expr) types.Type { - if expr == nil { - return nil - } - return j.NodePackage(expr).TypesInfo.TypeOf(expr) -} - -func IsOfType(j *lint.Job, expr ast.Expr, name string) bool { return IsType(TypeOf(j, expr), name) } - -func ObjectOf(j *lint.Job, ident *ast.Ident) types.Object { - if ident == nil { - return nil - } - return j.NodePackage(ident).TypesInfo.ObjectOf(ident) +func IsOfType(pass *analysis.Pass, expr ast.Expr, name string) bool { + return IsType(pass.TypesInfo.TypeOf(expr), name) } -func IsInTest(j *lint.Job, node lint.Positioner) bool { +func IsInTest(pass *analysis.Pass, node lint.Positioner) bool { // FIXME(dh): this doesn't work for global variables with // initializers - f := j.Program.SSA.Fset.File(node.Pos()) + f := pass.Fset.File(node.Pos()) return f != nil && strings.HasSuffix(f.Name(), "_test.go") } -func IsInMain(j *lint.Job, node lint.Positioner) bool { +func IsInMain(pass *analysis.Pass, node lint.Positioner) bool { if node, ok := node.(packager); ok { return node.Package().Pkg.Name() == "main" } - pkg := j.NodePackage(node) - if pkg == nil { - return false - } - return pkg.Types.Name() == "main" + return pass.Pkg.Name() == "main" } -func SelectorName(j *lint.Job, expr *ast.SelectorExpr) string { - info := j.NodePackage(expr).TypesInfo +func SelectorName(pass *analysis.Pass, expr *ast.SelectorExpr) string { + info := pass.TypesInfo sel := info.Selections[expr] if sel == nil { if x, ok := expr.X.(*ast.Ident); ok { @@ -154,16 +131,16 @@ func SelectorName(j *lint.Job, expr *ast.SelectorExpr) string { return fmt.Sprintf("(%s).%s", sel.Recv(), sel.Obj().Name()) } -func IsNil(j *lint.Job, expr ast.Expr) bool { - return j.NodePackage(expr).TypesInfo.Types[expr].IsNil() +func IsNil(pass *analysis.Pass, expr ast.Expr) bool { + return pass.TypesInfo.Types[expr].IsNil() } -func BoolConst(j *lint.Job, expr ast.Expr) bool { - val := j.NodePackage(expr).TypesInfo.ObjectOf(expr.(*ast.Ident)).(*types.Const).Val() +func BoolConst(pass *analysis.Pass, expr ast.Expr) bool { + val := pass.TypesInfo.ObjectOf(expr.(*ast.Ident)).(*types.Const).Val() return constant.BoolVal(val) } -func IsBoolConst(j *lint.Job, expr ast.Expr) bool { +func IsBoolConst(pass *analysis.Pass, expr ast.Expr) bool { // We explicitly don't support typed bools because more often than // not, custom bool types are used as binary enums and the // explicit comparison is desired. @@ -172,7 +149,7 @@ func IsBoolConst(j *lint.Job, expr ast.Expr) bool { if !ok { return false } - obj := j.NodePackage(expr).TypesInfo.ObjectOf(ident) + obj := pass.TypesInfo.ObjectOf(ident) c, ok := obj.(*types.Const) if !ok { return false @@ -187,8 +164,8 @@ func IsBoolConst(j *lint.Job, expr ast.Expr) bool { return true } -func ExprToInt(j *lint.Job, expr ast.Expr) (int64, bool) { - tv := j.NodePackage(expr).TypesInfo.Types[expr] +func ExprToInt(pass *analysis.Pass, expr ast.Expr) (int64, bool) { + tv := pass.TypesInfo.Types[expr] if tv.Value == nil { return 0, false } @@ -198,8 +175,8 @@ func ExprToInt(j *lint.Job, expr ast.Expr) (int64, bool) { return constant.Int64Val(tv.Value) } -func ExprToString(j *lint.Job, expr ast.Expr) (string, bool) { - val := j.NodePackage(expr).TypesInfo.Types[expr].Value +func ExprToString(pass *analysis.Pass, expr ast.Expr) (string, bool) { + val := pass.TypesInfo.Types[expr].Value if val == nil { return "", false } @@ -228,52 +205,63 @@ func DereferenceR(T types.Type) types.Type { return T } -func IsGoVersion(j *lint.Job, minor int) bool { - return j.Program.GoVersion >= minor +func IsGoVersion(pass *analysis.Pass, minor int) bool { + version := pass.Analyzer.Flags.Lookup("go").Value.(flag.Getter).Get().(int) + return version >= minor } -func CallNameAST(j *lint.Job, call *ast.CallExpr) string { - sel, ok := call.Fun.(*ast.SelectorExpr) - if !ok { - return "" - } - fn, ok := j.NodePackage(call).TypesInfo.ObjectOf(sel.Sel).(*types.Func) - if !ok { +func CallNameAST(pass *analysis.Pass, call *ast.CallExpr) string { + switch fun := call.Fun.(type) { + case *ast.SelectorExpr: + fn, ok := pass.TypesInfo.ObjectOf(fun.Sel).(*types.Func) + if !ok { + return "" + } + return lint.FuncName(fn) + case *ast.Ident: + obj := pass.TypesInfo.ObjectOf(fun) + switch obj := obj.(type) { + case *types.Func: + return lint.FuncName(obj) + case *types.Builtin: + return obj.Name() + default: + return "" + } + default: return "" } - return fn.FullName() } -func IsCallToAST(j *lint.Job, node ast.Node, name string) bool { +func IsCallToAST(pass *analysis.Pass, node ast.Node, name string) bool { call, ok := node.(*ast.CallExpr) if !ok { return false } - return CallNameAST(j, call) == name + return CallNameAST(pass, call) == name } -func IsCallToAnyAST(j *lint.Job, node ast.Node, names ...string) bool { +func IsCallToAnyAST(pass *analysis.Pass, node ast.Node, names ...string) bool { for _, name := range names { - if IsCallToAST(j, node, name) { + if IsCallToAST(pass, node, name) { return true } } return false } -func Render(j *lint.Job, x interface{}) string { - fset := j.Program.SSA.Fset +func Render(pass *analysis.Pass, x interface{}) string { var buf bytes.Buffer - if err := printer.Fprint(&buf, fset, x); err != nil { + if err := printer.Fprint(&buf, pass.Fset, x); err != nil { panic(err) } return buf.String() } -func RenderArgs(j *lint.Job, args []ast.Expr) string { +func RenderArgs(pass *analysis.Pass, args []ast.Expr) string { var ss []string for _, arg := range args { - ss = append(ss, Render(j, arg)) + ss = append(ss, Render(pass, arg)) } return strings.Join(ss, ", ") } @@ -300,11 +288,10 @@ func Inspect(node ast.Node, fn func(node ast.Node) bool) { ast.Inspect(node, fn) } -func GroupSpecs(j *lint.Job, specs []ast.Spec) [][]ast.Spec { +func GroupSpecs(fset *token.FileSet, specs []ast.Spec) [][]ast.Spec { if len(specs) == 0 { return nil } - fset := j.Program.SSA.Fset groups := make([][]ast.Spec, 1) groups[0] = append(groups[0], specs[0]) @@ -321,3 +308,93 @@ func GroupSpecs(j *lint.Job, specs []ast.Spec) [][]ast.Spec { return groups } + +func IsObject(obj types.Object, name string) bool { + var path string + if pkg := obj.Pkg(); pkg != nil { + path = pkg.Path() + "." + } + return path+obj.Name() == name +} + +type Field struct { + Var *types.Var + Tag string + Path []int +} + +// FlattenFields recursively flattens T and embedded structs, +// returning a list of fields. If multiple fields with the same name +// exist, all will be returned. +func FlattenFields(T *types.Struct) []Field { + return flattenFields(T, nil, nil) +} + +func flattenFields(T *types.Struct, path []int, seen map[types.Type]bool) []Field { + if seen == nil { + seen = map[types.Type]bool{} + } + if seen[T] { + return nil + } + seen[T] = true + var out []Field + for i := 0; i < T.NumFields(); i++ { + field := T.Field(i) + tag := T.Tag(i) + np := append(path[:len(path):len(path)], i) + if field.Anonymous() { + if s, ok := Dereference(field.Type()).Underlying().(*types.Struct); ok { + out = append(out, flattenFields(s, np, seen)...) + } + } else { + out = append(out, Field{field, tag, np}) + } + } + return out +} + +func File(pass *analysis.Pass, node lint.Positioner) *ast.File { + pass.Fset.PositionFor(node.Pos(), true) + m := pass.ResultOf[facts.TokenFile].(map[*token.File]*ast.File) + return m[pass.Fset.File(node.Pos())] +} + +// IsGenerated reports whether pos is in a generated file, It ignores +// //line directives. +func IsGenerated(pass *analysis.Pass, pos token.Pos) bool { + _, ok := Generator(pass, pos) + return ok +} + +// Generator returns the generator that generated the file containing +// pos. It ignores //line directives. +func Generator(pass *analysis.Pass, pos token.Pos) (facts.Generator, bool) { + file := pass.Fset.PositionFor(pos, false).Filename + m := pass.ResultOf[facts.Generated].(map[string]facts.Generator) + g, ok := m[file] + return g, ok +} + +func ReportfFG(pass *analysis.Pass, pos token.Pos, f string, args ...interface{}) { + file := lint.DisplayPosition(pass.Fset, pos).Filename + m := pass.ResultOf[facts.Generated].(map[string]facts.Generator) + if _, ok := m[file]; ok { + return + } + pass.Reportf(pos, f, args...) +} + +func ReportNodef(pass *analysis.Pass, node ast.Node, format string, args ...interface{}) { + msg := fmt.Sprintf(format, args...) + pass.Report(analysis.Diagnostic{Pos: node.Pos(), End: node.End(), Message: msg}) +} + +func ReportNodefFG(pass *analysis.Pass, node ast.Node, format string, args ...interface{}) { + file := lint.DisplayPosition(pass.Fset, node.Pos()).Filename + m := pass.ResultOf[facts.Generated].(map[string]facts.Generator) + if _, ok := m[file]; ok { + return + } + ReportNodef(pass, node, format, args...) +} diff --git a/vendor/github.com/golangci/go-tools/lint/lintutil/format/format.go b/vendor/honnef.co/go/tools/lint/lintutil/format/format.go similarity index 77% rename from vendor/github.com/golangci/go-tools/lint/lintutil/format/format.go rename to vendor/honnef.co/go/tools/lint/lintutil/format/format.go index 96befb1cc317..9385431f88b8 100644 --- a/vendor/github.com/golangci/go-tools/lint/lintutil/format/format.go +++ b/vendor/honnef.co/go/tools/lint/lintutil/format/format.go @@ -10,7 +10,7 @@ import ( "path/filepath" "text/tabwriter" - "github.com/golangci/go-tools/lint" + "honnef.co/go/tools/lint" ) func shortPath(path string) string { @@ -51,7 +51,7 @@ type Text struct { } func (o Text) Format(p lint.Problem) { - fmt.Fprintf(o.W, "%v: %s\n", relativePositionString(p.Position), p.String()) + fmt.Fprintf(o.W, "%v: %s\n", relativePositionString(p.Pos), p.String()) } type JSON struct { @@ -80,16 +80,22 @@ func (o JSON) Format(p lint.Problem) { Code string `json:"code"` Severity string `json:"severity,omitempty"` Location location `json:"location"` + End location `json:"end"` Message string `json:"message"` }{ Code: p.Check, Severity: severity(p.Severity), Location: location{ - File: p.Position.Filename, - Line: p.Position.Line, - Column: p.Position.Column, + File: p.Pos.Filename, + Line: p.Pos.Line, + Column: p.Pos.Column, }, - Message: p.Text, + End: location{ + File: p.End.Filename, + Line: p.End.Line, + Column: p.End.Column, + }, + Message: p.Message, } _ = json.NewEncoder(o.W).Encode(jp) } @@ -102,20 +108,21 @@ type Stylish struct { } func (o *Stylish) Format(p lint.Problem) { - if p.Position.Filename == "" { - p.Position.Filename = "-" + pos := p.Pos + if pos.Filename == "" { + pos.Filename = "-" } - if p.Position.Filename != o.prevFile { + if pos.Filename != o.prevFile { if o.prevFile != "" { o.tw.Flush() fmt.Fprintln(o.W) } - fmt.Fprintln(o.W, p.Position.Filename) - o.prevFile = p.Position.Filename + fmt.Fprintln(o.W, pos.Filename) + o.prevFile = pos.Filename o.tw = tabwriter.NewWriter(o.W, 0, 4, 2, ' ', 0) } - fmt.Fprintf(o.tw, " (%d, %d)\t%s\t%s\n", p.Position.Line, p.Position.Column, p.Check, p.Text) + fmt.Fprintf(o.tw, " (%d, %d)\t%s\t%s\n", pos.Line, pos.Column, p.Check, p.Message) } func (o *Stylish) Stats(total, errors, warnings int) { diff --git a/vendor/honnef.co/go/tools/lint/lintutil/stats.go b/vendor/honnef.co/go/tools/lint/lintutil/stats.go new file mode 100644 index 000000000000..ba8caf0afddb --- /dev/null +++ b/vendor/honnef.co/go/tools/lint/lintutil/stats.go @@ -0,0 +1,7 @@ +// +build !aix,!android,!darwin,!dragonfly,!freebsd,!linux,!netbsd,!openbsd,!solaris + +package lintutil + +import "os" + +var infoSignals = []os.Signal{} diff --git a/vendor/honnef.co/go/tools/lint/lintutil/stats_bsd.go b/vendor/honnef.co/go/tools/lint/lintutil/stats_bsd.go new file mode 100644 index 000000000000..3a62ede031c3 --- /dev/null +++ b/vendor/honnef.co/go/tools/lint/lintutil/stats_bsd.go @@ -0,0 +1,10 @@ +// +build darwin dragonfly freebsd netbsd openbsd + +package lintutil + +import ( + "os" + "syscall" +) + +var infoSignals = []os.Signal{syscall.SIGINFO} diff --git a/vendor/honnef.co/go/tools/lint/lintutil/stats_posix.go b/vendor/honnef.co/go/tools/lint/lintutil/stats_posix.go new file mode 100644 index 000000000000..53f21c666b12 --- /dev/null +++ b/vendor/honnef.co/go/tools/lint/lintutil/stats_posix.go @@ -0,0 +1,10 @@ +// +build aix android linux solaris + +package lintutil + +import ( + "os" + "syscall" +) + +var infoSignals = []os.Signal{syscall.SIGUSR1} diff --git a/vendor/github.com/golangci/go-tools/lint/lintutil/util.go b/vendor/honnef.co/go/tools/lint/lintutil/util.go similarity index 50% rename from vendor/github.com/golangci/go-tools/lint/lintutil/util.go rename to vendor/honnef.co/go/tools/lint/lintutil/util.go index 93edd41d4a2c..fe0279f921ce 100644 --- a/vendor/github.com/golangci/go-tools/lint/lintutil/util.go +++ b/vendor/honnef.co/go/tools/lint/lintutil/util.go @@ -5,67 +5,55 @@ // https://developers.google.com/open-source/licenses/bsd. // Package lintutil provides helpers for writing linter command lines. -package lintutil // import "github.com/golangci/go-tools/lint/lintutil" +package lintutil // import "honnef.co/go/tools/lint/lintutil" import ( + "crypto/sha256" "errors" "flag" "fmt" "go/build" "go/token" + "io" "log" "os" + "os/signal" "regexp" "runtime" "runtime/pprof" "strconv" "strings" - "time" + "sync/atomic" - "github.com/golangci/go-tools/config" - "github.com/golangci/go-tools/lint" - "github.com/golangci/go-tools/lint/lintutil/format" - "github.com/golangci/go-tools/version" + "honnef.co/go/tools/config" + "honnef.co/go/tools/internal/cache" + "honnef.co/go/tools/lint" + "honnef.co/go/tools/lint/lintutil/format" + "honnef.co/go/tools/version" + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/buildutil" "golang.org/x/tools/go/packages" ) -func usage(name string, flags *flag.FlagSet) func() { - return func() { - fmt.Fprintf(os.Stderr, "Usage of %s:\n", name) - fmt.Fprintf(os.Stderr, "\t%s [flags] # runs on package in current directory\n", name) - fmt.Fprintf(os.Stderr, "\t%s [flags] packages\n", name) - fmt.Fprintf(os.Stderr, "\t%s [flags] directory\n", name) - fmt.Fprintf(os.Stderr, "\t%s [flags] files... # must be a single package\n", name) - fmt.Fprintf(os.Stderr, "Flags:\n") - flags.PrintDefaults() - } -} - -func parseIgnore(s string) ([]lint.Ignore, error) { - var out []lint.Ignore - if len(s) == 0 { - return nil, nil - } - for _, part := range strings.Fields(s) { - p := strings.Split(part, ":") - if len(p) != 2 { - return nil, errors.New("malformed ignore string") - } - path := p[0] - checks := strings.Split(p[1], ",") - out = append(out, &lint.GlobIgnore{Pattern: path, Checks: checks}) +func NewVersionFlag() flag.Getter { + tags := build.Default.ReleaseTags + v := tags[len(tags)-1][2:] + version := new(VersionFlag) + if err := version.Set(v); err != nil { + panic(fmt.Sprintf("internal error: %s", err)) } - return out, nil + return version } -type versionFlag int +type VersionFlag int -func (v *versionFlag) String() string { +func (v *VersionFlag) String() string { return fmt.Sprintf("1.%d", *v) + } -func (v *versionFlag) Set(s string) error { +func (v *VersionFlag) Set(s string) error { if len(s) < 3 { return errors.New("invalid Go version") } @@ -76,14 +64,26 @@ func (v *versionFlag) Set(s string) error { return errors.New("invalid Go version") } i, err := strconv.Atoi(s[2:]) - *v = versionFlag(i) + *v = VersionFlag(i) return err } -func (v *versionFlag) Get() interface{} { +func (v *VersionFlag) Get() interface{} { return int(*v) } +func usage(name string, flags *flag.FlagSet) func() { + return func() { + fmt.Fprintf(os.Stderr, "Usage of %s:\n", name) + fmt.Fprintf(os.Stderr, "\t%s [flags] # runs on package in current directory\n", name) + fmt.Fprintf(os.Stderr, "\t%s [flags] packages\n", name) + fmt.Fprintf(os.Stderr, "\t%s [flags] directory\n", name) + fmt.Fprintf(os.Stderr, "\t%s [flags] files... # must be a single package\n", name) + fmt.Fprintf(os.Stderr, "Flags:\n") + flags.PrintDefaults() + } +} + type list []string func (list *list) String() string { @@ -104,16 +104,16 @@ func FlagSet(name string) *flag.FlagSet { flags := flag.NewFlagSet("", flag.ExitOnError) flags.Usage = usage(name, flags) flags.String("tags", "", "List of `build tags`") - flags.String("ignore", "", "Deprecated: use linter directives instead") flags.Bool("tests", true, "Include tests") flags.Bool("version", false, "Print version and exit") flags.Bool("show-ignored", false, "Don't filter ignored problems") flags.String("f", "text", "Output `format` (valid choices are 'stylish', 'text' and 'json')") + flags.String("explain", "", "Print description of `check`") - flags.Int("debug.max-concurrent-jobs", 0, "Number of jobs to run concurrently") - flags.Bool("debug.print-stats", false, "Print debug statistics") flags.String("debug.cpuprofile", "", "Write CPU profile to `file`") flags.String("debug.memprofile", "", "Write memory profile to `file`") + flags.Bool("debug.version", false, "Print detailed version information about this program") + flags.Bool("debug.no-compile-errors", false, "Don't print compile errors") checks := list{"inherit"} fail := list{"all"} @@ -122,7 +122,7 @@ func FlagSet(name string) *flag.FlagSet { tags := build.Default.ReleaseTags v := tags[len(tags)-1][2:] - version := new(versionFlag) + version := new(VersionFlag) if err := version.Set(v); err != nil { panic(fmt.Sprintf("internal error: %s", err)) } @@ -131,19 +131,28 @@ func FlagSet(name string) *flag.FlagSet { return flags } -func ProcessFlagSet(cs []lint.Checker, fs *flag.FlagSet) { +func findCheck(cs []*analysis.Analyzer, check string) (*analysis.Analyzer, bool) { + for _, c := range cs { + if c.Name == check { + return c, true + } + } + return nil, false +} + +func ProcessFlagSet(cs []*analysis.Analyzer, cums []lint.CumulativeChecker, fs *flag.FlagSet) { tags := fs.Lookup("tags").Value.(flag.Getter).Get().(string) - ignore := fs.Lookup("ignore").Value.(flag.Getter).Get().(string) tests := fs.Lookup("tests").Value.(flag.Getter).Get().(bool) goVersion := fs.Lookup("go").Value.(flag.Getter).Get().(int) formatter := fs.Lookup("f").Value.(flag.Getter).Get().(string) printVersion := fs.Lookup("version").Value.(flag.Getter).Get().(bool) showIgnored := fs.Lookup("show-ignored").Value.(flag.Getter).Get().(bool) + explain := fs.Lookup("explain").Value.(flag.Getter).Get().(string) - maxConcurrentJobs := fs.Lookup("debug.max-concurrent-jobs").Value.(flag.Getter).Get().(int) - printStats := fs.Lookup("debug.print-stats").Value.(flag.Getter).Get().(bool) cpuProfile := fs.Lookup("debug.cpuprofile").Value.(flag.Getter).Get().(string) memProfile := fs.Lookup("debug.memprofile").Value.(flag.Getter).Get().(string) + debugVersion := fs.Lookup("debug.version").Value.(flag.Getter).Get().(bool) + debugNoCompile := fs.Lookup("debug.no-compile-errors").Value.(flag.Getter).Get().(bool) cfg := config.Config{} cfg.Checks = *fs.Lookup("checks").Value.(*list) @@ -170,21 +179,49 @@ func ProcessFlagSet(cs []lint.Checker, fs *flag.FlagSet) { pprof.StartCPUProfile(f) } + if debugVersion { + version.Verbose() + exit(0) + } + if printVersion { version.Print() exit(0) } - ps, err := Lint(cs, fs.Args(), &Options{ - Tags: strings.Fields(tags), - LintTests: tests, - Ignores: ignore, - GoVersion: goVersion, - ReturnIgnored: showIgnored, - Config: cfg, + // Validate that the tags argument is well-formed. go/packages + // doesn't detect malformed build flags and returns unhelpful + // errors. + tf := buildutil.TagsFlag{} + if err := tf.Set(tags); err != nil { + fmt.Fprintln(os.Stderr, fmt.Errorf("invalid value %q for flag -tags: %s", tags, err)) + exit(1) + } - MaxConcurrentJobs: maxConcurrentJobs, - PrintStats: printStats, + if explain != "" { + var haystack []*analysis.Analyzer + haystack = append(haystack, cs...) + for _, cum := range cums { + haystack = append(haystack, cum.Analyzer()) + } + check, ok := findCheck(haystack, explain) + if !ok { + fmt.Fprintln(os.Stderr, "Couldn't find check", explain) + exit(1) + } + if check.Doc == "" { + fmt.Fprintln(os.Stderr, explain, "has no documentation") + exit(1) + } + fmt.Println(check.Doc) + exit(0) + } + + ps, err := Lint(cs, cums, fs.Args(), &Options{ + Tags: tags, + LintTests: tests, + GoVersion: goVersion, + Config: cfg, }) if err != nil { fmt.Fprintln(os.Stderr, err) @@ -211,15 +248,22 @@ func ProcessFlagSet(cs []lint.Checker, fs *flag.FlagSet) { ) fail := *fs.Lookup("fail").Value.(*list) - var allChecks []string - for _, p := range ps { - allChecks = append(allChecks, p.Check) + analyzers := make([]*analysis.Analyzer, len(cs), len(cs)+len(cums)) + copy(analyzers, cs) + for _, cum := range cums { + analyzers = append(analyzers, cum.Analyzer()) } - - shouldExit := lint.FilterChecks(allChecks, fail) + shouldExit := lint.FilterChecks(analyzers, fail) + shouldExit["compile"] = true total = len(ps) for _, p := range ps { + if p.Check == "compile" && debugNoCompile { + continue + } + if p.Severity == lint.Ignored && !showIgnored { + continue + } if shouldExit[p.Check] { errors++ } else { @@ -234,79 +278,97 @@ func ProcessFlagSet(cs []lint.Checker, fs *flag.FlagSet) { if errors > 0 { exit(1) } + exit(0) } type Options struct { Config config.Config - Tags []string - LintTests bool - Ignores string - GoVersion int - ReturnIgnored bool - - MaxConcurrentJobs int - PrintStats bool + Tags string + LintTests bool + GoVersion int } -func Lint(cs []lint.Checker, paths []string, opt *Options) ([]lint.Problem, error) { - stats := lint.PerfStats{ - CheckerInits: map[string]time.Duration{}, +func computeSalt() ([]byte, error) { + if version.Version != "devel" { + return []byte(version.Version), nil } - - if opt == nil { - opt = &Options{} - } - ignores, err := parseIgnore(opt.Ignores) + p, err := os.Executable() if err != nil { return nil, err } - - conf := &packages.Config{ - Mode: packages.LoadAllSyntax, - Tests: opt.LintTests, - BuildFlags: []string{ - "-tags=" + strings.Join(opt.Tags, " "), - }, - } - - t := time.Now() - if len(paths) == 0 { - paths = []string{"."} - } - pkgs, err := packages.Load(conf, paths...) + f, err := os.Open(p) if err != nil { return nil, err } - stats.PackageLoading = time.Since(t) + defer f.Close() + h := sha256.New() + if _, err := io.Copy(h, f); err != nil { + return nil, err + } + return h.Sum(nil), nil +} - var problems []lint.Problem - workingPkgs := make([]*packages.Package, 0, len(pkgs)) - for _, pkg := range pkgs { - if pkg.IllTyped { - problems = append(problems, compileErrors(pkg)...) - } else { - workingPkgs = append(workingPkgs, pkg) - } +func Lint(cs []*analysis.Analyzer, cums []lint.CumulativeChecker, paths []string, opt *Options) ([]lint.Problem, error) { + salt, err := computeSalt() + if err != nil { + return nil, fmt.Errorf("could not compute salt for cache: %s", err) } + cache.SetSalt(salt) - if len(workingPkgs) == 0 { - return problems, nil + if opt == nil { + opt = &Options{} } l := &lint.Linter{ - Checkers: cs, - Ignores: ignores, - GoVersion: opt.GoVersion, - ReturnIgnored: opt.ReturnIgnored, - Config: opt.Config, - - MaxConcurrentJobs: opt.MaxConcurrentJobs, - PrintStats: opt.PrintStats, + Checkers: cs, + CumulativeCheckers: cums, + GoVersion: opt.GoVersion, + Config: opt.Config, + } + cfg := &packages.Config{} + if opt.LintTests { + cfg.Tests = true + } + if opt.Tags != "" { + cfg.BuildFlags = append(cfg.BuildFlags, "-tags", opt.Tags) + } + + printStats := func() { + // Individual stats are read atomically, but overall there + // is no synchronisation. For printing rough progress + // information, this doesn't matter. + switch atomic.LoadUint32(&l.Stats.State) { + case lint.StateInitializing: + fmt.Fprintln(os.Stderr, "Status: initializing") + case lint.StateGraph: + fmt.Fprintln(os.Stderr, "Status: loading package graph") + case lint.StateProcessing: + fmt.Fprintf(os.Stderr, "Packages: %d/%d initial, %d/%d total; Workers: %d/%d; Problems: %d\n", + atomic.LoadUint32(&l.Stats.ProcessedInitialPackages), + atomic.LoadUint32(&l.Stats.InitialPackages), + atomic.LoadUint32(&l.Stats.ProcessedPackages), + atomic.LoadUint32(&l.Stats.TotalPackages), + atomic.LoadUint32(&l.Stats.ActiveWorkers), + atomic.LoadUint32(&l.Stats.TotalWorkers), + atomic.LoadUint32(&l.Stats.Problems), + ) + case lint.StateCumulative: + fmt.Fprintln(os.Stderr, "Status: processing cumulative checkers") + } + } + if len(infoSignals) > 0 { + ch := make(chan os.Signal, 1) + signal.Notify(ch, infoSignals...) + defer signal.Stop(ch) + go func() { + for range ch { + printStats() + } + }() } - problems = append(problems, l.Lint(workingPkgs, &stats)...) - return problems, nil + return l.Lint(cfg, paths) } var posRe = regexp.MustCompile(`^(.+?):(\d+)(?::(\d+)?)?$`) @@ -328,35 +390,3 @@ func parsePos(pos string) token.Position { Column: col, } } - -func compileErrors(pkg *packages.Package) []lint.Problem { - if !pkg.IllTyped { - return nil - } - if len(pkg.Errors) == 0 { - // transitively ill-typed - var ps []lint.Problem - for _, imp := range pkg.Imports { - ps = append(ps, compileErrors(imp)...) - } - return ps - } - var ps []lint.Problem - for _, err := range pkg.Errors { - p := lint.Problem{ - Position: parsePos(err.Pos), - Text: err.Msg, - Checker: "compiler", - Check: "compile", - } - ps = append(ps, p) - } - return ps -} - -func ProcessArgs(name string, cs []lint.Checker, args []string) { - flags := FlagSet(name) - flags.Parse(args) - - ProcessFlagSet(cs, flags) -} diff --git a/vendor/honnef.co/go/tools/lint/runner.go b/vendor/honnef.co/go/tools/lint/runner.go new file mode 100644 index 000000000000..3b22a63fa219 --- /dev/null +++ b/vendor/honnef.co/go/tools/lint/runner.go @@ -0,0 +1,970 @@ +package lint + +/* +Parallelism + +Runner implements parallel processing of packages by spawning one +goroutine per package in the dependency graph, without any semaphores. +Each goroutine initially waits on the completion of all of its +dependencies, thus establishing correct order of processing. Once all +dependencies finish processing, the goroutine will load the package +from export data or source – this loading is guarded by a semaphore, +sized according to the number of CPU cores. This way, we only have as +many packages occupying memory and CPU resources as there are actual +cores to process them. + +This combination of unbounded goroutines but bounded package loading +means that if we have many parallel, independent subgraphs, they will +all execute in parallel, while not wasting resources for long linear +chains or trying to process more subgraphs in parallel than the system +can handle. + +*/ + +import ( + "bytes" + "encoding/gob" + "encoding/hex" + "fmt" + "go/ast" + "go/token" + "go/types" + "reflect" + "regexp" + "runtime" + "sort" + "strconv" + "strings" + "sync" + "sync/atomic" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/packages" + "golang.org/x/tools/go/types/objectpath" + "honnef.co/go/tools/config" + "honnef.co/go/tools/facts" + "honnef.co/go/tools/internal/cache" + "honnef.co/go/tools/loader" +) + +// If enabled, abuse of the go/analysis API will lead to panics +const sanityCheck = true + +// OPT(dh): for a dependency tree A->B->C->D, if we have cached data +// for B, there should be no need to load C and D individually. Go's +// export data for B contains all the data we need on types, and our +// fact cache could store the union of B, C and D in B. +// +// This may change unused's behavior, however, as it may observe fewer +// interfaces from transitive dependencies. + +type Package struct { + dependents uint64 + + *packages.Package + Imports []*Package + initial bool + fromSource bool + hash string + done chan struct{} + + resultsMu sync.Mutex + // results maps analyzer IDs to analyzer results + results []*result + + cfg *config.Config + gen map[string]facts.Generator + problems []Problem + ignores []Ignore + errs []error + + // these slices are indexed by analysis + facts []map[types.Object][]analysis.Fact + pkgFacts [][]analysis.Fact + + canClearTypes bool +} + +func (pkg *Package) decUse() { + atomic.AddUint64(&pkg.dependents, ^uint64(0)) + if atomic.LoadUint64(&pkg.dependents) == 0 { + // nobody depends on this package anymore + if pkg.canClearTypes { + pkg.Types = nil + } + pkg.facts = nil + pkg.pkgFacts = nil + + for _, imp := range pkg.Imports { + imp.decUse() + } + } +} + +type result struct { + v interface{} + err error + ready chan struct{} +} + +type Runner struct { + ld loader.Loader + cache *cache.Cache + + analyzerIDs analyzerIDs + + // limits parallelism of loading packages + loadSem chan struct{} + + goVersion int + stats *Stats +} + +type analyzerIDs struct { + m map[*analysis.Analyzer]int +} + +func (ids analyzerIDs) get(a *analysis.Analyzer) int { + id, ok := ids.m[a] + if !ok { + panic(fmt.Sprintf("no analyzer ID for %s", a.Name)) + } + return id +} + +type Fact struct { + Path string + Fact analysis.Fact +} + +type analysisAction struct { + analyzer *analysis.Analyzer + analyzerID int + pkg *Package + newPackageFacts []analysis.Fact + problems []Problem + + pkgFacts map[*types.Package][]analysis.Fact +} + +func (ac *analysisAction) String() string { + return fmt.Sprintf("%s @ %s", ac.analyzer, ac.pkg) +} + +func (ac *analysisAction) allObjectFacts() []analysis.ObjectFact { + out := make([]analysis.ObjectFact, 0, len(ac.pkg.facts[ac.analyzerID])) + for obj, facts := range ac.pkg.facts[ac.analyzerID] { + for _, fact := range facts { + out = append(out, analysis.ObjectFact{ + Object: obj, + Fact: fact, + }) + } + } + return out +} + +func (ac *analysisAction) allPackageFacts() []analysis.PackageFact { + out := make([]analysis.PackageFact, 0, len(ac.pkgFacts)) + for pkg, facts := range ac.pkgFacts { + for _, fact := range facts { + out = append(out, analysis.PackageFact{ + Package: pkg, + Fact: fact, + }) + } + } + return out +} + +func (ac *analysisAction) importObjectFact(obj types.Object, fact analysis.Fact) bool { + if sanityCheck && len(ac.analyzer.FactTypes) == 0 { + panic("analysis doesn't export any facts") + } + for _, f := range ac.pkg.facts[ac.analyzerID][obj] { + if reflect.TypeOf(f) == reflect.TypeOf(fact) { + reflect.ValueOf(fact).Elem().Set(reflect.ValueOf(f).Elem()) + return true + } + } + return false +} + +func (ac *analysisAction) importPackageFact(pkg *types.Package, fact analysis.Fact) bool { + if sanityCheck && len(ac.analyzer.FactTypes) == 0 { + panic("analysis doesn't export any facts") + } + for _, f := range ac.pkgFacts[pkg] { + if reflect.TypeOf(f) == reflect.TypeOf(fact) { + reflect.ValueOf(fact).Elem().Set(reflect.ValueOf(f).Elem()) + return true + } + } + return false +} + +func (ac *analysisAction) exportObjectFact(obj types.Object, fact analysis.Fact) { + if sanityCheck && len(ac.analyzer.FactTypes) == 0 { + panic("analysis doesn't export any facts") + } + ac.pkg.facts[ac.analyzerID][obj] = append(ac.pkg.facts[ac.analyzerID][obj], fact) +} + +func (ac *analysisAction) exportPackageFact(fact analysis.Fact) { + if sanityCheck && len(ac.analyzer.FactTypes) == 0 { + panic("analysis doesn't export any facts") + } + ac.pkgFacts[ac.pkg.Types] = append(ac.pkgFacts[ac.pkg.Types], fact) + ac.newPackageFacts = append(ac.newPackageFacts, fact) +} + +func (ac *analysisAction) report(pass *analysis.Pass, d analysis.Diagnostic) { + p := Problem{ + Pos: DisplayPosition(pass.Fset, d.Pos), + End: DisplayPosition(pass.Fset, d.End), + Message: d.Message, + Check: pass.Analyzer.Name, + } + ac.problems = append(ac.problems, p) +} + +func (r *Runner) runAnalysis(ac *analysisAction) (ret interface{}, err error) { + ac.pkg.resultsMu.Lock() + res := ac.pkg.results[r.analyzerIDs.get(ac.analyzer)] + if res != nil { + ac.pkg.resultsMu.Unlock() + <-res.ready + return res.v, res.err + } else { + res = &result{ + ready: make(chan struct{}), + } + ac.pkg.results[r.analyzerIDs.get(ac.analyzer)] = res + ac.pkg.resultsMu.Unlock() + + defer func() { + res.v = ret + res.err = err + close(res.ready) + }() + + pass := new(analysis.Pass) + *pass = analysis.Pass{ + Analyzer: ac.analyzer, + Fset: ac.pkg.Fset, + Files: ac.pkg.Syntax, + // type information may be nil or may be populated. if it is + // nil, it will get populated later. + Pkg: ac.pkg.Types, + TypesInfo: ac.pkg.TypesInfo, + TypesSizes: ac.pkg.TypesSizes, + ResultOf: map[*analysis.Analyzer]interface{}{}, + ImportObjectFact: ac.importObjectFact, + ImportPackageFact: ac.importPackageFact, + ExportObjectFact: ac.exportObjectFact, + ExportPackageFact: ac.exportPackageFact, + Report: func(d analysis.Diagnostic) { + ac.report(pass, d) + }, + AllObjectFacts: ac.allObjectFacts, + AllPackageFacts: ac.allPackageFacts, + } + + if !ac.pkg.initial { + // Don't report problems in dependencies + pass.Report = func(analysis.Diagnostic) {} + } + return r.runAnalysisUser(pass, ac) + } +} + +func (r *Runner) loadCachedFacts(a *analysis.Analyzer, pkg *Package) ([]Fact, bool) { + if len(a.FactTypes) == 0 { + return nil, true + } + + var facts []Fact + // Look in the cache for facts + aID, err := passActionID(pkg, a) + if err != nil { + return nil, false + } + aID = cache.Subkey(aID, "facts") + b, _, err := r.cache.GetBytes(aID) + if err != nil { + // No cached facts, analyse this package like a user-provided one, but ignore diagnostics + return nil, false + } + + if err := gob.NewDecoder(bytes.NewReader(b)).Decode(&facts); err != nil { + // Cached facts are broken, analyse this package like a user-provided one, but ignore diagnostics + return nil, false + } + return facts, true +} + +type dependencyError struct { + dep string + err error +} + +func (err dependencyError) nested() dependencyError { + if o, ok := err.err.(dependencyError); ok { + return o.nested() + } + return err +} + +func (err dependencyError) Error() string { + if o, ok := err.err.(dependencyError); ok { + return o.Error() + } + return fmt.Sprintf("error running dependency %s: %s", err.dep, err.err) +} + +func (r *Runner) makeAnalysisAction(a *analysis.Analyzer, pkg *Package) *analysisAction { + aid := r.analyzerIDs.get(a) + ac := &analysisAction{ + analyzer: a, + analyzerID: aid, + pkg: pkg, + } + + if len(a.FactTypes) == 0 { + return ac + } + + // Merge all package facts of dependencies + ac.pkgFacts = map[*types.Package][]analysis.Fact{} + seen := map[*Package]struct{}{} + var dfs func(*Package) + dfs = func(pkg *Package) { + if _, ok := seen[pkg]; ok { + return + } + seen[pkg] = struct{}{} + s := pkg.pkgFacts[aid] + ac.pkgFacts[pkg.Types] = s[0:len(s):len(s)] + for _, imp := range pkg.Imports { + dfs(imp) + } + } + dfs(pkg) + + return ac +} + +// analyzes that we always want to run, even if they're not being run +// explicitly or as dependencies. these are necessary for the inner +// workings of the runner. +var injectedAnalyses = []*analysis.Analyzer{facts.Generated, config.Analyzer} + +func (r *Runner) runAnalysisUser(pass *analysis.Pass, ac *analysisAction) (interface{}, error) { + if !ac.pkg.fromSource { + panic(fmt.Sprintf("internal error: %s was not loaded from source", ac.pkg)) + } + + // User-provided package, analyse it + // First analyze it with dependencies + for _, req := range ac.analyzer.Requires { + acReq := r.makeAnalysisAction(req, ac.pkg) + ret, err := r.runAnalysis(acReq) + if err != nil { + // We couldn't run a dependency, no point in going on + return nil, dependencyError{req.Name, err} + } + + pass.ResultOf[req] = ret + } + + // Then with this analyzer + ret, err := ac.analyzer.Run(pass) + if err != nil { + return nil, err + } + + if len(ac.analyzer.FactTypes) > 0 { + // Merge new facts into the package and persist them. + var facts []Fact + for _, fact := range ac.newPackageFacts { + id := r.analyzerIDs.get(ac.analyzer) + ac.pkg.pkgFacts[id] = append(ac.pkg.pkgFacts[id], fact) + facts = append(facts, Fact{"", fact}) + } + for obj, afacts := range ac.pkg.facts[ac.analyzerID] { + if obj.Pkg() != ac.pkg.Package.Types { + continue + } + path, err := objectpath.For(obj) + if err != nil { + continue + } + for _, fact := range afacts { + facts = append(facts, Fact{string(path), fact}) + } + } + + buf := &bytes.Buffer{} + if err := gob.NewEncoder(buf).Encode(facts); err != nil { + return nil, err + } + aID, err := passActionID(ac.pkg, ac.analyzer) + if err != nil { + return nil, err + } + aID = cache.Subkey(aID, "facts") + if err := r.cache.PutBytes(aID, buf.Bytes()); err != nil { + return nil, err + } + } + + return ret, nil +} + +func NewRunner(stats *Stats) (*Runner, error) { + cache, err := cache.Default() + if err != nil { + return nil, err + } + + return &Runner{ + cache: cache, + stats: stats, + }, nil +} + +// Run loads packages corresponding to patterns and analyses them with +// analyzers. It returns the loaded packages, which contain reported +// diagnostics as well as extracted ignore directives. +// +// Note that diagnostics have not been filtered at this point yet, to +// accomodate cumulative analyzes that require additional steps to +// produce diagnostics. +func (r *Runner) Run(cfg *packages.Config, patterns []string, analyzers []*analysis.Analyzer, hasCumulative bool) ([]*Package, error) { + r.analyzerIDs = analyzerIDs{m: map[*analysis.Analyzer]int{}} + id := 0 + seen := map[*analysis.Analyzer]struct{}{} + var dfs func(a *analysis.Analyzer) + dfs = func(a *analysis.Analyzer) { + if _, ok := seen[a]; ok { + return + } + seen[a] = struct{}{} + r.analyzerIDs.m[a] = id + id++ + for _, f := range a.FactTypes { + gob.Register(f) + } + for _, req := range a.Requires { + dfs(req) + } + } + for _, a := range analyzers { + if v := a.Flags.Lookup("go"); v != nil { + v.Value.Set(fmt.Sprintf("1.%d", r.goVersion)) + } + dfs(a) + } + for _, a := range injectedAnalyses { + dfs(a) + } + + var dcfg packages.Config + if cfg != nil { + dcfg = *cfg + } + + atomic.StoreUint32(&r.stats.State, StateGraph) + initialPkgs, err := r.ld.Graph(dcfg, patterns...) + if err != nil { + return nil, err + } + + defer r.cache.Trim() + + var allPkgs []*Package + m := map[*packages.Package]*Package{} + packages.Visit(initialPkgs, nil, func(l *packages.Package) { + m[l] = &Package{ + Package: l, + results: make([]*result, len(r.analyzerIDs.m)), + facts: make([]map[types.Object][]analysis.Fact, len(r.analyzerIDs.m)), + pkgFacts: make([][]analysis.Fact, len(r.analyzerIDs.m)), + done: make(chan struct{}), + // every package needs itself + dependents: 1, + canClearTypes: !hasCumulative, + } + allPkgs = append(allPkgs, m[l]) + for i := range m[l].facts { + m[l].facts[i] = map[types.Object][]analysis.Fact{} + } + for _, err := range l.Errors { + m[l].errs = append(m[l].errs, err) + } + for _, v := range l.Imports { + m[v].dependents++ + m[l].Imports = append(m[l].Imports, m[v]) + } + + m[l].hash, err = packageHash(m[l]) + if err != nil { + m[l].errs = append(m[l].errs, err) + } + }) + + pkgs := make([]*Package, len(initialPkgs)) + for i, l := range initialPkgs { + pkgs[i] = m[l] + pkgs[i].initial = true + } + + atomic.StoreUint32(&r.stats.InitialPackages, uint32(len(initialPkgs))) + atomic.StoreUint32(&r.stats.TotalPackages, uint32(len(allPkgs))) + atomic.StoreUint32(&r.stats.State, StateProcessing) + + var wg sync.WaitGroup + wg.Add(len(allPkgs)) + r.loadSem = make(chan struct{}, runtime.GOMAXPROCS(-1)) + atomic.StoreUint32(&r.stats.TotalWorkers, uint32(cap(r.loadSem))) + for _, pkg := range allPkgs { + pkg := pkg + go func() { + r.processPkg(pkg, analyzers) + + if pkg.initial { + atomic.AddUint32(&r.stats.ProcessedInitialPackages, 1) + } + atomic.AddUint32(&r.stats.Problems, uint32(len(pkg.problems))) + wg.Done() + }() + } + wg.Wait() + + return pkgs, nil +} + +var posRe = regexp.MustCompile(`^(.+?):(\d+)(?::(\d+)?)?`) + +func parsePos(pos string) (token.Position, int, error) { + if pos == "-" || pos == "" { + return token.Position{}, 0, nil + } + parts := posRe.FindStringSubmatch(pos) + if parts == nil { + return token.Position{}, 0, fmt.Errorf("malformed position %q", pos) + } + file := parts[1] + line, _ := strconv.Atoi(parts[2]) + col, _ := strconv.Atoi(parts[3]) + return token.Position{ + Filename: file, + Line: line, + Column: col, + }, len(parts[0]), nil +} + +// loadPkg loads a Go package. If the package is in the set of initial +// packages, it will be loaded from source, otherwise it will be +// loaded from export data. In the case that the package was loaded +// from export data, cached facts will also be loaded. +// +// Currently, only cached facts for this package will be loaded, not +// for any of its dependencies. +func (r *Runner) loadPkg(pkg *Package, analyzers []*analysis.Analyzer) error { + if pkg.Types != nil { + panic(fmt.Sprintf("internal error: %s has already been loaded", pkg.Package)) + } + + // Load type information + if pkg.initial { + // Load package from source + pkg.fromSource = true + return r.ld.LoadFromSource(pkg.Package) + } + + // Load package from export data + if err := r.ld.LoadFromExport(pkg.Package); err != nil { + // We asked Go to give us up to date export data, yet + // we can't load it. There must be something wrong. + // + // Attempt loading from source. This should fail (because + // otherwise there would be export data); we just want to + // get the compile errors. If loading from source succeeds + // we discard the result, anyway. Otherwise we'll fail + // when trying to reload from export data later. + // + // FIXME(dh): we no longer reload from export data, so + // theoretically we should be able to continue + pkg.fromSource = true + if err := r.ld.LoadFromSource(pkg.Package); err != nil { + return err + } + // Make sure this package can't be imported successfully + pkg.Package.Errors = append(pkg.Package.Errors, packages.Error{ + Pos: "-", + Msg: fmt.Sprintf("could not load export data: %s", err), + Kind: packages.ParseError, + }) + return fmt.Errorf("could not load export data: %s", err) + } + + failed := false + seen := make([]bool, len(r.analyzerIDs.m)) + var dfs func(*analysis.Analyzer) + dfs = func(a *analysis.Analyzer) { + if seen[r.analyzerIDs.get(a)] { + return + } + seen[r.analyzerIDs.get(a)] = true + + if len(a.FactTypes) > 0 { + facts, ok := r.loadCachedFacts(a, pkg) + if !ok { + failed = true + return + } + + for _, f := range facts { + if f.Path == "" { + // This is a package fact + pkg.pkgFacts[r.analyzerIDs.get(a)] = append(pkg.pkgFacts[r.analyzerIDs.get(a)], f.Fact) + continue + } + obj, err := objectpath.Object(pkg.Types, objectpath.Path(f.Path)) + if err != nil { + // Be lenient about these errors. For example, when + // analysing io/ioutil from source, we may get a fact + // for methods on the devNull type, and objectpath + // will happily create a path for them. However, when + // we later load io/ioutil from export data, the path + // no longer resolves. + // + // If an exported type embeds the unexported type, + // then (part of) the unexported type will become part + // of the type information and our path will resolve + // again. + continue + } + pkg.facts[r.analyzerIDs.get(a)][obj] = append(pkg.facts[r.analyzerIDs.get(a)][obj], f.Fact) + } + } + + for _, req := range a.Requires { + dfs(req) + } + } + for _, a := range analyzers { + dfs(a) + } + + if failed { + pkg.fromSource = true + // XXX we added facts to the maps, we need to get rid of those + return r.ld.LoadFromSource(pkg.Package) + } + + return nil +} + +type analysisError struct { + analyzer *analysis.Analyzer + pkg *Package + err error +} + +func (err analysisError) Error() string { + return fmt.Sprintf("error running analyzer %s on %s: %s", err.analyzer, err.pkg, err.err) +} + +// processPkg processes a package. This involves loading the package, +// either from export data or from source. For packages loaded from +// source, the provides analyzers will be run on the package. +func (r *Runner) processPkg(pkg *Package, analyzers []*analysis.Analyzer) { + defer func() { + // Clear information we no longer need. Make sure to do this + // when returning from processPkg so that we clear + // dependencies, not just initial packages. + pkg.TypesInfo = nil + pkg.Syntax = nil + pkg.results = nil + + atomic.AddUint32(&r.stats.ProcessedPackages, 1) + pkg.decUse() + close(pkg.done) + }() + + // Ensure all packages have the generated map and config. This is + // required by interna of the runner. Analyses that themselves + // make use of either have an explicit dependency so that other + // runners work correctly, too. + analyzers = append(analyzers[0:len(analyzers):len(analyzers)], injectedAnalyses...) + + if len(pkg.errs) != 0 { + return + } + + for _, imp := range pkg.Imports { + <-imp.done + if len(imp.errs) > 0 { + if imp.initial { + // Don't print the error of the dependency since it's + // an initial package and we're already printing the + // error. + pkg.errs = append(pkg.errs, fmt.Errorf("could not analyze dependency %s of %s", imp, pkg)) + } else { + var s string + for _, err := range imp.errs { + s += "\n\t" + err.Error() + } + pkg.errs = append(pkg.errs, fmt.Errorf("could not analyze dependency %s of %s: %s", imp, pkg, s)) + } + return + } + } + if pkg.PkgPath == "unsafe" { + pkg.Types = types.Unsafe + return + } + + r.loadSem <- struct{}{} + atomic.AddUint32(&r.stats.ActiveWorkers, 1) + defer func() { + <-r.loadSem + atomic.AddUint32(&r.stats.ActiveWorkers, ^uint32(0)) + }() + if err := r.loadPkg(pkg, analyzers); err != nil { + pkg.errs = append(pkg.errs, err) + return + } + + // A package's object facts is the union of all of its dependencies. + for _, imp := range pkg.Imports { + for ai, m := range imp.facts { + for obj, facts := range m { + pkg.facts[ai][obj] = facts[0:len(facts):len(facts)] + } + } + } + + if !pkg.fromSource { + // Nothing left to do for the package. + return + } + + // Run analyses on initial packages and those missing facts + var wg sync.WaitGroup + wg.Add(len(analyzers)) + errs := make([]error, len(analyzers)) + var acs []*analysisAction + for i, a := range analyzers { + i := i + a := a + ac := r.makeAnalysisAction(a, pkg) + acs = append(acs, ac) + go func() { + defer wg.Done() + // Only initial packages and packages with missing + // facts will have been loaded from source. + if pkg.initial || r.hasFacts(a) { + if _, err := r.runAnalysis(ac); err != nil { + errs[i] = analysisError{a, pkg, err} + return + } + } + }() + } + wg.Wait() + + depErrors := map[dependencyError]int{} + for _, err := range errs { + if err == nil { + continue + } + switch err := err.(type) { + case analysisError: + switch err := err.err.(type) { + case dependencyError: + depErrors[err.nested()]++ + default: + pkg.errs = append(pkg.errs, err) + } + default: + pkg.errs = append(pkg.errs, err) + } + } + for err, count := range depErrors { + pkg.errs = append(pkg.errs, + fmt.Errorf("could not run %s@%s, preventing %d analyzers from running: %s", err.dep, pkg, count, err.err)) + } + + // We can't process ignores at this point because `unused` needs + // to see more than one package to make its decision. + ignores, problems := parseDirectives(pkg.Package) + pkg.ignores = append(pkg.ignores, ignores...) + pkg.problems = append(pkg.problems, problems...) + for _, ac := range acs { + pkg.problems = append(pkg.problems, ac.problems...) + } + + if pkg.initial { + // Only initial packages have these analyzers run, and only + // initial packages need these. + if pkg.results[r.analyzerIDs.get(config.Analyzer)].v != nil { + pkg.cfg = pkg.results[r.analyzerIDs.get(config.Analyzer)].v.(*config.Config) + } + pkg.gen = pkg.results[r.analyzerIDs.get(facts.Generated)].v.(map[string]facts.Generator) + } + + // In a previous version of the code, we would throw away all type + // information and reload it from export data. That was + // nonsensical. The *types.Package doesn't keep any information + // live that export data wouldn't also. We only need to discard + // the AST and the TypesInfo maps; that happens after we return + // from processPkg. +} + +// hasFacts reports whether an analysis exports any facts. An analysis +// that has a transitive dependency that exports facts is considered +// to be exporting facts. +func (r *Runner) hasFacts(a *analysis.Analyzer) bool { + ret := false + seen := make([]bool, len(r.analyzerIDs.m)) + var dfs func(*analysis.Analyzer) + dfs = func(a *analysis.Analyzer) { + if seen[r.analyzerIDs.get(a)] { + return + } + seen[r.analyzerIDs.get(a)] = true + if len(a.FactTypes) > 0 { + ret = true + } + for _, req := range a.Requires { + if ret { + break + } + dfs(req) + } + } + dfs(a) + return ret +} + +func parseDirective(s string) (cmd string, args []string) { + if !strings.HasPrefix(s, "//lint:") { + return "", nil + } + s = strings.TrimPrefix(s, "//lint:") + fields := strings.Split(s, " ") + return fields[0], fields[1:] +} + +// parseDirectives extracts all linter directives from the source +// files of the package. Malformed directives are returned as problems. +func parseDirectives(pkg *packages.Package) ([]Ignore, []Problem) { + var ignores []Ignore + var problems []Problem + + for _, f := range pkg.Syntax { + found := false + commentLoop: + for _, cg := range f.Comments { + for _, c := range cg.List { + if strings.Contains(c.Text, "//lint:") { + found = true + break commentLoop + } + } + } + if !found { + continue + } + cm := ast.NewCommentMap(pkg.Fset, f, f.Comments) + for node, cgs := range cm { + for _, cg := range cgs { + for _, c := range cg.List { + if !strings.HasPrefix(c.Text, "//lint:") { + continue + } + cmd, args := parseDirective(c.Text) + switch cmd { + case "ignore", "file-ignore": + if len(args) < 2 { + p := Problem{ + Pos: DisplayPosition(pkg.Fset, c.Pos()), + Message: "malformed linter directive; missing the required reason field?", + Severity: Error, + Check: "compile", + } + problems = append(problems, p) + continue + } + default: + // unknown directive, ignore + continue + } + checks := strings.Split(args[0], ",") + pos := DisplayPosition(pkg.Fset, node.Pos()) + var ig Ignore + switch cmd { + case "ignore": + ig = &LineIgnore{ + File: pos.Filename, + Line: pos.Line, + Checks: checks, + Pos: c.Pos(), + } + case "file-ignore": + ig = &FileIgnore{ + File: pos.Filename, + Checks: checks, + } + } + ignores = append(ignores, ig) + } + } + } + } + + return ignores, problems +} + +// packageHash computes a package's hash. The hash is based on all Go +// files that make up the package, as well as the hashes of imported +// packages. +func packageHash(pkg *Package) (string, error) { + key := cache.NewHash("package hash") + fmt.Fprintf(key, "pkgpath %s\n", pkg.PkgPath) + for _, f := range pkg.CompiledGoFiles { + h, err := cache.FileHash(f) + if err != nil { + return "", err + } + fmt.Fprintf(key, "file %s %x\n", f, h) + } + + imps := make([]*Package, len(pkg.Imports)) + copy(imps, pkg.Imports) + sort.Slice(imps, func(i, j int) bool { + return imps[i].PkgPath < imps[j].PkgPath + }) + for _, dep := range imps { + if dep.PkgPath == "unsafe" { + continue + } + + fmt.Fprintf(key, "import %s %s\n", dep.PkgPath, dep.hash) + } + h := key.Sum() + return hex.EncodeToString(h[:]), nil +} + +// passActionID computes an ActionID for an analysis pass. +func passActionID(pkg *Package, analyzer *analysis.Analyzer) (cache.ActionID, error) { + key := cache.NewHash("action ID") + fmt.Fprintf(key, "pkgpath %s\n", pkg.PkgPath) + fmt.Fprintf(key, "pkghash %s\n", pkg.hash) + fmt.Fprintf(key, "analyzer %s\n", analyzer.Name) + + return key.Sum(), nil +} diff --git a/vendor/honnef.co/go/tools/lint/stats.go b/vendor/honnef.co/go/tools/lint/stats.go new file mode 100644 index 000000000000..2f6508559370 --- /dev/null +++ b/vendor/honnef.co/go/tools/lint/stats.go @@ -0,0 +1,20 @@ +package lint + +const ( + StateInitializing = 0 + StateGraph = 1 + StateProcessing = 2 + StateCumulative = 3 +) + +type Stats struct { + State uint32 + + InitialPackages uint32 + TotalPackages uint32 + ProcessedPackages uint32 + ProcessedInitialPackages uint32 + Problems uint32 + ActiveWorkers uint32 + TotalWorkers uint32 +} diff --git a/vendor/honnef.co/go/tools/loader/loader.go b/vendor/honnef.co/go/tools/loader/loader.go new file mode 100644 index 000000000000..9c6885d485f0 --- /dev/null +++ b/vendor/honnef.co/go/tools/loader/loader.go @@ -0,0 +1,197 @@ +package loader + +import ( + "fmt" + "go/ast" + "go/parser" + "go/scanner" + "go/token" + "go/types" + "log" + "os" + "sync" + + "golang.org/x/tools/go/gcexportdata" + "golang.org/x/tools/go/packages" +) + +type Loader struct { + exportMu sync.RWMutex +} + +// Graph resolves patterns and returns packages with all the +// information required to later load type information, and optionally +// syntax trees. +// +// The provided config can set any setting with the exception of Mode. +func (ld *Loader) Graph(cfg packages.Config, patterns ...string) ([]*packages.Package, error) { + cfg.Mode = packages.NeedName | packages.NeedImports | packages.NeedDeps | packages.NeedExportsFile | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedTypesSizes + pkgs, err := packages.Load(&cfg, patterns...) + if err != nil { + return nil, err + } + fset := token.NewFileSet() + packages.Visit(pkgs, nil, func(pkg *packages.Package) { + pkg.Fset = fset + }) + return pkgs, nil +} + +// LoadFromExport loads a package from export data. All of its +// dependencies must have been loaded already. +func (ld *Loader) LoadFromExport(pkg *packages.Package) error { + ld.exportMu.Lock() + defer ld.exportMu.Unlock() + + pkg.IllTyped = true + for path, pkg := range pkg.Imports { + if pkg.Types == nil { + return fmt.Errorf("dependency %q hasn't been loaded yet", path) + } + } + if pkg.ExportFile == "" { + return fmt.Errorf("no export data for %q", pkg.ID) + } + f, err := os.Open(pkg.ExportFile) + if err != nil { + return err + } + defer f.Close() + + r, err := gcexportdata.NewReader(f) + if err != nil { + return err + } + + view := make(map[string]*types.Package) // view seen by gcexportdata + seen := make(map[*packages.Package]bool) // all visited packages + var visit func(pkgs map[string]*packages.Package) + visit = func(pkgs map[string]*packages.Package) { + for _, pkg := range pkgs { + if !seen[pkg] { + seen[pkg] = true + view[pkg.PkgPath] = pkg.Types + visit(pkg.Imports) + } + } + } + visit(pkg.Imports) + tpkg, err := gcexportdata.Read(r, pkg.Fset, view, pkg.PkgPath) + if err != nil { + return err + } + pkg.Types = tpkg + pkg.IllTyped = false + return nil +} + +// LoadFromSource loads a package from source. All of its dependencies +// must have been loaded already. +func (ld *Loader) LoadFromSource(pkg *packages.Package) error { + ld.exportMu.RLock() + defer ld.exportMu.RUnlock() + + pkg.IllTyped = true + pkg.Types = types.NewPackage(pkg.PkgPath, pkg.Name) + + // OPT(dh): many packages have few files, much fewer than there + // are CPU cores. Additionally, parsing each individual file is + // very fast. A naive parallel implementation of this loop won't + // be faster, and tends to be slower due to extra scheduling, + // bookkeeping and potentially false sharing of cache lines. + pkg.Syntax = make([]*ast.File, len(pkg.CompiledGoFiles)) + for i, file := range pkg.CompiledGoFiles { + f, err := parser.ParseFile(pkg.Fset, file, nil, parser.ParseComments) + if err != nil { + pkg.Errors = append(pkg.Errors, convertError(err)...) + return err + } + pkg.Syntax[i] = f + } + pkg.TypesInfo = &types.Info{ + Types: make(map[ast.Expr]types.TypeAndValue), + Defs: make(map[*ast.Ident]types.Object), + Uses: make(map[*ast.Ident]types.Object), + Implicits: make(map[ast.Node]types.Object), + Scopes: make(map[ast.Node]*types.Scope), + Selections: make(map[*ast.SelectorExpr]*types.Selection), + } + + importer := func(path string) (*types.Package, error) { + if path == "unsafe" { + return types.Unsafe, nil + } + imp := pkg.Imports[path] + if imp == nil { + return nil, nil + } + if len(imp.Errors) > 0 { + return nil, imp.Errors[0] + } + return imp.Types, nil + } + tc := &types.Config{ + Importer: importerFunc(importer), + Error: func(err error) { + pkg.Errors = append(pkg.Errors, convertError(err)...) + }, + } + err := types.NewChecker(tc, pkg.Fset, pkg.Types, pkg.TypesInfo).Files(pkg.Syntax) + if err != nil { + return err + } + pkg.IllTyped = false + return nil +} + +func convertError(err error) []packages.Error { + var errs []packages.Error + // taken from go/packages + switch err := err.(type) { + case packages.Error: + // from driver + errs = append(errs, err) + + case *os.PathError: + // from parser + errs = append(errs, packages.Error{ + Pos: err.Path + ":1", + Msg: err.Err.Error(), + Kind: packages.ParseError, + }) + + case scanner.ErrorList: + // from parser + for _, err := range err { + errs = append(errs, packages.Error{ + Pos: err.Pos.String(), + Msg: err.Msg, + Kind: packages.ParseError, + }) + } + + case types.Error: + // from type checker + errs = append(errs, packages.Error{ + Pos: err.Fset.Position(err.Pos).String(), + Msg: err.Msg, + Kind: packages.TypeError, + }) + + default: + // unexpected impoverished error from parser? + errs = append(errs, packages.Error{ + Pos: "-", + Msg: err.Error(), + Kind: packages.UnknownError, + }) + + // If you see this error message, please file a bug. + log.Printf("internal error: error %q (%T) without position", err, err) + } + return errs +} + +type importerFunc func(path string) (*types.Package, error) + +func (f importerFunc) Import(path string) (*types.Package, error) { return f(path) } diff --git a/vendor/honnef.co/go/tools/printf/fuzz.go b/vendor/honnef.co/go/tools/printf/fuzz.go new file mode 100644 index 000000000000..8ebf357fb42c --- /dev/null +++ b/vendor/honnef.co/go/tools/printf/fuzz.go @@ -0,0 +1,11 @@ +// +build gofuzz + +package printf + +func Fuzz(data []byte) int { + _, err := Parse(string(data)) + if err == nil { + return 1 + } + return 0 +} diff --git a/vendor/honnef.co/go/tools/printf/printf.go b/vendor/honnef.co/go/tools/printf/printf.go new file mode 100644 index 000000000000..754db9b16d8f --- /dev/null +++ b/vendor/honnef.co/go/tools/printf/printf.go @@ -0,0 +1,197 @@ +// Package printf implements a parser for fmt.Printf-style format +// strings. +// +// It parses verbs according to the following syntax: +// Numeric -> '0'-'9' +// Letter -> 'a'-'z' | 'A'-'Z' +// Index -> '[' Numeric+ ']' +// Star -> '*' +// Star -> Index '*' +// +// Precision -> Numeric+ | Star +// Width -> Numeric+ | Star +// +// WidthAndPrecision -> Width '.' Precision +// WidthAndPrecision -> Width '.' +// WidthAndPrecision -> Width +// WidthAndPrecision -> '.' Precision +// WidthAndPrecision -> '.' +// +// Flag -> '+' | '-' | '#' | ' ' | '0' +// Verb -> Letter | '%' +// +// Input -> '%' [ Flag+ ] [ WidthAndPrecision ] [ Index ] Verb +package printf + +import ( + "errors" + "regexp" + "strconv" + "strings" +) + +// ErrInvalid is returned for invalid format strings or verbs. +var ErrInvalid = errors.New("invalid format string") + +type Verb struct { + Letter rune + Flags string + + Width Argument + Precision Argument + // Which value in the argument list the verb uses. + // -1 denotes the next argument, + // values > 0 denote explicit arguments. + // The value 0 denotes that no argument is consumed. This is the case for %%. + Value int + + Raw string +} + +// Argument is an implicit or explicit width or precision. +type Argument interface { + isArgument() +} + +// The Default value, when no width or precision is provided. +type Default struct{} + +// Zero is the implicit zero value. +// This value may only appear for precisions in format strings like %6.f +type Zero struct{} + +// Star is a * value, which may either refer to the next argument (Index == -1) or an explicit argument. +type Star struct{ Index int } + +// A Literal value, such as 6 in %6d. +type Literal int + +func (Default) isArgument() {} +func (Zero) isArgument() {} +func (Star) isArgument() {} +func (Literal) isArgument() {} + +// Parse parses f and returns a list of actions. +// An action may either be a literal string, or a Verb. +func Parse(f string) ([]interface{}, error) { + var out []interface{} + for len(f) > 0 { + if f[0] == '%' { + v, n, err := ParseVerb(f) + if err != nil { + return nil, err + } + f = f[n:] + out = append(out, v) + } else { + n := strings.IndexByte(f, '%') + if n > -1 { + out = append(out, f[:n]) + f = f[n:] + } else { + out = append(out, f) + f = "" + } + } + } + + return out, nil +} + +func atoi(s string) int { + n, _ := strconv.Atoi(s) + return n +} + +// ParseVerb parses the verb at the beginning of f. +// It returns the verb, how much of the input was consumed, and an error, if any. +func ParseVerb(f string) (Verb, int, error) { + if len(f) < 2 { + return Verb{}, 0, ErrInvalid + } + const ( + flags = 1 + + width = 2 + widthStar = 3 + widthIndex = 5 + + dot = 6 + prec = 7 + precStar = 8 + precIndex = 10 + + verbIndex = 11 + verb = 12 + ) + + m := re.FindStringSubmatch(f) + if m == nil { + return Verb{}, 0, ErrInvalid + } + + v := Verb{ + Letter: []rune(m[verb])[0], + Flags: m[flags], + Raw: m[0], + } + + if m[width] != "" { + // Literal width + v.Width = Literal(atoi(m[width])) + } else if m[widthStar] != "" { + // Star width + if m[widthIndex] != "" { + v.Width = Star{atoi(m[widthIndex])} + } else { + v.Width = Star{-1} + } + } else { + // Default width + v.Width = Default{} + } + + if m[dot] == "" { + // default precision + v.Precision = Default{} + } else { + if m[prec] != "" { + // Literal precision + v.Precision = Literal(atoi(m[prec])) + } else if m[precStar] != "" { + // Star precision + if m[precIndex] != "" { + v.Precision = Star{atoi(m[precIndex])} + } else { + v.Precision = Star{-1} + } + } else { + // Zero precision + v.Precision = Zero{} + } + } + + if m[verb] == "%" { + v.Value = 0 + } else if m[verbIndex] != "" { + v.Value = atoi(m[verbIndex]) + } else { + v.Value = -1 + } + + return v, len(m[0]), nil +} + +const ( + flags = `([+#0 -]*)` + verb = `([a-zA-Z%])` + index = `(?:\[([0-9]+)\])` + star = `((` + index + `)?\*)` + width1 = `([0-9]+)` + width2 = star + width = `(?:` + width1 + `|` + width2 + `)` + precision = width + widthAndPrecision = `(?:(?:` + width + `)?(?:(\.)(?:` + precision + `)?)?)` +) + +var re = regexp.MustCompile(`^%` + flags + widthAndPrecision + `?` + index + `?` + verb) diff --git a/vendor/github.com/golangci/go-tools/simple/CONTRIBUTING.md b/vendor/honnef.co/go/tools/simple/CONTRIBUTING.md similarity index 89% rename from vendor/github.com/golangci/go-tools/simple/CONTRIBUTING.md rename to vendor/honnef.co/go/tools/simple/CONTRIBUTING.md index 105023cf513a..c54c6c50ac95 100644 --- a/vendor/github.com/golangci/go-tools/simple/CONTRIBUTING.md +++ b/vendor/honnef.co/go/tools/simple/CONTRIBUTING.md @@ -6,7 +6,7 @@ Check you have the latest version of its dependencies. Run ``` -go get -u github.com/golangci/go-tools/simple +go get -u honnef.co/go/tools/simple ``` If you still have problems, consider searching for existing issues before filing a new issue. diff --git a/vendor/honnef.co/go/tools/simple/analysis.go b/vendor/honnef.co/go/tools/simple/analysis.go new file mode 100644 index 000000000000..abb1648fab0b --- /dev/null +++ b/vendor/honnef.co/go/tools/simple/analysis.go @@ -0,0 +1,223 @@ +package simple + +import ( + "flag" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/inspect" + "honnef.co/go/tools/facts" + "honnef.co/go/tools/internal/passes/buildssa" + "honnef.co/go/tools/lint/lintutil" +) + +func newFlagSet() flag.FlagSet { + fs := flag.NewFlagSet("", flag.PanicOnError) + fs.Var(lintutil.NewVersionFlag(), "go", "Target Go version") + return *fs +} + +var Analyzers = map[string]*analysis.Analyzer{ + "S1000": { + Name: "S1000", + Run: LintSingleCaseSelect, + Doc: Docs["S1000"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1001": { + Name: "S1001", + Run: LintLoopCopy, + Doc: Docs["S1001"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1002": { + Name: "S1002", + Run: LintIfBoolCmp, + Doc: Docs["S1002"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1003": { + Name: "S1003", + Run: LintStringsContains, + Doc: Docs["S1003"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1004": { + Name: "S1004", + Run: LintBytesCompare, + Doc: Docs["S1004"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1005": { + Name: "S1005", + Run: LintUnnecessaryBlank, + Doc: Docs["S1005"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1006": { + Name: "S1006", + Run: LintForTrue, + Doc: Docs["S1006"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1007": { + Name: "S1007", + Run: LintRegexpRaw, + Doc: Docs["S1007"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1008": { + Name: "S1008", + Run: LintIfReturn, + Doc: Docs["S1008"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1009": { + Name: "S1009", + Run: LintRedundantNilCheckWithLen, + Doc: Docs["S1009"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1010": { + Name: "S1010", + Run: LintSlicing, + Doc: Docs["S1010"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1011": { + Name: "S1011", + Run: LintLoopAppend, + Doc: Docs["S1011"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1012": { + Name: "S1012", + Run: LintTimeSince, + Doc: Docs["S1012"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1016": { + Name: "S1016", + Run: LintSimplerStructConversion, + Doc: Docs["S1016"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1017": { + Name: "S1017", + Run: LintTrim, + Doc: Docs["S1017"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1018": { + Name: "S1018", + Run: LintLoopSlide, + Doc: Docs["S1018"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1019": { + Name: "S1019", + Run: LintMakeLenCap, + Doc: Docs["S1019"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1020": { + Name: "S1020", + Run: LintAssertNotNil, + Doc: Docs["S1020"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1021": { + Name: "S1021", + Run: LintDeclareAssign, + Doc: Docs["S1021"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1023": { + Name: "S1023", + Run: LintRedundantBreak, + Doc: Docs["S1023"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1024": { + Name: "S1024", + Run: LintTimeUntil, + Doc: Docs["S1024"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1025": { + Name: "S1025", + Run: LintRedundantSprintf, + Doc: Docs["S1025"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1028": { + Name: "S1028", + Run: LintErrorsNewSprintf, + Doc: Docs["S1028"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1029": { + Name: "S1029", + Run: LintRangeStringRunes, + Doc: Docs["S1029"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + Flags: newFlagSet(), + }, + "S1030": { + Name: "S1030", + Run: LintBytesBufferConversions, + Doc: Docs["S1030"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1031": { + Name: "S1031", + Run: LintNilCheckAroundRange, + Doc: Docs["S1031"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1032": { + Name: "S1032", + Run: LintSortHelpers, + Doc: Docs["S1032"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1033": { + Name: "S1033", + Run: LintGuardedDelete, + Doc: Docs["S1033"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "S1034": { + Name: "S1034", + Run: LintSimplifyTypeSwitch, + Doc: Docs["S1034"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, +} diff --git a/vendor/honnef.co/go/tools/simple/doc.go b/vendor/honnef.co/go/tools/simple/doc.go new file mode 100644 index 000000000000..eb0072de5dc5 --- /dev/null +++ b/vendor/honnef.co/go/tools/simple/doc.go @@ -0,0 +1,425 @@ +package simple + +import "honnef.co/go/tools/lint" + +var Docs = map[string]*lint.Documentation{ + "S1000": &lint.Documentation{ + Title: `Use plain channel send or receive instead of single-case select`, + Text: `Select statements with a single case can be replaced with a simple +send or receive. + +Before: + + select { + case x := <-ch: + fmt.Println(x) + } + +After: + + x := <-ch + fmt.Println(x)`, + Since: "2017.1", + }, + + "S1001": &lint.Documentation{ + Title: `Replace for loop with call to copy`, + Text: `Use copy() for copying elements from one slice to another. + +Before: + + for i, x := range src { + dst[i] = x + } + +After: + + copy(dst, src)`, + Since: "2017.1", + }, + + "S1002": &lint.Documentation{ + Title: `Omit comparison with boolean constant`, + Text: `Before: + + if x == true {} + +After: + + if x {}`, + Since: "2017.1", + }, + + "S1003": &lint.Documentation{ + Title: `Replace call to strings.Index with strings.Contains`, + Text: `Before: + + if strings.Index(x, y) != -1 {} + +After: + + if strings.Contains(x, y) {}`, + Since: "2017.1", + }, + + "S1004": &lint.Documentation{ + Title: `Replace call to bytes.Compare with bytes.Equal`, + Text: `Before: + + if bytes.Compare(x, y) == 0 {} + +After: + + if bytes.Equal(x, y) {}`, + Since: "2017.1", + }, + + "S1005": &lint.Documentation{ + Title: `Drop unnecessary use of the blank identifier`, + Text: `In many cases, assigning to the blank identifier is unnecessary. + +Before: + + for _ = range s {} + x, _ = someMap[key] + _ = <-ch + +After: + + for range s{} + x = someMap[key] + <-ch`, + Since: "2017.1", + }, + + "S1006": &lint.Documentation{ + Title: `Use for { ... } for infinite loops`, + Text: `For infinite loops, using for { ... } is the most idiomatic choice.`, + Since: "2017.1", + }, + + "S1007": &lint.Documentation{ + Title: `Simplify regular expression by using raw string literal`, + Text: `Raw string literals use ` + "`" + ` instead of " and do not support +any escape sequences. This means that the backslash (\) can be used +freely, without the need of escaping. + +Since regular expressions have their own escape sequences, raw strings +can improve their readability. + +Before: + + regexp.Compile("\\A(\\w+) profile: total \\d+\\n\\z") + +After: + + regexp.Compile(` + "`" + `\A(\w+) profile: total \d+\n\z` + "`" + `)`, + Since: "2017.1", + }, + + "S1008": &lint.Documentation{ + Title: `Simplify returning boolean expression`, + Text: `Before: + + if { + return true + } + return false + +After: + + return `, + Since: "2017.1", + }, + + "S1009": &lint.Documentation{ + Title: `Omit redundant nil check on slices`, + Text: `The len function is defined for all slices, even nil ones, which have +a length of zero. It is not necessary to check if a slice is not nil +before checking that its length is not zero. + +Before: + + if x != nil && len(x) != 0 {} + +After: + + if len(x) != 0 {}`, + Since: "2017.1", + }, + + "S1010": &lint.Documentation{ + Title: `Omit default slice index`, + Text: `When slicing, the second index defaults to the length of the value, +making s[n:len(s)] and s[n:] equivalent.`, + Since: "2017.1", + }, + + "S1011": &lint.Documentation{ + Title: `Use a single append to concatenate two slices`, + Text: `Before: + + for _, e := range y { + x = append(x, e) + } + +After: + + x = append(x, y...)`, + Since: "2017.1", + }, + + "S1012": &lint.Documentation{ + Title: `Replace time.Now().Sub(x) with time.Since(x)`, + Text: `The time.Since helper has the same effect as using time.Now().Sub(x) +but is easier to read. + +Before: + + time.Now().Sub(x) + +After: + + time.Since(x)`, + Since: "2017.1", + }, + + "S1016": &lint.Documentation{ + Title: `Use a type conversion instead of manually copying struct fields`, + Text: `Two struct types with identical fields can be converted between each +other. In older versions of Go, the fields had to have identical +struct tags. Since Go 1.8, however, struct tags are ignored during +conversions. It is thus not necessary to manually copy every field +individually. + +Before: + + var x T1 + y := T2{ + Field1: x.Field1, + Field2: x.Field2, + } + +After: + + var x T1 + y := T2(x)`, + Since: "2017.1", + }, + + "S1017": &lint.Documentation{ + Title: `Replace manual trimming with strings.TrimPrefix`, + Text: `Instead of using strings.HasPrefix and manual slicing, use the +strings.TrimPrefix function. If the string doesn't start with the +prefix, the original string will be returned. Using strings.TrimPrefix +reduces complexity, and avoids common bugs, such as off-by-one +mistakes. + +Before: + + if strings.HasPrefix(str, prefix) { + str = str[len(prefix):] + } + +After: + + str = strings.TrimPrefix(str, prefix)`, + Since: "2017.1", + }, + + "S1018": &lint.Documentation{ + Title: `Use copy for sliding elements`, + Text: `copy() permits using the same source and destination slice, even with +overlapping ranges. This makes it ideal for sliding elements in a +slice. + +Before: + + for i := 0; i < n; i++ { + bs[i] = bs[offset+i] + } + +After: + + copy(bs[:n], bs[offset:])`, + Since: "2017.1", + }, + + "S1019": &lint.Documentation{ + Title: `Simplify make call by omitting redundant arguments`, + Text: `The make function has default values for the length and capacity +arguments. For channels and maps, the length defaults to zero. +Additionally, for slices the capacity defaults to the length.`, + Since: "2017.1", + }, + + "S1020": &lint.Documentation{ + Title: `Omit redundant nil check in type assertion`, + Text: `Before: + + if _, ok := i.(T); ok && i != nil {} + +After: + + if _, ok := i.(T); ok {}`, + Since: "2017.1", + }, + + "S1021": &lint.Documentation{ + Title: `Merge variable declaration and assignment`, + Text: `Before: + + var x uint + x = 1 + +After: + + var x uint = 1`, + Since: "2017.1", + }, + + "S1023": &lint.Documentation{ + Title: `Omit redundant control flow`, + Text: `Functions that have no return value do not need a return statement as +the final statement of the function. + +Switches in Go do not have automatic fallthrough, unlike languages +like C. It is not necessary to have a break statement as the final +statement in a case block.`, + Since: "2017.1", + }, + + "S1024": &lint.Documentation{ + Title: `Replace x.Sub(time.Now()) with time.Until(x)`, + Text: `The time.Until helper has the same effect as using x.Sub(time.Now()) +but is easier to read. + +Before: + + x.Sub(time.Now()) + +After: + + time.Until(x)`, + Since: "2017.1", + }, + + "S1025": &lint.Documentation{ + Title: `Don't use fmt.Sprintf("%s", x) unnecessarily`, + Text: `In many instances, there are easier and more efficient ways of getting +a value's string representation. Whenever a value's underlying type is +a string already, or the type has a String method, they should be used +directly. + +Given the following shared definitions + + type T1 string + type T2 int + + func (T2) String() string { return "Hello, world" } + + var x string + var y T1 + var z T2 + +we can simplify the following + + fmt.Sprintf("%s", x) + fmt.Sprintf("%s", y) + fmt.Sprintf("%s", z) + +to + + x + string(y) + z.String()`, + Since: "2017.1", + }, + + "S1028": &lint.Documentation{ + Title: `Simplify error construction with fmt.Errorf`, + Text: `Before: + + errors.New(fmt.Sprintf(...)) + +After: + + fmt.Errorf(...)`, + Since: "2017.1", + }, + + "S1029": &lint.Documentation{ + Title: `Range over the string directly`, + Text: `Ranging over a string will yield byte offsets and runes. If the offset +isn't used, this is functionally equivalent to converting the string +to a slice of runes and ranging over that. Ranging directly over the +string will be more performant, however, as it avoids allocating a new +slice, the size of which depends on the length of the string. + +Before: + + for _, r := range []rune(s) {} + +After: + + for _, r := range s {}`, + Since: "2017.1", + }, + + "S1030": &lint.Documentation{ + Title: `Use bytes.Buffer.String or bytes.Buffer.Bytes`, + Text: `bytes.Buffer has both a String and a Bytes method. It is never +necessary to use string(buf.Bytes()) or []byte(buf.String()) – simply +use the other method.`, + Since: "2017.1", + }, + + "S1031": &lint.Documentation{ + Title: `Omit redundant nil check around loop`, + Text: `You can use range on nil slices and maps, the loop will simply never +execute. This makes an additional nil check around the loop +unnecessary. + +Before: + + if s != nil { + for _, x := range s { + ... + } + } + +After: + + for _, x := range s { + ... + }`, + Since: "2017.1", + }, + + "S1032": &lint.Documentation{ + Title: `Use sort.Ints(x), sort.Float64s(x), and sort.Strings(x)`, + Text: `The sort.Ints, sort.Float64s and sort.Strings functions are easier to +read than sort.Sort(sort.IntSlice(x)), sort.Sort(sort.Float64Slice(x)) +and sort.Sort(sort.StringSlice(x)). + +Before: + + sort.Sort(sort.StringSlice(x)) + +After: + + sort.Strings(x)`, + Since: "2019.1", + }, + + "S1033": &lint.Documentation{ + Title: `Unnecessary guard around call to delete`, + Text: `Calling delete on a nil map is a no-op.`, + Since: "2019.2", + }, + + "S1034": &lint.Documentation{ + Title: `Use result of type assertion to simplify cases`, + Since: "2019.2", + }, +} diff --git a/vendor/honnef.co/go/tools/simple/lint.go b/vendor/honnef.co/go/tools/simple/lint.go new file mode 100644 index 000000000000..c78a7bb7a425 --- /dev/null +++ b/vendor/honnef.co/go/tools/simple/lint.go @@ -0,0 +1,1816 @@ +// Package simple contains a linter for Go source code. +package simple // import "honnef.co/go/tools/simple" + +import ( + "fmt" + "go/ast" + "go/constant" + "go/token" + "go/types" + "reflect" + "sort" + "strings" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/inspect" + "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/go/types/typeutil" + . "honnef.co/go/tools/arg" + "honnef.co/go/tools/internal/passes/buildssa" + "honnef.co/go/tools/internal/sharedcheck" + "honnef.co/go/tools/lint" + . "honnef.co/go/tools/lint/lintdsl" +) + +func LintSingleCaseSelect(pass *analysis.Pass) (interface{}, error) { + isSingleSelect := func(node ast.Node) bool { + v, ok := node.(*ast.SelectStmt) + if !ok { + return false + } + return len(v.Body.List) == 1 + } + + seen := map[ast.Node]struct{}{} + fn := func(node ast.Node) { + switch v := node.(type) { + case *ast.ForStmt: + if len(v.Body.List) != 1 { + return + } + if !isSingleSelect(v.Body.List[0]) { + return + } + if _, ok := v.Body.List[0].(*ast.SelectStmt).Body.List[0].(*ast.CommClause).Comm.(*ast.SendStmt); ok { + // Don't suggest using range for channel sends + return + } + seen[v.Body.List[0]] = struct{}{} + ReportNodefFG(pass, node, "should use for range instead of for { select {} }") + case *ast.SelectStmt: + if _, ok := seen[v]; ok { + return + } + if !isSingleSelect(v) { + return + } + ReportNodefFG(pass, node, "should use a simple channel send/receive instead of select with a single case") + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.ForStmt)(nil), (*ast.SelectStmt)(nil)}, fn) + return nil, nil +} + +func LintLoopCopy(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + loop := node.(*ast.RangeStmt) + + if loop.Key == nil { + return + } + if len(loop.Body.List) != 1 { + return + } + stmt, ok := loop.Body.List[0].(*ast.AssignStmt) + if !ok { + return + } + if stmt.Tok != token.ASSIGN || len(stmt.Lhs) != 1 || len(stmt.Rhs) != 1 { + return + } + lhs, ok := stmt.Lhs[0].(*ast.IndexExpr) + if !ok { + return + } + + if _, ok := pass.TypesInfo.TypeOf(lhs.X).(*types.Slice); !ok { + return + } + lidx, ok := lhs.Index.(*ast.Ident) + if !ok { + return + } + key, ok := loop.Key.(*ast.Ident) + if !ok { + return + } + if pass.TypesInfo.TypeOf(lhs) == nil || pass.TypesInfo.TypeOf(stmt.Rhs[0]) == nil { + return + } + if pass.TypesInfo.ObjectOf(lidx) != pass.TypesInfo.ObjectOf(key) { + return + } + if !types.Identical(pass.TypesInfo.TypeOf(lhs), pass.TypesInfo.TypeOf(stmt.Rhs[0])) { + return + } + if _, ok := pass.TypesInfo.TypeOf(loop.X).(*types.Slice); !ok { + return + } + + if rhs, ok := stmt.Rhs[0].(*ast.IndexExpr); ok { + rx, ok := rhs.X.(*ast.Ident) + _ = rx + if !ok { + return + } + ridx, ok := rhs.Index.(*ast.Ident) + if !ok { + return + } + if pass.TypesInfo.ObjectOf(ridx) != pass.TypesInfo.ObjectOf(key) { + return + } + } else if rhs, ok := stmt.Rhs[0].(*ast.Ident); ok { + value, ok := loop.Value.(*ast.Ident) + if !ok { + return + } + if pass.TypesInfo.ObjectOf(rhs) != pass.TypesInfo.ObjectOf(value) { + return + } + } else { + return + } + ReportNodefFG(pass, loop, "should use copy() instead of a loop") + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.RangeStmt)(nil)}, fn) + return nil, nil +} + +func LintIfBoolCmp(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + expr := node.(*ast.BinaryExpr) + if expr.Op != token.EQL && expr.Op != token.NEQ { + return + } + x := IsBoolConst(pass, expr.X) + y := IsBoolConst(pass, expr.Y) + if !x && !y { + return + } + var other ast.Expr + var val bool + if x { + val = BoolConst(pass, expr.X) + other = expr.Y + } else { + val = BoolConst(pass, expr.Y) + other = expr.X + } + basic, ok := pass.TypesInfo.TypeOf(other).Underlying().(*types.Basic) + if !ok || basic.Kind() != types.Bool { + return + } + op := "" + if (expr.Op == token.EQL && !val) || (expr.Op == token.NEQ && val) { + op = "!" + } + r := op + Render(pass, other) + l1 := len(r) + r = strings.TrimLeft(r, "!") + if (l1-len(r))%2 == 1 { + r = "!" + r + } + if IsInTest(pass, node) { + return + } + ReportNodefFG(pass, expr, "should omit comparison to bool constant, can be simplified to %s", r) + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.BinaryExpr)(nil)}, fn) + return nil, nil +} + +func LintBytesBufferConversions(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + call := node.(*ast.CallExpr) + if len(call.Args) != 1 { + return + } + + argCall, ok := call.Args[0].(*ast.CallExpr) + if !ok { + return + } + sel, ok := argCall.Fun.(*ast.SelectorExpr) + if !ok { + return + } + + typ := pass.TypesInfo.TypeOf(call.Fun) + if typ == types.Universe.Lookup("string").Type() && IsCallToAST(pass, call.Args[0], "(*bytes.Buffer).Bytes") { + ReportNodefFG(pass, call, "should use %v.String() instead of %v", Render(pass, sel.X), Render(pass, call)) + } else if typ, ok := typ.(*types.Slice); ok && typ.Elem() == types.Universe.Lookup("byte").Type() && IsCallToAST(pass, call.Args[0], "(*bytes.Buffer).String") { + ReportNodefFG(pass, call, "should use %v.Bytes() instead of %v", Render(pass, sel.X), Render(pass, call)) + } + + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn) + return nil, nil +} + +func LintStringsContains(pass *analysis.Pass) (interface{}, error) { + // map of value to token to bool value + allowed := map[int64]map[token.Token]bool{ + -1: {token.GTR: true, token.NEQ: true, token.EQL: false}, + 0: {token.GEQ: true, token.LSS: false}, + } + fn := func(node ast.Node) { + expr := node.(*ast.BinaryExpr) + switch expr.Op { + case token.GEQ, token.GTR, token.NEQ, token.LSS, token.EQL: + default: + return + } + + value, ok := ExprToInt(pass, expr.Y) + if !ok { + return + } + + allowedOps, ok := allowed[value] + if !ok { + return + } + b, ok := allowedOps[expr.Op] + if !ok { + return + } + + call, ok := expr.X.(*ast.CallExpr) + if !ok { + return + } + sel, ok := call.Fun.(*ast.SelectorExpr) + if !ok { + return + } + pkgIdent, ok := sel.X.(*ast.Ident) + if !ok { + return + } + funIdent := sel.Sel + if pkgIdent.Name != "strings" && pkgIdent.Name != "bytes" { + return + } + newFunc := "" + switch funIdent.Name { + case "IndexRune": + newFunc = "ContainsRune" + case "IndexAny": + newFunc = "ContainsAny" + case "Index": + newFunc = "Contains" + default: + return + } + + prefix := "" + if !b { + prefix = "!" + } + ReportNodefFG(pass, node, "should use %s%s.%s(%s) instead", prefix, pkgIdent.Name, newFunc, RenderArgs(pass, call.Args)) + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.BinaryExpr)(nil)}, fn) + return nil, nil +} + +func LintBytesCompare(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + expr := node.(*ast.BinaryExpr) + if expr.Op != token.NEQ && expr.Op != token.EQL { + return + } + call, ok := expr.X.(*ast.CallExpr) + if !ok { + return + } + if !IsCallToAST(pass, call, "bytes.Compare") { + return + } + value, ok := ExprToInt(pass, expr.Y) + if !ok || value != 0 { + return + } + args := RenderArgs(pass, call.Args) + prefix := "" + if expr.Op == token.NEQ { + prefix = "!" + } + ReportNodefFG(pass, node, "should use %sbytes.Equal(%s) instead", prefix, args) + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.BinaryExpr)(nil)}, fn) + return nil, nil +} + +func LintForTrue(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + loop := node.(*ast.ForStmt) + if loop.Init != nil || loop.Post != nil { + return + } + if !IsBoolConst(pass, loop.Cond) || !BoolConst(pass, loop.Cond) { + return + } + ReportNodefFG(pass, loop, "should use for {} instead of for true {}") + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.ForStmt)(nil)}, fn) + return nil, nil +} + +func LintRegexpRaw(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + call := node.(*ast.CallExpr) + if !IsCallToAST(pass, call, "regexp.MustCompile") && + !IsCallToAST(pass, call, "regexp.Compile") { + return + } + sel, ok := call.Fun.(*ast.SelectorExpr) + if !ok { + return + } + if len(call.Args) != 1 { + // invalid function call + return + } + lit, ok := call.Args[Arg("regexp.Compile.expr")].(*ast.BasicLit) + if !ok { + // TODO(dominikh): support string concat, maybe support constants + return + } + if lit.Kind != token.STRING { + // invalid function call + return + } + if lit.Value[0] != '"' { + // already a raw string + return + } + val := lit.Value + if !strings.Contains(val, `\\`) { + return + } + if strings.Contains(val, "`") { + return + } + + bs := false + for _, c := range val { + if !bs && c == '\\' { + bs = true + continue + } + if bs && c == '\\' { + bs = false + continue + } + if bs { + // backslash followed by non-backslash -> escape sequence + return + } + } + + ReportNodefFG(pass, call, "should use raw string (`...`) with regexp.%s to avoid having to escape twice", sel.Sel.Name) + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn) + return nil, nil +} + +func LintIfReturn(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + block := node.(*ast.BlockStmt) + l := len(block.List) + if l < 2 { + return + } + n1, n2 := block.List[l-2], block.List[l-1] + + if len(block.List) >= 3 { + if _, ok := block.List[l-3].(*ast.IfStmt); ok { + // Do not flag a series of if statements + return + } + } + // if statement with no init, no else, a single condition + // checking an identifier or function call and just a return + // statement in the body, that returns a boolean constant + ifs, ok := n1.(*ast.IfStmt) + if !ok { + return + } + if ifs.Else != nil || ifs.Init != nil { + return + } + if len(ifs.Body.List) != 1 { + return + } + if op, ok := ifs.Cond.(*ast.BinaryExpr); ok { + switch op.Op { + case token.EQL, token.LSS, token.GTR, token.NEQ, token.LEQ, token.GEQ: + default: + return + } + } + ret1, ok := ifs.Body.List[0].(*ast.ReturnStmt) + if !ok { + return + } + if len(ret1.Results) != 1 { + return + } + if !IsBoolConst(pass, ret1.Results[0]) { + return + } + + ret2, ok := n2.(*ast.ReturnStmt) + if !ok { + return + } + if len(ret2.Results) != 1 { + return + } + if !IsBoolConst(pass, ret2.Results[0]) { + return + } + + if ret1.Results[0].(*ast.Ident).Name == ret2.Results[0].(*ast.Ident).Name { + // we want the function to return true and false, not the + // same value both times. + return + } + + ReportNodefFG(pass, n1, "should use 'return ' instead of 'if { return }; return '") + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.BlockStmt)(nil)}, fn) + return nil, nil +} + +// LintRedundantNilCheckWithLen checks for the following reduntant nil-checks: +// +// if x == nil || len(x) == 0 {} +// if x != nil && len(x) != 0 {} +// if x != nil && len(x) == N {} (where N != 0) +// if x != nil && len(x) > N {} +// if x != nil && len(x) >= N {} (where N != 0) +// +func LintRedundantNilCheckWithLen(pass *analysis.Pass) (interface{}, error) { + isConstZero := func(expr ast.Expr) (isConst bool, isZero bool) { + _, ok := expr.(*ast.BasicLit) + if ok { + return true, IsZero(expr) + } + id, ok := expr.(*ast.Ident) + if !ok { + return false, false + } + c, ok := pass.TypesInfo.ObjectOf(id).(*types.Const) + if !ok { + return false, false + } + return true, c.Val().Kind() == constant.Int && c.Val().String() == "0" + } + + fn := func(node ast.Node) { + // check that expr is "x || y" or "x && y" + expr := node.(*ast.BinaryExpr) + if expr.Op != token.LOR && expr.Op != token.LAND { + return + } + eqNil := expr.Op == token.LOR + + // check that x is "xx == nil" or "xx != nil" + x, ok := expr.X.(*ast.BinaryExpr) + if !ok { + return + } + if eqNil && x.Op != token.EQL { + return + } + if !eqNil && x.Op != token.NEQ { + return + } + xx, ok := x.X.(*ast.Ident) + if !ok { + return + } + if !IsNil(pass, x.Y) { + return + } + + // check that y is "len(xx) == 0" or "len(xx) ... " + y, ok := expr.Y.(*ast.BinaryExpr) + if !ok { + return + } + if eqNil && y.Op != token.EQL { // must be len(xx) *==* 0 + return + } + yx, ok := y.X.(*ast.CallExpr) + if !ok { + return + } + yxFun, ok := yx.Fun.(*ast.Ident) + if !ok || yxFun.Name != "len" || len(yx.Args) != 1 { + return + } + yxArg, ok := yx.Args[Arg("len.v")].(*ast.Ident) + if !ok { + return + } + if yxArg.Name != xx.Name { + return + } + + if eqNil && !IsZero(y.Y) { // must be len(x) == *0* + return + } + + if !eqNil { + isConst, isZero := isConstZero(y.Y) + if !isConst { + return + } + switch y.Op { + case token.EQL: + // avoid false positive for "xx != nil && len(xx) == 0" + if isZero { + return + } + case token.GEQ: + // avoid false positive for "xx != nil && len(xx) >= 0" + if isZero { + return + } + case token.NEQ: + // avoid false positive for "xx != nil && len(xx) != " + if !isZero { + return + } + case token.GTR: + // ok + default: + return + } + } + + // finally check that xx type is one of array, slice, map or chan + // this is to prevent false positive in case if xx is a pointer to an array + var nilType string + switch pass.TypesInfo.TypeOf(xx).(type) { + case *types.Slice: + nilType = "nil slices" + case *types.Map: + nilType = "nil maps" + case *types.Chan: + nilType = "nil channels" + default: + return + } + ReportNodefFG(pass, expr, "should omit nil check; len() for %s is defined as zero", nilType) + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.BinaryExpr)(nil)}, fn) + return nil, nil +} + +func LintSlicing(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + n := node.(*ast.SliceExpr) + if n.Max != nil { + return + } + s, ok := n.X.(*ast.Ident) + if !ok || s.Obj == nil { + return + } + call, ok := n.High.(*ast.CallExpr) + if !ok || len(call.Args) != 1 || call.Ellipsis.IsValid() { + return + } + fun, ok := call.Fun.(*ast.Ident) + if !ok || fun.Name != "len" { + return + } + if _, ok := pass.TypesInfo.ObjectOf(fun).(*types.Builtin); !ok { + return + } + arg, ok := call.Args[Arg("len.v")].(*ast.Ident) + if !ok || arg.Obj != s.Obj { + return + } + ReportNodefFG(pass, n, "should omit second index in slice, s[a:len(s)] is identical to s[a:]") + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.SliceExpr)(nil)}, fn) + return nil, nil +} + +func refersTo(pass *analysis.Pass, expr ast.Expr, ident *ast.Ident) bool { + found := false + fn := func(node ast.Node) bool { + ident2, ok := node.(*ast.Ident) + if !ok { + return true + } + if pass.TypesInfo.ObjectOf(ident) == pass.TypesInfo.ObjectOf(ident2) { + found = true + return false + } + return true + } + ast.Inspect(expr, fn) + return found +} + +func LintLoopAppend(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + loop := node.(*ast.RangeStmt) + if !IsBlank(loop.Key) { + return + } + val, ok := loop.Value.(*ast.Ident) + if !ok { + return + } + if len(loop.Body.List) != 1 { + return + } + stmt, ok := loop.Body.List[0].(*ast.AssignStmt) + if !ok { + return + } + if stmt.Tok != token.ASSIGN || len(stmt.Lhs) != 1 || len(stmt.Rhs) != 1 { + return + } + if refersTo(pass, stmt.Lhs[0], val) { + return + } + call, ok := stmt.Rhs[0].(*ast.CallExpr) + if !ok { + return + } + if len(call.Args) != 2 || call.Ellipsis.IsValid() { + return + } + fun, ok := call.Fun.(*ast.Ident) + if !ok { + return + } + obj := pass.TypesInfo.ObjectOf(fun) + fn, ok := obj.(*types.Builtin) + if !ok || fn.Name() != "append" { + return + } + + src := pass.TypesInfo.TypeOf(loop.X) + dst := pass.TypesInfo.TypeOf(call.Args[Arg("append.slice")]) + // TODO(dominikh) remove nil check once Go issue #15173 has + // been fixed + if src == nil { + return + } + if !types.Identical(src, dst) { + return + } + + if Render(pass, stmt.Lhs[0]) != Render(pass, call.Args[Arg("append.slice")]) { + return + } + + el, ok := call.Args[Arg("append.elems")].(*ast.Ident) + if !ok { + return + } + if pass.TypesInfo.ObjectOf(val) != pass.TypesInfo.ObjectOf(el) { + return + } + ReportNodefFG(pass, loop, "should replace loop with %s = append(%s, %s...)", + Render(pass, stmt.Lhs[0]), Render(pass, call.Args[Arg("append.slice")]), Render(pass, loop.X)) + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.RangeStmt)(nil)}, fn) + return nil, nil +} + +func LintTimeSince(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + call := node.(*ast.CallExpr) + sel, ok := call.Fun.(*ast.SelectorExpr) + if !ok { + return + } + if !IsCallToAST(pass, sel.X, "time.Now") { + return + } + if sel.Sel.Name != "Sub" { + return + } + ReportNodefFG(pass, call, "should use time.Since instead of time.Now().Sub") + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn) + return nil, nil +} + +func LintTimeUntil(pass *analysis.Pass) (interface{}, error) { + if !IsGoVersion(pass, 8) { + return nil, nil + } + fn := func(node ast.Node) { + call := node.(*ast.CallExpr) + if !IsCallToAST(pass, call, "(time.Time).Sub") { + return + } + if !IsCallToAST(pass, call.Args[Arg("(time.Time).Sub.u")], "time.Now") { + return + } + ReportNodefFG(pass, call, "should use time.Until instead of t.Sub(time.Now())") + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn) + return nil, nil +} + +func LintUnnecessaryBlank(pass *analysis.Pass) (interface{}, error) { + fn1 := func(node ast.Node) { + assign := node.(*ast.AssignStmt) + if len(assign.Lhs) != 2 || len(assign.Rhs) != 1 { + return + } + if !IsBlank(assign.Lhs[1]) { + return + } + switch rhs := assign.Rhs[0].(type) { + case *ast.IndexExpr: + // The type-checker should make sure that it's a map, but + // let's be safe. + if _, ok := pass.TypesInfo.TypeOf(rhs.X).Underlying().(*types.Map); !ok { + return + } + case *ast.UnaryExpr: + if rhs.Op != token.ARROW { + return + } + default: + return + } + cp := *assign + cp.Lhs = cp.Lhs[0:1] + ReportNodefFG(pass, assign, "should write %s instead of %s", Render(pass, &cp), Render(pass, assign)) + } + + fn2 := func(node ast.Node) { + stmt := node.(*ast.AssignStmt) + if len(stmt.Lhs) != len(stmt.Rhs) { + return + } + for i, lh := range stmt.Lhs { + rh := stmt.Rhs[i] + if !IsBlank(lh) { + continue + } + expr, ok := rh.(*ast.UnaryExpr) + if !ok { + continue + } + if expr.Op != token.ARROW { + continue + } + ReportNodefFG(pass, lh, "'_ = <-ch' can be simplified to '<-ch'") + } + } + + fn3 := func(node ast.Node) { + rs := node.(*ast.RangeStmt) + + // for x, _ + if !IsBlank(rs.Key) && IsBlank(rs.Value) { + ReportNodefFG(pass, rs.Value, "should omit value from range; this loop is equivalent to `for %s %s range ...`", Render(pass, rs.Key), rs.Tok) + } + // for _, _ || for _ + if IsBlank(rs.Key) && (IsBlank(rs.Value) || rs.Value == nil) { + ReportNodefFG(pass, rs.Key, "should omit values from range; this loop is equivalent to `for range ...`") + } + } + + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.AssignStmt)(nil)}, fn1) + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.AssignStmt)(nil)}, fn2) + if IsGoVersion(pass, 4) { + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.RangeStmt)(nil)}, fn3) + } + return nil, nil +} + +func LintSimplerStructConversion(pass *analysis.Pass) (interface{}, error) { + var skip ast.Node + fn := func(node ast.Node) { + // Do not suggest type conversion between pointers + if unary, ok := node.(*ast.UnaryExpr); ok && unary.Op == token.AND { + if lit, ok := unary.X.(*ast.CompositeLit); ok { + skip = lit + } + return + } + + if node == skip { + return + } + + lit, ok := node.(*ast.CompositeLit) + if !ok { + return + } + typ1, _ := pass.TypesInfo.TypeOf(lit.Type).(*types.Named) + if typ1 == nil { + return + } + s1, ok := typ1.Underlying().(*types.Struct) + if !ok { + return + } + + var typ2 *types.Named + var ident *ast.Ident + getSelType := func(expr ast.Expr) (types.Type, *ast.Ident, bool) { + sel, ok := expr.(*ast.SelectorExpr) + if !ok { + return nil, nil, false + } + ident, ok := sel.X.(*ast.Ident) + if !ok { + return nil, nil, false + } + typ := pass.TypesInfo.TypeOf(sel.X) + return typ, ident, typ != nil + } + if len(lit.Elts) == 0 { + return + } + if s1.NumFields() != len(lit.Elts) { + return + } + for i, elt := range lit.Elts { + var t types.Type + var id *ast.Ident + var ok bool + switch elt := elt.(type) { + case *ast.SelectorExpr: + t, id, ok = getSelType(elt) + if !ok { + return + } + if i >= s1.NumFields() || s1.Field(i).Name() != elt.Sel.Name { + return + } + case *ast.KeyValueExpr: + var sel *ast.SelectorExpr + sel, ok = elt.Value.(*ast.SelectorExpr) + if !ok { + return + } + + if elt.Key.(*ast.Ident).Name != sel.Sel.Name { + return + } + t, id, ok = getSelType(elt.Value) + } + if !ok { + return + } + // All fields must be initialized from the same object + if ident != nil && ident.Obj != id.Obj { + return + } + typ2, _ = t.(*types.Named) + if typ2 == nil { + return + } + ident = id + } + + if typ2 == nil { + return + } + + if typ1.Obj().Pkg() != typ2.Obj().Pkg() { + // Do not suggest type conversions between different + // packages. Types in different packages might only match + // by coincidence. Furthermore, if the dependency ever + // adds more fields to its type, it could break the code + // that relies on the type conversion to work. + return + } + + s2, ok := typ2.Underlying().(*types.Struct) + if !ok { + return + } + if typ1 == typ2 { + return + } + if IsGoVersion(pass, 8) { + if !types.IdenticalIgnoreTags(s1, s2) { + return + } + } else { + if !types.Identical(s1, s2) { + return + } + } + ReportNodefFG(pass, node, "should convert %s (type %s) to %s instead of using struct literal", + ident.Name, typ2.Obj().Name(), typ1.Obj().Name()) + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.UnaryExpr)(nil), (*ast.CompositeLit)(nil)}, fn) + return nil, nil +} + +func LintTrim(pass *analysis.Pass) (interface{}, error) { + sameNonDynamic := func(node1, node2 ast.Node) bool { + if reflect.TypeOf(node1) != reflect.TypeOf(node2) { + return false + } + + switch node1 := node1.(type) { + case *ast.Ident: + return node1.Obj == node2.(*ast.Ident).Obj + case *ast.SelectorExpr: + return Render(pass, node1) == Render(pass, node2) + case *ast.IndexExpr: + return Render(pass, node1) == Render(pass, node2) + } + return false + } + + isLenOnIdent := func(fn ast.Expr, ident ast.Expr) bool { + call, ok := fn.(*ast.CallExpr) + if !ok { + return false + } + if fn, ok := call.Fun.(*ast.Ident); !ok || fn.Name != "len" { + return false + } + if len(call.Args) != 1 { + return false + } + return sameNonDynamic(call.Args[Arg("len.v")], ident) + } + + fn := func(node ast.Node) { + var pkg string + var fun string + + ifstmt := node.(*ast.IfStmt) + if ifstmt.Init != nil { + return + } + if ifstmt.Else != nil { + return + } + if len(ifstmt.Body.List) != 1 { + return + } + condCall, ok := ifstmt.Cond.(*ast.CallExpr) + if !ok { + return + } + switch { + case IsCallToAST(pass, condCall, "strings.HasPrefix"): + pkg = "strings" + fun = "HasPrefix" + case IsCallToAST(pass, condCall, "strings.HasSuffix"): + pkg = "strings" + fun = "HasSuffix" + case IsCallToAST(pass, condCall, "strings.Contains"): + pkg = "strings" + fun = "Contains" + case IsCallToAST(pass, condCall, "bytes.HasPrefix"): + pkg = "bytes" + fun = "HasPrefix" + case IsCallToAST(pass, condCall, "bytes.HasSuffix"): + pkg = "bytes" + fun = "HasSuffix" + case IsCallToAST(pass, condCall, "bytes.Contains"): + pkg = "bytes" + fun = "Contains" + default: + return + } + + assign, ok := ifstmt.Body.List[0].(*ast.AssignStmt) + if !ok { + return + } + if assign.Tok != token.ASSIGN { + return + } + if len(assign.Lhs) != 1 || len(assign.Rhs) != 1 { + return + } + if !sameNonDynamic(condCall.Args[0], assign.Lhs[0]) { + return + } + + switch rhs := assign.Rhs[0].(type) { + case *ast.CallExpr: + if len(rhs.Args) < 2 || !sameNonDynamic(condCall.Args[0], rhs.Args[0]) || !sameNonDynamic(condCall.Args[1], rhs.Args[1]) { + return + } + if IsCallToAST(pass, condCall, "strings.HasPrefix") && IsCallToAST(pass, rhs, "strings.TrimPrefix") || + IsCallToAST(pass, condCall, "strings.HasSuffix") && IsCallToAST(pass, rhs, "strings.TrimSuffix") || + IsCallToAST(pass, condCall, "strings.Contains") && IsCallToAST(pass, rhs, "strings.Replace") || + IsCallToAST(pass, condCall, "bytes.HasPrefix") && IsCallToAST(pass, rhs, "bytes.TrimPrefix") || + IsCallToAST(pass, condCall, "bytes.HasSuffix") && IsCallToAST(pass, rhs, "bytes.TrimSuffix") || + IsCallToAST(pass, condCall, "bytes.Contains") && IsCallToAST(pass, rhs, "bytes.Replace") { + ReportNodefFG(pass, ifstmt, "should replace this if statement with an unconditional %s", CallNameAST(pass, rhs)) + } + return + case *ast.SliceExpr: + slice := rhs + if !ok { + return + } + if slice.Slice3 { + return + } + if !sameNonDynamic(slice.X, condCall.Args[0]) { + return + } + var index ast.Expr + switch fun { + case "HasPrefix": + // TODO(dh) We could detect a High that is len(s), but another + // rule will already flag that, anyway. + if slice.High != nil { + return + } + index = slice.Low + case "HasSuffix": + if slice.Low != nil { + n, ok := ExprToInt(pass, slice.Low) + if !ok || n != 0 { + return + } + } + index = slice.High + } + + switch index := index.(type) { + case *ast.CallExpr: + if fun != "HasPrefix" { + return + } + if fn, ok := index.Fun.(*ast.Ident); !ok || fn.Name != "len" { + return + } + if len(index.Args) != 1 { + return + } + id3 := index.Args[Arg("len.v")] + switch oid3 := condCall.Args[1].(type) { + case *ast.BasicLit: + if pkg != "strings" { + return + } + lit, ok := id3.(*ast.BasicLit) + if !ok { + return + } + s1, ok1 := ExprToString(pass, lit) + s2, ok2 := ExprToString(pass, condCall.Args[1]) + if !ok1 || !ok2 || s1 != s2 { + return + } + default: + if !sameNonDynamic(id3, oid3) { + return + } + } + case *ast.BasicLit, *ast.Ident: + if fun != "HasPrefix" { + return + } + if pkg != "strings" { + return + } + string, ok1 := ExprToString(pass, condCall.Args[1]) + int, ok2 := ExprToInt(pass, slice.Low) + if !ok1 || !ok2 || int != int64(len(string)) { + return + } + case *ast.BinaryExpr: + if fun != "HasSuffix" { + return + } + if index.Op != token.SUB { + return + } + if !isLenOnIdent(index.X, condCall.Args[0]) || + !isLenOnIdent(index.Y, condCall.Args[1]) { + return + } + default: + return + } + + var replacement string + switch fun { + case "HasPrefix": + replacement = "TrimPrefix" + case "HasSuffix": + replacement = "TrimSuffix" + } + ReportNodefFG(pass, ifstmt, "should replace this if statement with an unconditional %s.%s", pkg, replacement) + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.IfStmt)(nil)}, fn) + return nil, nil +} + +func LintLoopSlide(pass *analysis.Pass) (interface{}, error) { + // TODO(dh): detect bs[i+offset] in addition to bs[offset+i] + // TODO(dh): consider merging this function with LintLoopCopy + // TODO(dh): detect length that is an expression, not a variable name + // TODO(dh): support sliding to a different offset than the beginning of the slice + + fn := func(node ast.Node) { + /* + for i := 0; i < n; i++ { + bs[i] = bs[offset+i] + } + + ↓ + + copy(bs[:n], bs[offset:offset+n]) + */ + + loop := node.(*ast.ForStmt) + if len(loop.Body.List) != 1 || loop.Init == nil || loop.Cond == nil || loop.Post == nil { + return + } + assign, ok := loop.Init.(*ast.AssignStmt) + if !ok || len(assign.Lhs) != 1 || len(assign.Rhs) != 1 || !IsZero(assign.Rhs[0]) { + return + } + initvar, ok := assign.Lhs[0].(*ast.Ident) + if !ok { + return + } + post, ok := loop.Post.(*ast.IncDecStmt) + if !ok || post.Tok != token.INC { + return + } + postvar, ok := post.X.(*ast.Ident) + if !ok || pass.TypesInfo.ObjectOf(postvar) != pass.TypesInfo.ObjectOf(initvar) { + return + } + bin, ok := loop.Cond.(*ast.BinaryExpr) + if !ok || bin.Op != token.LSS { + return + } + binx, ok := bin.X.(*ast.Ident) + if !ok || pass.TypesInfo.ObjectOf(binx) != pass.TypesInfo.ObjectOf(initvar) { + return + } + biny, ok := bin.Y.(*ast.Ident) + if !ok { + return + } + + assign, ok = loop.Body.List[0].(*ast.AssignStmt) + if !ok || len(assign.Lhs) != 1 || len(assign.Rhs) != 1 || assign.Tok != token.ASSIGN { + return + } + lhs, ok := assign.Lhs[0].(*ast.IndexExpr) + if !ok { + return + } + rhs, ok := assign.Rhs[0].(*ast.IndexExpr) + if !ok { + return + } + + bs1, ok := lhs.X.(*ast.Ident) + if !ok { + return + } + bs2, ok := rhs.X.(*ast.Ident) + if !ok { + return + } + obj1 := pass.TypesInfo.ObjectOf(bs1) + obj2 := pass.TypesInfo.ObjectOf(bs2) + if obj1 != obj2 { + return + } + if _, ok := obj1.Type().Underlying().(*types.Slice); !ok { + return + } + + index1, ok := lhs.Index.(*ast.Ident) + if !ok || pass.TypesInfo.ObjectOf(index1) != pass.TypesInfo.ObjectOf(initvar) { + return + } + index2, ok := rhs.Index.(*ast.BinaryExpr) + if !ok || index2.Op != token.ADD { + return + } + add1, ok := index2.X.(*ast.Ident) + if !ok { + return + } + add2, ok := index2.Y.(*ast.Ident) + if !ok || pass.TypesInfo.ObjectOf(add2) != pass.TypesInfo.ObjectOf(initvar) { + return + } + + ReportNodefFG(pass, loop, "should use copy(%s[:%s], %s[%s:]) instead", Render(pass, bs1), Render(pass, biny), Render(pass, bs1), Render(pass, add1)) + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.ForStmt)(nil)}, fn) + return nil, nil +} + +func LintMakeLenCap(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + call := node.(*ast.CallExpr) + if fn, ok := call.Fun.(*ast.Ident); !ok || fn.Name != "make" { + // FIXME check whether make is indeed the built-in function + return + } + switch len(call.Args) { + case 2: + // make(T, len) + if _, ok := pass.TypesInfo.TypeOf(call.Args[Arg("make.t")]).Underlying().(*types.Slice); ok { + break + } + if IsZero(call.Args[Arg("make.size[0]")]) { + ReportNodefFG(pass, call.Args[Arg("make.size[0]")], "should use make(%s) instead", Render(pass, call.Args[Arg("make.t")])) + } + case 3: + // make(T, len, cap) + if Render(pass, call.Args[Arg("make.size[0]")]) == Render(pass, call.Args[Arg("make.size[1]")]) { + ReportNodefFG(pass, call.Args[Arg("make.size[0]")], + "should use make(%s, %s) instead", + Render(pass, call.Args[Arg("make.t")]), Render(pass, call.Args[Arg("make.size[0]")])) + } + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn) + return nil, nil +} + +func LintAssertNotNil(pass *analysis.Pass) (interface{}, error) { + isNilCheck := func(ident *ast.Ident, expr ast.Expr) bool { + xbinop, ok := expr.(*ast.BinaryExpr) + if !ok || xbinop.Op != token.NEQ { + return false + } + xident, ok := xbinop.X.(*ast.Ident) + if !ok || xident.Obj != ident.Obj { + return false + } + if !IsNil(pass, xbinop.Y) { + return false + } + return true + } + isOKCheck := func(ident *ast.Ident, expr ast.Expr) bool { + yident, ok := expr.(*ast.Ident) + if !ok || yident.Obj != ident.Obj { + return false + } + return true + } + fn1 := func(node ast.Node) { + ifstmt := node.(*ast.IfStmt) + assign, ok := ifstmt.Init.(*ast.AssignStmt) + if !ok || len(assign.Lhs) != 2 || len(assign.Rhs) != 1 || !IsBlank(assign.Lhs[0]) { + return + } + assert, ok := assign.Rhs[0].(*ast.TypeAssertExpr) + if !ok { + return + } + binop, ok := ifstmt.Cond.(*ast.BinaryExpr) + if !ok || binop.Op != token.LAND { + return + } + assertIdent, ok := assert.X.(*ast.Ident) + if !ok { + return + } + assignIdent, ok := assign.Lhs[1].(*ast.Ident) + if !ok { + return + } + if !(isNilCheck(assertIdent, binop.X) && isOKCheck(assignIdent, binop.Y)) && + !(isNilCheck(assertIdent, binop.Y) && isOKCheck(assignIdent, binop.X)) { + return + } + ReportNodefFG(pass, ifstmt, "when %s is true, %s can't be nil", Render(pass, assignIdent), Render(pass, assertIdent)) + } + fn2 := func(node ast.Node) { + // Check that outer ifstmt is an 'if x != nil {}' + ifstmt := node.(*ast.IfStmt) + if ifstmt.Init != nil { + return + } + if ifstmt.Else != nil { + return + } + if len(ifstmt.Body.List) != 1 { + return + } + binop, ok := ifstmt.Cond.(*ast.BinaryExpr) + if !ok { + return + } + if binop.Op != token.NEQ { + return + } + lhs, ok := binop.X.(*ast.Ident) + if !ok { + return + } + if !IsNil(pass, binop.Y) { + return + } + + // Check that inner ifstmt is an `if _, ok := x.(T); ok {}` + ifstmt, ok = ifstmt.Body.List[0].(*ast.IfStmt) + if !ok { + return + } + assign, ok := ifstmt.Init.(*ast.AssignStmt) + if !ok || len(assign.Lhs) != 2 || len(assign.Rhs) != 1 || !IsBlank(assign.Lhs[0]) { + return + } + assert, ok := assign.Rhs[0].(*ast.TypeAssertExpr) + if !ok { + return + } + assertIdent, ok := assert.X.(*ast.Ident) + if !ok { + return + } + if lhs.Obj != assertIdent.Obj { + return + } + assignIdent, ok := assign.Lhs[1].(*ast.Ident) + if !ok { + return + } + if !isOKCheck(assignIdent, ifstmt.Cond) { + return + } + ReportNodefFG(pass, ifstmt, "when %s is true, %s can't be nil", Render(pass, assignIdent), Render(pass, assertIdent)) + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.IfStmt)(nil)}, fn1) + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.IfStmt)(nil)}, fn2) + return nil, nil +} + +func LintDeclareAssign(pass *analysis.Pass) (interface{}, error) { + hasMultipleAssignments := func(root ast.Node, ident *ast.Ident) bool { + num := 0 + ast.Inspect(root, func(node ast.Node) bool { + if num >= 2 { + return false + } + assign, ok := node.(*ast.AssignStmt) + if !ok { + return true + } + for _, lhs := range assign.Lhs { + if oident, ok := lhs.(*ast.Ident); ok { + if oident.Obj == ident.Obj { + num++ + } + } + } + + return true + }) + return num >= 2 + } + fn := func(node ast.Node) { + block := node.(*ast.BlockStmt) + if len(block.List) < 2 { + return + } + for i, stmt := range block.List[:len(block.List)-1] { + _ = i + decl, ok := stmt.(*ast.DeclStmt) + if !ok { + continue + } + gdecl, ok := decl.Decl.(*ast.GenDecl) + if !ok || gdecl.Tok != token.VAR || len(gdecl.Specs) != 1 { + continue + } + vspec, ok := gdecl.Specs[0].(*ast.ValueSpec) + if !ok || len(vspec.Names) != 1 || len(vspec.Values) != 0 { + continue + } + + assign, ok := block.List[i+1].(*ast.AssignStmt) + if !ok || assign.Tok != token.ASSIGN { + continue + } + if len(assign.Lhs) != 1 || len(assign.Rhs) != 1 { + continue + } + ident, ok := assign.Lhs[0].(*ast.Ident) + if !ok { + continue + } + if vspec.Names[0].Obj != ident.Obj { + continue + } + + if refersTo(pass, assign.Rhs[0], ident) { + continue + } + if hasMultipleAssignments(block, ident) { + continue + } + + ReportNodefFG(pass, decl, "should merge variable declaration with assignment on next line") + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.BlockStmt)(nil)}, fn) + return nil, nil +} + +func LintRedundantBreak(pass *analysis.Pass) (interface{}, error) { + fn1 := func(node ast.Node) { + clause := node.(*ast.CaseClause) + if len(clause.Body) < 2 { + return + } + branch, ok := clause.Body[len(clause.Body)-1].(*ast.BranchStmt) + if !ok || branch.Tok != token.BREAK || branch.Label != nil { + return + } + ReportNodefFG(pass, branch, "redundant break statement") + } + fn2 := func(node ast.Node) { + var ret *ast.FieldList + var body *ast.BlockStmt + switch x := node.(type) { + case *ast.FuncDecl: + ret = x.Type.Results + body = x.Body + case *ast.FuncLit: + ret = x.Type.Results + body = x.Body + default: + panic(fmt.Sprintf("unreachable: %T", node)) + } + // if the func has results, a return can't be redundant. + // similarly, if there are no statements, there can be + // no return. + if ret != nil || body == nil || len(body.List) < 1 { + return + } + rst, ok := body.List[len(body.List)-1].(*ast.ReturnStmt) + if !ok { + return + } + // we don't need to check rst.Results as we already + // checked x.Type.Results to be nil. + ReportNodefFG(pass, rst, "redundant return statement") + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CaseClause)(nil)}, fn1) + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.FuncDecl)(nil), (*ast.FuncLit)(nil)}, fn2) + return nil, nil +} + +func isStringer(T types.Type, msCache *typeutil.MethodSetCache) bool { + ms := msCache.MethodSet(T) + sel := ms.Lookup(nil, "String") + if sel == nil { + return false + } + fn, ok := sel.Obj().(*types.Func) + if !ok { + // should be unreachable + return false + } + sig := fn.Type().(*types.Signature) + if sig.Params().Len() != 0 { + return false + } + if sig.Results().Len() != 1 { + return false + } + if !IsType(sig.Results().At(0).Type(), "string") { + return false + } + return true +} + +func LintRedundantSprintf(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + call := node.(*ast.CallExpr) + if !IsCallToAST(pass, call, "fmt.Sprintf") { + return + } + if len(call.Args) != 2 { + return + } + if s, ok := ExprToString(pass, call.Args[Arg("fmt.Sprintf.format")]); !ok || s != "%s" { + return + } + arg := call.Args[Arg("fmt.Sprintf.a[0]")] + typ := pass.TypesInfo.TypeOf(arg) + + ssapkg := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).Pkg + if isStringer(typ, &ssapkg.Prog.MethodSets) { + ReportNodef(pass, call, "should use String() instead of fmt.Sprintf") + return + } + + if typ.Underlying() == types.Universe.Lookup("string").Type() { + if typ == types.Universe.Lookup("string").Type() { + ReportNodefFG(pass, call, "the argument is already a string, there's no need to use fmt.Sprintf") + } else { + ReportNodefFG(pass, call, "the argument's underlying type is a string, should use a simple conversion instead of fmt.Sprintf") + } + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn) + return nil, nil +} + +func LintErrorsNewSprintf(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + if !IsCallToAST(pass, node, "errors.New") { + return + } + call := node.(*ast.CallExpr) + if !IsCallToAST(pass, call.Args[Arg("errors.New.text")], "fmt.Sprintf") { + return + } + ReportNodefFG(pass, node, "should use fmt.Errorf(...) instead of errors.New(fmt.Sprintf(...))") + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn) + return nil, nil +} + +func LintRangeStringRunes(pass *analysis.Pass) (interface{}, error) { + return sharedcheck.CheckRangeStringRunes(pass) +} + +func LintNilCheckAroundRange(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + ifstmt := node.(*ast.IfStmt) + cond, ok := ifstmt.Cond.(*ast.BinaryExpr) + if !ok { + return + } + + if cond.Op != token.NEQ || !IsNil(pass, cond.Y) || len(ifstmt.Body.List) != 1 { + return + } + + loop, ok := ifstmt.Body.List[0].(*ast.RangeStmt) + if !ok { + return + } + ifXIdent, ok := cond.X.(*ast.Ident) + if !ok { + return + } + rangeXIdent, ok := loop.X.(*ast.Ident) + if !ok { + return + } + if ifXIdent.Obj != rangeXIdent.Obj { + return + } + switch pass.TypesInfo.TypeOf(rangeXIdent).(type) { + case *types.Slice, *types.Map: + ReportNodefFG(pass, node, "unnecessary nil check around range") + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.IfStmt)(nil)}, fn) + return nil, nil +} + +func isPermissibleSort(pass *analysis.Pass, node ast.Node) bool { + call := node.(*ast.CallExpr) + typeconv, ok := call.Args[0].(*ast.CallExpr) + if !ok { + return true + } + + sel, ok := typeconv.Fun.(*ast.SelectorExpr) + if !ok { + return true + } + name := SelectorName(pass, sel) + switch name { + case "sort.IntSlice", "sort.Float64Slice", "sort.StringSlice": + default: + return true + } + + return false +} + +func LintSortHelpers(pass *analysis.Pass) (interface{}, error) { + type Error struct { + node ast.Node + msg string + } + var allErrors []Error + fn := func(node ast.Node) { + var body *ast.BlockStmt + switch node := node.(type) { + case *ast.FuncLit: + body = node.Body + case *ast.FuncDecl: + body = node.Body + default: + panic(fmt.Sprintf("unreachable: %T", node)) + } + if body == nil { + return + } + + var errors []Error + permissible := false + fnSorts := func(node ast.Node) bool { + if permissible { + return false + } + if !IsCallToAST(pass, node, "sort.Sort") { + return true + } + if isPermissibleSort(pass, node) { + permissible = true + return false + } + call := node.(*ast.CallExpr) + typeconv := call.Args[Arg("sort.Sort.data")].(*ast.CallExpr) + sel := typeconv.Fun.(*ast.SelectorExpr) + name := SelectorName(pass, sel) + + switch name { + case "sort.IntSlice": + errors = append(errors, Error{node, "should use sort.Ints(...) instead of sort.Sort(sort.IntSlice(...))"}) + case "sort.Float64Slice": + errors = append(errors, Error{node, "should use sort.Float64s(...) instead of sort.Sort(sort.Float64Slice(...))"}) + case "sort.StringSlice": + errors = append(errors, Error{node, "should use sort.Strings(...) instead of sort.Sort(sort.StringSlice(...))"}) + } + return true + } + ast.Inspect(body, fnSorts) + + if permissible { + return + } + allErrors = append(allErrors, errors...) + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.FuncLit)(nil), (*ast.FuncDecl)(nil)}, fn) + sort.Slice(allErrors, func(i, j int) bool { + return allErrors[i].node.Pos() < allErrors[j].node.Pos() + }) + var prev token.Pos + for _, err := range allErrors { + if err.node.Pos() == prev { + continue + } + prev = err.node.Pos() + ReportNodefFG(pass, err.node, "%s", err.msg) + } + return nil, nil +} + +func LintGuardedDelete(pass *analysis.Pass) (interface{}, error) { + isCommaOkMapIndex := func(stmt ast.Stmt) (b *ast.Ident, m ast.Expr, key ast.Expr, ok bool) { + // Has to be of the form `_, = [] + + assign, ok := stmt.(*ast.AssignStmt) + if !ok { + return nil, nil, nil, false + } + if len(assign.Lhs) != 2 || len(assign.Rhs) != 1 { + return nil, nil, nil, false + } + if !IsBlank(assign.Lhs[0]) { + return nil, nil, nil, false + } + ident, ok := assign.Lhs[1].(*ast.Ident) + if !ok { + return nil, nil, nil, false + } + index, ok := assign.Rhs[0].(*ast.IndexExpr) + if !ok { + return nil, nil, nil, false + } + if _, ok := pass.TypesInfo.TypeOf(index.X).(*types.Map); !ok { + return nil, nil, nil, false + } + key = index.Index + return ident, index.X, key, true + } + fn := func(node ast.Node) { + stmt := node.(*ast.IfStmt) + if len(stmt.Body.List) != 1 { + return + } + if stmt.Else != nil { + return + } + expr, ok := stmt.Body.List[0].(*ast.ExprStmt) + if !ok { + return + } + call, ok := expr.X.(*ast.CallExpr) + if !ok { + return + } + if !IsCallToAST(pass, call, "delete") { + return + } + b, m, key, ok := isCommaOkMapIndex(stmt.Init) + if !ok { + return + } + if cond, ok := stmt.Cond.(*ast.Ident); !ok || pass.TypesInfo.ObjectOf(cond) != pass.TypesInfo.ObjectOf(b) { + return + } + if Render(pass, call.Args[0]) != Render(pass, m) || Render(pass, call.Args[1]) != Render(pass, key) { + return + } + ReportNodefFG(pass, stmt, "unnecessary guard around call to delete") + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.IfStmt)(nil)}, fn) + return nil, nil +} + +func LintSimplifyTypeSwitch(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + stmt := node.(*ast.TypeSwitchStmt) + if stmt.Init != nil { + // bailing out for now, can't anticipate how type switches with initializers are being used + return + } + expr, ok := stmt.Assign.(*ast.ExprStmt) + if !ok { + // the user is in fact assigning the result + return + } + assert := expr.X.(*ast.TypeAssertExpr) + ident, ok := assert.X.(*ast.Ident) + if !ok { + return + } + x := pass.TypesInfo.ObjectOf(ident) + var allOffenders []ast.Node + for _, clause := range stmt.Body.List { + clause := clause.(*ast.CaseClause) + if len(clause.List) != 1 { + continue + } + hasUnrelatedAssertion := false + var offenders []ast.Node + ast.Inspect(clause, func(node ast.Node) bool { + assert2, ok := node.(*ast.TypeAssertExpr) + if !ok { + return true + } + ident, ok := assert2.X.(*ast.Ident) + if !ok { + hasUnrelatedAssertion = true + return false + } + if pass.TypesInfo.ObjectOf(ident) != x { + hasUnrelatedAssertion = true + return false + } + + if !types.Identical(pass.TypesInfo.TypeOf(clause.List[0]), pass.TypesInfo.TypeOf(assert2.Type)) { + hasUnrelatedAssertion = true + return false + } + offenders = append(offenders, assert2) + return true + }) + if !hasUnrelatedAssertion { + // don't flag cases that have other type assertions + // unrelated to the one in the case clause. often + // times, this is done for symmetry, when two + // different values have to be asserted to the same + // type. + allOffenders = append(allOffenders, offenders...) + } + } + if len(allOffenders) != 0 { + at := "" + for _, offender := range allOffenders { + pos := lint.DisplayPosition(pass.Fset, offender.Pos()) + at += "\n\t" + pos.String() + } + ReportNodefFG(pass, expr, "assigning the result of this type assertion to a variable (switch %s := %s.(type)) could eliminate the following type assertions:%s", Render(pass, ident), Render(pass, ident), at) + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.TypeSwitchStmt)(nil)}, fn) + return nil, nil +} diff --git a/vendor/github.com/golangci/go-tools/ssa/LICENSE b/vendor/honnef.co/go/tools/ssa/LICENSE similarity index 100% rename from vendor/github.com/golangci/go-tools/ssa/LICENSE rename to vendor/honnef.co/go/tools/ssa/LICENSE diff --git a/vendor/github.com/golangci/go-tools/ssa/blockopt.go b/vendor/honnef.co/go/tools/ssa/blockopt.go similarity index 100% rename from vendor/github.com/golangci/go-tools/ssa/blockopt.go rename to vendor/honnef.co/go/tools/ssa/blockopt.go diff --git a/vendor/github.com/golangci/go-tools/ssa/builder.go b/vendor/honnef.co/go/tools/ssa/builder.go similarity index 99% rename from vendor/github.com/golangci/go-tools/ssa/builder.go rename to vendor/honnef.co/go/tools/ssa/builder.go index 032819a2a110..317ac0611665 100644 --- a/vendor/github.com/golangci/go-tools/ssa/builder.go +++ b/vendor/honnef.co/go/tools/ssa/builder.go @@ -32,7 +32,7 @@ package ssa import ( "fmt" "go/ast" - exact "go/constant" + "go/constant" "go/token" "go/types" "os" @@ -58,12 +58,12 @@ var ( tString = types.Typ[types.String] tUntypedNil = types.Typ[types.UntypedNil] tRangeIter = &opaqueType{nil, "iter"} // the type of all "range" iterators - tEface = types.NewInterface(nil, nil).Complete() + tEface = types.NewInterfaceType(nil, nil).Complete() // SSA Value constants. vZero = intConst(0) vOne = intConst(1) - vTrue = NewConst(exact.MakeBool(true), tBool) + vTrue = NewConst(constant.MakeBool(true), tBool) ) // builder holds state associated with the package currently being built. @@ -131,11 +131,11 @@ func (b *builder) logicalBinop(fn *Function, e *ast.BinaryExpr) Value { switch e.Op { case token.LAND: b.cond(fn, e.X, rhs, done) - short = NewConst(exact.MakeBool(false), t) + short = NewConst(constant.MakeBool(false), t) case token.LOR: b.cond(fn, e.X, done, rhs) - short = NewConst(exact.MakeBool(true), t) + short = NewConst(constant.MakeBool(true), t) } // Is rhs unreachable? @@ -969,10 +969,10 @@ func (b *builder) setCall(fn *Function, e *ast.CallExpr, c *CallCommon) { c.Args = b.emitCallArgs(fn, sig, e, c.Args) } -// assignOp emits to fn code to perform loc += incr or loc -= incr. -func (b *builder) assignOp(fn *Function, loc lvalue, incr Value, op token.Token, pos token.Pos) { +// assignOp emits to fn code to perform loc = val. +func (b *builder) assignOp(fn *Function, loc lvalue, val Value, op token.Token, pos token.Pos) { oldv := loc.load(fn) - loc.store(fn, emitArith(fn, op, oldv, emitConv(fn, incr, oldv.Type()), loc.typ(), pos)) + loc.store(fn, emitArith(fn, op, oldv, emitConv(fn, val, oldv.Type()), loc.typ(), pos)) } // localValueSpec emits to fn code to define all of the vars in the @@ -1998,7 +1998,7 @@ start: op = token.SUB } loc := b.addr(fn, s.X, false) - b.assignOp(fn, loc, NewConst(exact.MakeInt64(1), loc.typ()), op, s.Pos()) + b.assignOp(fn, loc, NewConst(constant.MakeInt64(1), loc.typ()), op, s.Pos()) case *ast.AssignStmt: switch s.Tok { diff --git a/vendor/github.com/golangci/go-tools/ssa/const.go b/vendor/honnef.co/go/tools/ssa/const.go similarity index 78% rename from vendor/github.com/golangci/go-tools/ssa/const.go rename to vendor/honnef.co/go/tools/ssa/const.go index 140e778c56b4..f95d9e114008 100644 --- a/vendor/github.com/golangci/go-tools/ssa/const.go +++ b/vendor/honnef.co/go/tools/ssa/const.go @@ -8,7 +8,7 @@ package ssa import ( "fmt" - exact "go/constant" + "go/constant" "go/token" "go/types" "strconv" @@ -17,14 +17,14 @@ import ( // NewConst returns a new constant of the specified value and type. // val must be valid according to the specification of Const.Value. // -func NewConst(val exact.Value, typ types.Type) *Const { +func NewConst(val constant.Value, typ types.Type) *Const { return &Const{typ, val} } // intConst returns an 'int' constant that evaluates to i. // (i is an int64 in case the host is narrower than the target.) func intConst(i int64) *Const { - return NewConst(exact.MakeInt64(i), tInt) + return NewConst(constant.MakeInt64(i), tInt) } // nilConst returns a nil constant of the specified type, which may @@ -36,7 +36,7 @@ func nilConst(typ types.Type) *Const { // stringConst returns a 'string' constant that evaluates to s. func stringConst(s string) *Const { - return NewConst(exact.MakeString(s), tString) + return NewConst(constant.MakeString(s), tString) } // zeroConst returns a new "zero" constant of the specified type, @@ -48,11 +48,11 @@ func zeroConst(t types.Type) *Const { case *types.Basic: switch { case t.Info()&types.IsBoolean != 0: - return NewConst(exact.MakeBool(false), t) + return NewConst(constant.MakeBool(false), t) case t.Info()&types.IsNumeric != 0: - return NewConst(exact.MakeInt64(0), t) + return NewConst(constant.MakeInt64(0), t) case t.Info()&types.IsString != 0: - return NewConst(exact.MakeString(""), t) + return NewConst(constant.MakeString(""), t) case t.Kind() == types.UnsafePointer: fallthrough case t.Kind() == types.UntypedNil: @@ -74,8 +74,8 @@ func (c *Const) RelString(from *types.Package) string { var s string if c.Value == nil { s = "nil" - } else if c.Value.Kind() == exact.String { - s = exact.StringVal(c.Value) + } else if c.Value.Kind() == constant.String { + s = constant.StringVal(c.Value) const max = 20 // TODO(adonovan): don't cut a rune in half. if len(s) > max { @@ -115,20 +115,20 @@ func (c *Const) IsNil() bool { return c.Value == nil } -// TODO(adonovan): move everything below into github.com/golangci/go-tools/ssa/interp. +// TODO(adonovan): move everything below into honnef.co/go/tools/ssa/interp. // Int64 returns the numeric value of this constant truncated to fit // a signed 64-bit integer. // func (c *Const) Int64() int64 { - switch x := exact.ToInt(c.Value); x.Kind() { - case exact.Int: - if i, ok := exact.Int64Val(x); ok { + switch x := constant.ToInt(c.Value); x.Kind() { + case constant.Int: + if i, ok := constant.Int64Val(x); ok { return i } return 0 - case exact.Float: - f, _ := exact.Float64Val(x) + case constant.Float: + f, _ := constant.Float64Val(x) return int64(f) } panic(fmt.Sprintf("unexpected constant value: %T", c.Value)) @@ -138,14 +138,14 @@ func (c *Const) Int64() int64 { // an unsigned 64-bit integer. // func (c *Const) Uint64() uint64 { - switch x := exact.ToInt(c.Value); x.Kind() { - case exact.Int: - if u, ok := exact.Uint64Val(x); ok { + switch x := constant.ToInt(c.Value); x.Kind() { + case constant.Int: + if u, ok := constant.Uint64Val(x); ok { return u } return 0 - case exact.Float: - f, _ := exact.Float64Val(x) + case constant.Float: + f, _ := constant.Float64Val(x) return uint64(f) } panic(fmt.Sprintf("unexpected constant value: %T", c.Value)) @@ -155,7 +155,7 @@ func (c *Const) Uint64() uint64 { // a float64. // func (c *Const) Float64() float64 { - f, _ := exact.Float64Val(c.Value) + f, _ := constant.Float64Val(c.Value) return f } @@ -163,7 +163,7 @@ func (c *Const) Float64() float64 { // fit a complex128. // func (c *Const) Complex128() complex128 { - re, _ := exact.Float64Val(exact.Real(c.Value)) - im, _ := exact.Float64Val(exact.Imag(c.Value)) + re, _ := constant.Float64Val(constant.Real(c.Value)) + im, _ := constant.Float64Val(constant.Imag(c.Value)) return complex(re, im) } diff --git a/vendor/github.com/golangci/go-tools/ssa/create.go b/vendor/honnef.co/go/tools/ssa/create.go similarity index 89% rename from vendor/github.com/golangci/go-tools/ssa/create.go rename to vendor/honnef.co/go/tools/ssa/create.go index 69ac12b1bf1c..85163a0c5a74 100644 --- a/vendor/github.com/golangci/go-tools/ssa/create.go +++ b/vendor/honnef.co/go/tools/ssa/create.go @@ -251,12 +251,19 @@ func (prog *Program) AllPackages() []*Package { return pkgs } -// ImportedPackage returns the importable SSA Package whose import -// path is path, or nil if no such SSA package has been created. +// ImportedPackage returns the importable Package whose PkgPath +// is path, or nil if no such Package has been created. // -// Not all packages are importable. For example, no import -// declaration can resolve to the x_test package created by 'go test' -// or the ad-hoc main package created 'go build foo.go'. +// A parameter to CreatePackage determines whether a package should be +// considered importable. For example, no import declaration can resolve +// to the ad-hoc main package created by 'go build foo.go'. +// +// TODO(adonovan): rethink this function and the "importable" concept; +// most packages are importable. This function assumes that all +// types.Package.Path values are unique within the ssa.Program, which is +// false---yet this function remains very convenient. +// Clients should use (*Program).Package instead where possible. +// SSA doesn't really need a string-keyed map of packages. // func (prog *Program) ImportedPackage(path string) *Package { return prog.imported[path] diff --git a/vendor/github.com/golangci/go-tools/ssa/doc.go b/vendor/honnef.co/go/tools/ssa/doc.go similarity index 91% rename from vendor/github.com/golangci/go-tools/ssa/doc.go rename to vendor/honnef.co/go/tools/ssa/doc.go index 2406a31a18da..0f71fda00137 100644 --- a/vendor/github.com/golangci/go-tools/ssa/doc.go +++ b/vendor/honnef.co/go/tools/ssa/doc.go @@ -23,11 +23,13 @@ // such as multi-way branch can be reconstructed as needed; see // ssautil.Switches() for an example. // -// To construct an SSA-form program, call ssautil.CreateProgram on a -// loader.Program, a set of type-checked packages created from -// parsed Go source files. The resulting ssa.Program contains all the -// packages and their members, but SSA code is not created for -// function bodies until a subsequent call to (*Package).Build. +// The simplest way to create the SSA representation of a package is +// to load typed syntax trees using golang.org/x/tools/go/packages, then +// invoke the ssautil.Packages helper function. See ExampleLoadPackages +// and ExampleWholeProgram for examples. +// The resulting ssa.Program contains all the packages and their +// members, but SSA code is not created for function bodies until a +// subsequent call to (*Package).Build or (*Program).Build. // // The builder initially builds a naive SSA form in which all local // variables are addresses of stack locations with explicit loads and @@ -120,4 +122,4 @@ // domains of source locations, ast.Nodes, types.Objects, // ssa.Values/Instructions. // -package ssa // import "github.com/golangci/go-tools/ssa" +package ssa // import "honnef.co/go/tools/ssa" diff --git a/vendor/github.com/golangci/go-tools/ssa/dom.go b/vendor/honnef.co/go/tools/ssa/dom.go similarity index 98% rename from vendor/github.com/golangci/go-tools/ssa/dom.go rename to vendor/honnef.co/go/tools/ssa/dom.go index 12ef4308f3c0..a036be87c4c3 100644 --- a/vendor/github.com/golangci/go-tools/ssa/dom.go +++ b/vendor/honnef.co/go/tools/ssa/dom.go @@ -53,7 +53,7 @@ func (a byDomPreorder) Less(i, j int) bool { return a[i].dom.pre < a[j].dom.pre // func (f *Function) DomPreorder() []*BasicBlock { n := len(f.Blocks) - order := make(byDomPreorder, n, n) + order := make(byDomPreorder, n) copy(order, f.Blocks) sort.Sort(order) return order @@ -123,7 +123,7 @@ func buildDomTree(f *Function) { n := len(f.Blocks) // Allocate space for 5 contiguous [n]*BasicBlock arrays: // sdom, parent, ancestor, preorder, buckets. - space := make([]*BasicBlock, 5*n, 5*n) + space := make([]*BasicBlock, 5*n) lt := ltState{ sdom: space[0:n], parent: space[n : 2*n], @@ -310,6 +310,7 @@ func sanityCheckDomTree(f *Function) { // Printing functions ---------------------------------------- // printDomTree prints the dominator tree as text, using indentation. +//lint:ignore U1000 used during debugging func printDomTreeText(buf *bytes.Buffer, v *BasicBlock, indent int) { fmt.Fprintf(buf, "%*s%s\n", 4*indent, "", v) for _, child := range v.dom.children { @@ -319,6 +320,7 @@ func printDomTreeText(buf *bytes.Buffer, v *BasicBlock, indent int) { // printDomTreeDot prints the dominator tree of f in AT&T GraphViz // (.dot) format. +//lint:ignore U1000 used during debugging func printDomTreeDot(buf *bytes.Buffer, f *Function) { fmt.Fprintln(buf, "//", f) fmt.Fprintln(buf, "digraph domtree {") diff --git a/vendor/github.com/golangci/go-tools/ssa/emit.go b/vendor/honnef.co/go/tools/ssa/emit.go similarity index 99% rename from vendor/github.com/golangci/go-tools/ssa/emit.go rename to vendor/honnef.co/go/tools/ssa/emit.go index 1036988adcb8..6bf9ec32dae9 100644 --- a/vendor/github.com/golangci/go-tools/ssa/emit.go +++ b/vendor/honnef.co/go/tools/ssa/emit.go @@ -127,6 +127,7 @@ func emitCompare(f *Function, op token.Token, x, y Value, pos token.Pos) Value { x = emitConv(f, x, y.Type()) } else if _, ok := y.(*Const); ok { y = emitConv(f, y, x.Type()) + //lint:ignore SA9003 no-op } else { // other cases, e.g. channels. No-op. } diff --git a/vendor/github.com/golangci/go-tools/ssa/func.go b/vendor/honnef.co/go/tools/ssa/func.go similarity index 92% rename from vendor/github.com/golangci/go-tools/ssa/func.go rename to vendor/honnef.co/go/tools/ssa/func.go index 8e958c42258f..222eea64183f 100644 --- a/vendor/github.com/golangci/go-tools/ssa/func.go +++ b/vendor/honnef.co/go/tools/ssa/func.go @@ -265,10 +265,6 @@ func (f *Function) createSyntacticParams(recv *ast.FieldList, functype *ast.Func } } -type setNumable interface { - setNum(int) -} - // numberRegisters assigns numbers to all SSA registers // (value-defining Instructions) in f, to aid debugging. // (Non-Instruction Values are named at construction.) @@ -279,7 +275,9 @@ func numberRegisters(f *Function) { for _, instr := range b.Instrs { switch instr.(type) { case Value: - instr.(setNumable).setNum(v) + instr.(interface { + setNum(int) + }).setNum(v) v++ } } @@ -330,6 +328,70 @@ func (f *Function) finishBody() { } f.Locals = f.Locals[:j] + // comma-ok receiving from a time.Tick channel will never return + // ok == false, so any branching on the value of ok can be + // replaced with an unconditional jump. This will primarily match + // `for range time.Tick(x)` loops, but it can also match + // user-written code. + for _, block := range f.Blocks { + if len(block.Instrs) < 3 { + continue + } + if len(block.Succs) != 2 { + continue + } + var instrs []*Instruction + for i, ins := range block.Instrs { + if _, ok := ins.(*DebugRef); ok { + continue + } + instrs = append(instrs, &block.Instrs[i]) + } + + for i, ins := range instrs { + unop, ok := (*ins).(*UnOp) + if !ok || unop.Op != token.ARROW { + continue + } + call, ok := unop.X.(*Call) + if !ok { + continue + } + if call.Common().IsInvoke() { + continue + } + + // OPT(dh): surely there is a more efficient way of doing + // this, than using FullName. We should already have + // resolved time.Tick somewhere? + v, ok := call.Common().Value.(*Function) + if !ok { + continue + } + t, ok := v.Object().(*types.Func) + if !ok { + continue + } + if t.FullName() != "time.Tick" { + continue + } + ex, ok := (*instrs[i+1]).(*Extract) + if !ok || ex.Tuple != unop || ex.Index != 1 { + continue + } + + ifstmt, ok := (*instrs[i+2]).(*If) + if !ok || ifstmt.Cond != ex { + continue + } + + *instrs[i+2] = NewJump(block) + succ := block.Succs[1] + block.Succs = block.Succs[0:1] + succ.RemovePred(block) + } + } + optimizeBlocks(f) buildReferrers(f) diff --git a/vendor/github.com/golangci/go-tools/ssa/identical.go b/vendor/honnef.co/go/tools/ssa/identical.go similarity index 100% rename from vendor/github.com/golangci/go-tools/ssa/identical.go rename to vendor/honnef.co/go/tools/ssa/identical.go diff --git a/vendor/github.com/golangci/go-tools/ssa/identical_17.go b/vendor/honnef.co/go/tools/ssa/identical_17.go similarity index 100% rename from vendor/github.com/golangci/go-tools/ssa/identical_17.go rename to vendor/honnef.co/go/tools/ssa/identical_17.go diff --git a/vendor/github.com/golangci/go-tools/ssa/lift.go b/vendor/honnef.co/go/tools/ssa/lift.go similarity index 97% rename from vendor/github.com/golangci/go-tools/ssa/lift.go rename to vendor/honnef.co/go/tools/ssa/lift.go index 048e9b03260b..531358fa3bb2 100644 --- a/vendor/github.com/golangci/go-tools/ssa/lift.go +++ b/vendor/honnef.co/go/tools/ssa/lift.go @@ -341,10 +341,10 @@ func phiHasDirectReferrer(phi *Phi) bool { return false } -type blockSet struct{ big.Int } // (inherit methods from Int) +type BlockSet struct{ big.Int } // (inherit methods from Int) // add adds b to the set and returns true if the set changed. -func (s *blockSet) add(b *BasicBlock) bool { +func (s *BlockSet) Add(b *BasicBlock) bool { i := b.Index if s.Bit(i) != 0 { return false @@ -353,9 +353,13 @@ func (s *blockSet) add(b *BasicBlock) bool { return true } +func (s *BlockSet) Has(b *BasicBlock) bool { + return s.Bit(b.Index) == 1 +} + // take removes an arbitrary element from a set s and // returns its index, or returns -1 if empty. -func (s *blockSet) take() int { +func (s *BlockSet) Take() int { l := s.BitLen() for i := 0; i < l; i++ { if s.Bit(i) == 1 { @@ -403,7 +407,7 @@ func liftAlloc(df domFrontier, alloc *Alloc, newPhis newPhiMap, fresh *int) bool // Compute defblocks, the set of blocks containing a // definition of the alloc cell. - var defblocks blockSet + var defblocks BlockSet for _, instr := range *alloc.Referrers() { // Bail out if we discover the alloc is not liftable; // the only operations permitted to use the alloc are @@ -416,7 +420,7 @@ func liftAlloc(df domFrontier, alloc *Alloc, newPhis newPhiMap, fresh *int) bool if instr.Addr != alloc { panic("Alloc.Referrers is inconsistent") } - defblocks.add(instr.Block()) + defblocks.Add(instr.Block()) case *UnOp: if instr.Op != token.MUL { return false // not a load @@ -431,7 +435,7 @@ func liftAlloc(df domFrontier, alloc *Alloc, newPhis newPhiMap, fresh *int) bool } } // The Alloc itself counts as a (zero) definition of the cell. - defblocks.add(alloc.Block()) + defblocks.Add(alloc.Block()) if debugLifting { fmt.Fprintln(os.Stderr, "\tlifting ", alloc, alloc.Name()) @@ -448,18 +452,18 @@ func liftAlloc(df domFrontier, alloc *Alloc, newPhis newPhiMap, fresh *int) bool // // TODO(adonovan): opt: recycle slice storage for W, // hasAlready, defBlocks across liftAlloc calls. - var hasAlready blockSet + var hasAlready BlockSet // Initialize W and work to defblocks. - var work blockSet = defblocks // blocks seen - var W blockSet // blocks to do + var work BlockSet = defblocks // blocks seen + var W BlockSet // blocks to do W.Set(&defblocks.Int) // Traverse iterated dominance frontier, inserting φ-nodes. - for i := W.take(); i != -1; i = W.take() { + for i := W.Take(); i != -1; i = W.Take() { u := fn.Blocks[i] for _, v := range df[u.Index] { - if hasAlready.add(v) { + if hasAlready.Add(v) { // Create φ-node. // It will be prepended to v.Instrs later, if needed. phi := &Phi{ @@ -478,8 +482,8 @@ func liftAlloc(df domFrontier, alloc *Alloc, newPhis newPhiMap, fresh *int) bool } newPhis[v] = append(newPhis[v], newPhi{phi, alloc}) - if work.add(v) { - W.add(v) + if work.Add(v) { + W.Add(v) } } } diff --git a/vendor/github.com/golangci/go-tools/ssa/lvalue.go b/vendor/honnef.co/go/tools/ssa/lvalue.go similarity index 100% rename from vendor/github.com/golangci/go-tools/ssa/lvalue.go rename to vendor/honnef.co/go/tools/ssa/lvalue.go diff --git a/vendor/github.com/golangci/go-tools/ssa/methods.go b/vendor/honnef.co/go/tools/ssa/methods.go similarity index 98% rename from vendor/github.com/golangci/go-tools/ssa/methods.go rename to vendor/honnef.co/go/tools/ssa/methods.go index 080dca968ef8..9cf383916bbe 100644 --- a/vendor/github.com/golangci/go-tools/ssa/methods.go +++ b/vendor/honnef.co/go/tools/ssa/methods.go @@ -23,14 +23,14 @@ import ( // func (prog *Program) MethodValue(sel *types.Selection) *Function { if sel.Kind() != types.MethodVal { - panic(fmt.Sprintf("Method(%s) kind != MethodVal", sel)) + panic(fmt.Sprintf("MethodValue(%s) kind != MethodVal", sel)) } T := sel.Recv() if isInterface(T) { return nil // abstract method } if prog.mode&LogSource != 0 { - defer logStack("Method %s %v", T, sel)() + defer logStack("MethodValue %s %v", T, sel)() } prog.methodsMu.Lock() diff --git a/vendor/github.com/golangci/go-tools/ssa/mode.go b/vendor/honnef.co/go/tools/ssa/mode.go similarity index 100% rename from vendor/github.com/golangci/go-tools/ssa/mode.go rename to vendor/honnef.co/go/tools/ssa/mode.go diff --git a/vendor/github.com/golangci/go-tools/ssa/print.go b/vendor/honnef.co/go/tools/ssa/print.go similarity index 100% rename from vendor/github.com/golangci/go-tools/ssa/print.go rename to vendor/honnef.co/go/tools/ssa/print.go diff --git a/vendor/github.com/golangci/go-tools/ssa/sanity.go b/vendor/honnef.co/go/tools/ssa/sanity.go similarity index 96% rename from vendor/github.com/golangci/go-tools/ssa/sanity.go rename to vendor/honnef.co/go/tools/ssa/sanity.go index c56b2682c083..1d29b66b02c3 100644 --- a/vendor/github.com/golangci/go-tools/ssa/sanity.go +++ b/vendor/honnef.co/go/tools/ssa/sanity.go @@ -410,8 +410,8 @@ func (s *sanity) checkFunction(fn *Function) bool { s.errorf("nil Prog") } - fn.String() // must not crash - fn.RelString(fn.pkg()) // must not crash + _ = fn.String() // must not crash + _ = fn.RelString(fn.pkg()) // must not crash // All functions have a package, except delegates (which are // shared across packages, or duplicated as weak symbols in a @@ -448,6 +448,18 @@ func (s *sanity) checkFunction(fn *Function) bool { if p.Parent() != fn { s.errorf("Param %s at index %d has wrong parent", p.Name(), i) } + // Check common suffix of Signature and Params match type. + if sig := fn.Signature; sig != nil { + j := i - len(fn.Params) + sig.Params().Len() // index within sig.Params + if j < 0 { + continue + } + if !types.Identical(p.Type(), sig.Params().At(j).Type()) { + s.errorf("Param %s at index %d has wrong type (%s, versus %s in Signature)", p.Name(), i, p.Type(), sig.Params().At(j).Type()) + + } + } + s.checkReferrerList(p) } for i, fv := range fn.FreeVars { @@ -490,7 +502,7 @@ func sanityCheckPackage(pkg *Package) { if pkg.Pkg == nil { panic(fmt.Sprintf("Package %s has no Object", pkg)) } - pkg.String() // must not crash + _ = pkg.String() // must not crash for name, mem := range pkg.Members { if name != mem.Name() { diff --git a/vendor/github.com/golangci/go-tools/ssa/source.go b/vendor/honnef.co/go/tools/ssa/source.go similarity index 99% rename from vendor/github.com/golangci/go-tools/ssa/source.go rename to vendor/honnef.co/go/tools/ssa/source.go index 6d2223edaa36..8d9cca170395 100644 --- a/vendor/github.com/golangci/go-tools/ssa/source.go +++ b/vendor/honnef.co/go/tools/ssa/source.go @@ -150,7 +150,7 @@ func findNamedFunc(pkg *Package, pos token.Pos) *Function { // (modulo "untyped" bools resulting from comparisons). // // (Tip: to find the ssa.Value given a source position, use -// importer.PathEnclosingInterval to locate the ast.Node, then +// astutil.PathEnclosingInterval to locate the ast.Node, then // EnclosingFunction to locate the Function, then ValueForExpr to find // the ssa.Value.) // diff --git a/vendor/github.com/golangci/go-tools/ssa/ssa.go b/vendor/honnef.co/go/tools/ssa/ssa.go similarity index 99% rename from vendor/github.com/golangci/go-tools/ssa/ssa.go rename to vendor/honnef.co/go/tools/ssa/ssa.go index 8825e7b5990d..aeddd65e5814 100644 --- a/vendor/github.com/golangci/go-tools/ssa/ssa.go +++ b/vendor/honnef.co/go/tools/ssa/ssa.go @@ -10,7 +10,7 @@ package ssa import ( "fmt" "go/ast" - exact "go/constant" + "go/constant" "go/token" "go/types" "sync" @@ -405,7 +405,7 @@ type Parameter struct { // of the same type and value. // // Value holds the exact value of the constant, independent of its -// Type(), using the same representation as package go/exact uses for +// Type(), using the same representation as package go/constant uses for // constants, or nil for a typed nil value. // // Pos() returns token.NoPos. @@ -417,7 +417,7 @@ type Parameter struct { // type Const struct { typ types.Type - Value exact.Value + Value constant.Value } // A Global is a named Value holding the address of a package-level @@ -572,8 +572,8 @@ type BinOp struct { register // One of: // ADD SUB MUL QUO REM + - * / % - // AND OR XOR SHL SHR AND_NOT & | ^ << >> &~ - // EQL LSS GTR NEQ LEQ GEQ == != < <= < >= + // AND OR XOR SHL SHR AND_NOT & | ^ << >> &^ + // EQL NEQ LSS LEQ GTR GEQ == != < <= < >= Op token.Token X, Y Value } @@ -680,10 +680,10 @@ type ChangeInterface struct { // value of a concrete type. // // Use Program.MethodSets.MethodSet(X.Type()) to find the method-set -// of X, and Program.Method(m) to find the implementation of a method. +// of X, and Program.MethodValue(m) to find the implementation of a method. // // To construct the zero value of an interface type T, use: -// NewConst(exact.MakeNil(), T, pos) +// NewConst(constant.MakeNil(), T, pos) // // Pos() returns the ast.CallExpr.Lparen, if the instruction arose // from an explicit conversion in the source. @@ -813,7 +813,7 @@ type Slice struct { type FieldAddr struct { register X Value // *struct - Field int // index into X.Type().Deref().(*types.Struct).Fields + Field int // field is X.Type().Underlying().(*types.Pointer).Elem().Underlying().(*types.Struct).Field(Field) } // The Field instruction yields the Field of struct X. diff --git a/vendor/honnef.co/go/tools/ssa/staticcheck.conf b/vendor/honnef.co/go/tools/ssa/staticcheck.conf new file mode 100644 index 000000000000..d7b38bc3563d --- /dev/null +++ b/vendor/honnef.co/go/tools/ssa/staticcheck.conf @@ -0,0 +1,3 @@ +# ssa/... is mostly imported from upstream and we don't want to +# deviate from it too much, hence disabling SA1019 +checks = ["inherit", "-SA1019"] diff --git a/vendor/github.com/golangci/go-tools/ssa/testmain.go b/vendor/honnef.co/go/tools/ssa/testmain.go similarity index 96% rename from vendor/github.com/golangci/go-tools/ssa/testmain.go rename to vendor/honnef.co/go/tools/ssa/testmain.go index ea232ada951f..8ec15ba50513 100644 --- a/vendor/github.com/golangci/go-tools/ssa/testmain.go +++ b/vendor/honnef.co/go/tools/ssa/testmain.go @@ -8,8 +8,8 @@ package ssa // tests of the supplied packages. // It is closely coupled to $GOROOT/src/cmd/go/test.go and $GOROOT/src/testing. // -// TODO(adonovan): this file no longer needs to live in the ssa package. -// Move it to ssautil. +// TODO(adonovan): throws this all away now that x/tools/go/packages +// provides access to the actual synthetic test main files. import ( "bytes" @@ -26,6 +26,8 @@ import ( // FindTests returns the Test, Benchmark, and Example functions // (as defined by "go test") defined in the specified package, // and its TestMain function, if any. +// +// Deprecated: use x/tools/go/packages to access synthetic testmain packages. func FindTests(pkg *Package) (tests, benchmarks, examples []*Function, main *Function) { prog := pkg.Prog @@ -109,6 +111,8 @@ func isTest(name, prefix string) bool { // // Subsequent calls to prog.AllPackages include the new package. // The package pkg must belong to the program prog. +// +// Deprecated: use x/tools/go/packages to access synthetic testmain packages. func (prog *Program) CreateTestMainPackage(pkg *Package) *Package { if pkg.Prog != prog { log.Fatal("Package does not belong to Program") diff --git a/vendor/github.com/golangci/go-tools/ssa/util.go b/vendor/honnef.co/go/tools/ssa/util.go similarity index 100% rename from vendor/github.com/golangci/go-tools/ssa/util.go rename to vendor/honnef.co/go/tools/ssa/util.go diff --git a/vendor/github.com/golangci/go-tools/ssa/wrappers.go b/vendor/honnef.co/go/tools/ssa/wrappers.go similarity index 98% rename from vendor/github.com/golangci/go-tools/ssa/wrappers.go rename to vendor/honnef.co/go/tools/ssa/wrappers.go index 701dd90d7d9d..a4ae71d8cfcf 100644 --- a/vendor/github.com/golangci/go-tools/ssa/wrappers.go +++ b/vendor/honnef.co/go/tools/ssa/wrappers.go @@ -141,13 +141,9 @@ func makeWrapper(prog *Program, sel *types.Selection) *Function { // start is the index of the first regular parameter to use. // func createParams(fn *Function, start int) { - var last *Parameter tparams := fn.Signature.Params() for i, n := start, tparams.Len(); i < n; i++ { - last = fn.addParamObj(tparams.At(i)) - } - if fn.Signature.Variadic() { - last.typ = types.NewSlice(last.typ) + fn.addParamObj(tparams.At(i)) } } diff --git a/vendor/github.com/golangci/go-tools/ssa/write.go b/vendor/honnef.co/go/tools/ssa/write.go similarity index 100% rename from vendor/github.com/golangci/go-tools/ssa/write.go rename to vendor/honnef.co/go/tools/ssa/write.go diff --git a/vendor/github.com/golangci/go-tools/ssautil/ssautil.go b/vendor/honnef.co/go/tools/ssautil/ssautil.go similarity index 60% rename from vendor/github.com/golangci/go-tools/ssautil/ssautil.go rename to vendor/honnef.co/go/tools/ssautil/ssautil.go index 19b7e1018866..72c3c919d62c 100644 --- a/vendor/github.com/golangci/go-tools/ssautil/ssautil.go +++ b/vendor/honnef.co/go/tools/ssautil/ssautil.go @@ -1,7 +1,7 @@ package ssautil import ( - "github.com/golangci/go-tools/ssa" + "honnef.co/go/tools/ssa" ) func Reachable(from, to *ssa.BasicBlock) bool { @@ -39,3 +39,20 @@ func Walk(b *ssa.BasicBlock, fn func(*ssa.BasicBlock) bool) { wl = append(wl, b.Succs...) } } + +func Vararg(x *ssa.Slice) ([]ssa.Value, bool) { + var out []ssa.Value + slice, ok := x.X.(*ssa.Alloc) + if !ok || slice.Comment != "varargs" { + return nil, false + } + for _, ref := range *slice.Referrers() { + idx, ok := ref.(*ssa.IndexAddr) + if !ok { + continue + } + v := (*idx.Referrers())[0].(*ssa.Store).Val + out = append(out, v) + } + return out, true +} diff --git a/vendor/github.com/golangci/go-tools/staticcheck/CONTRIBUTING.md b/vendor/honnef.co/go/tools/staticcheck/CONTRIBUTING.md similarity index 88% rename from vendor/github.com/golangci/go-tools/staticcheck/CONTRIBUTING.md rename to vendor/honnef.co/go/tools/staticcheck/CONTRIBUTING.md index 42573b78be0f..b12c7afc748e 100644 --- a/vendor/github.com/golangci/go-tools/staticcheck/CONTRIBUTING.md +++ b/vendor/honnef.co/go/tools/staticcheck/CONTRIBUTING.md @@ -6,7 +6,7 @@ Check you have the latest version of its dependencies. Run ``` -go get -u github.com/golangci/go-tools/staticcheck +go get -u honnef.co/go/tools/staticcheck ``` If you still have problems, consider searching for existing issues before filing a new issue. diff --git a/vendor/honnef.co/go/tools/staticcheck/analysis.go b/vendor/honnef.co/go/tools/staticcheck/analysis.go new file mode 100644 index 000000000000..442aebe5a18d --- /dev/null +++ b/vendor/honnef.co/go/tools/staticcheck/analysis.go @@ -0,0 +1,525 @@ +package staticcheck + +import ( + "flag" + + "honnef.co/go/tools/facts" + "honnef.co/go/tools/internal/passes/buildssa" + "honnef.co/go/tools/lint/lintutil" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/inspect" +) + +func newFlagSet() flag.FlagSet { + fs := flag.NewFlagSet("", flag.PanicOnError) + fs.Var(lintutil.NewVersionFlag(), "go", "Target Go version") + return *fs +} + +var Analyzers = map[string]*analysis.Analyzer{ + "SA1000": { + Name: "SA1000", + Run: callChecker(checkRegexpRules), + Doc: Docs["SA1000"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, valueRangesAnalyzer}, + Flags: newFlagSet(), + }, + "SA1001": { + Name: "SA1001", + Run: CheckTemplate, + Doc: Docs["SA1001"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA1002": { + Name: "SA1002", + Run: callChecker(checkTimeParseRules), + Doc: Docs["SA1002"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, valueRangesAnalyzer}, + Flags: newFlagSet(), + }, + "SA1003": { + Name: "SA1003", + Run: callChecker(checkEncodingBinaryRules), + Doc: Docs["SA1003"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, valueRangesAnalyzer}, + Flags: newFlagSet(), + }, + "SA1004": { + Name: "SA1004", + Run: CheckTimeSleepConstant, + Doc: Docs["SA1004"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA1005": { + Name: "SA1005", + Run: CheckExec, + Doc: Docs["SA1005"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA1006": { + Name: "SA1006", + Run: CheckUnsafePrintf, + Doc: Docs["SA1006"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA1007": { + Name: "SA1007", + Run: callChecker(checkURLsRules), + Doc: Docs["SA1007"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, valueRangesAnalyzer}, + Flags: newFlagSet(), + }, + "SA1008": { + Name: "SA1008", + Run: CheckCanonicalHeaderKey, + Doc: Docs["SA1008"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA1010": { + Name: "SA1010", + Run: callChecker(checkRegexpFindAllRules), + Doc: Docs["SA1010"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, valueRangesAnalyzer}, + Flags: newFlagSet(), + }, + "SA1011": { + Name: "SA1011", + Run: callChecker(checkUTF8CutsetRules), + Doc: Docs["SA1011"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, valueRangesAnalyzer}, + Flags: newFlagSet(), + }, + "SA1012": { + Name: "SA1012", + Run: CheckNilContext, + Doc: Docs["SA1012"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA1013": { + Name: "SA1013", + Run: CheckSeeker, + Doc: Docs["SA1013"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA1014": { + Name: "SA1014", + Run: callChecker(checkUnmarshalPointerRules), + Doc: Docs["SA1014"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, valueRangesAnalyzer}, + Flags: newFlagSet(), + }, + "SA1015": { + Name: "SA1015", + Run: CheckLeakyTimeTick, + Doc: Docs["SA1015"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + Flags: newFlagSet(), + }, + "SA1016": { + Name: "SA1016", + Run: CheckUntrappableSignal, + Doc: Docs["SA1016"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA1017": { + Name: "SA1017", + Run: callChecker(checkUnbufferedSignalChanRules), + Doc: Docs["SA1017"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, valueRangesAnalyzer}, + Flags: newFlagSet(), + }, + "SA1018": { + Name: "SA1018", + Run: callChecker(checkStringsReplaceZeroRules), + Doc: Docs["SA1018"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, valueRangesAnalyzer}, + Flags: newFlagSet(), + }, + "SA1019": { + Name: "SA1019", + Run: CheckDeprecated, + Doc: Docs["SA1019"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Deprecated}, + Flags: newFlagSet(), + }, + "SA1020": { + Name: "SA1020", + Run: callChecker(checkListenAddressRules), + Doc: Docs["SA1020"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, valueRangesAnalyzer}, + Flags: newFlagSet(), + }, + "SA1021": { + Name: "SA1021", + Run: callChecker(checkBytesEqualIPRules), + Doc: Docs["SA1021"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, valueRangesAnalyzer}, + Flags: newFlagSet(), + }, + "SA1023": { + Name: "SA1023", + Run: CheckWriterBufferModified, + Doc: Docs["SA1023"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + Flags: newFlagSet(), + }, + "SA1024": { + Name: "SA1024", + Run: callChecker(checkUniqueCutsetRules), + Doc: Docs["SA1024"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, valueRangesAnalyzer}, + Flags: newFlagSet(), + }, + "SA1025": { + Name: "SA1025", + Run: CheckTimerResetReturnValue, + Doc: Docs["SA1025"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + Flags: newFlagSet(), + }, + "SA1026": { + Name: "SA1026", + Run: callChecker(checkUnsupportedMarshal), + Doc: Docs["SA1026"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, valueRangesAnalyzer}, + Flags: newFlagSet(), + }, + "SA1027": { + Name: "SA1027", + Run: callChecker(checkAtomicAlignment), + Doc: Docs["SA1027"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, valueRangesAnalyzer}, + Flags: newFlagSet(), + }, + + "SA2000": { + Name: "SA2000", + Run: CheckWaitgroupAdd, + Doc: Docs["SA2000"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA2001": { + Name: "SA2001", + Run: CheckEmptyCriticalSection, + Doc: Docs["SA2001"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA2002": { + Name: "SA2002", + Run: CheckConcurrentTesting, + Doc: Docs["SA2002"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + Flags: newFlagSet(), + }, + "SA2003": { + Name: "SA2003", + Run: CheckDeferLock, + Doc: Docs["SA2003"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + Flags: newFlagSet(), + }, + + "SA3000": { + Name: "SA3000", + Run: CheckTestMainExit, + Doc: Docs["SA3000"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA3001": { + Name: "SA3001", + Run: CheckBenchmarkN, + Doc: Docs["SA3001"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + + "SA4000": { + Name: "SA4000", + Run: CheckLhsRhsIdentical, + Doc: Docs["SA4000"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.TokenFile, facts.Generated}, + Flags: newFlagSet(), + }, + "SA4001": { + Name: "SA4001", + Run: CheckIneffectiveCopy, + Doc: Docs["SA4001"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA4002": { + Name: "SA4002", + Run: CheckDiffSizeComparison, + Doc: Docs["SA4002"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, valueRangesAnalyzer}, + Flags: newFlagSet(), + }, + "SA4003": { + Name: "SA4003", + Run: CheckExtremeComparison, + Doc: Docs["SA4003"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA4004": { + Name: "SA4004", + Run: CheckIneffectiveLoop, + Doc: Docs["SA4004"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA4006": { + Name: "SA4006", + Run: CheckUnreadVariableValues, + Doc: Docs["SA4006"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "SA4008": { + Name: "SA4008", + Run: CheckLoopCondition, + Doc: Docs["SA4008"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + Flags: newFlagSet(), + }, + "SA4009": { + Name: "SA4009", + Run: CheckArgOverwritten, + Doc: Docs["SA4009"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + Flags: newFlagSet(), + }, + "SA4010": { + Name: "SA4010", + Run: CheckIneffectiveAppend, + Doc: Docs["SA4010"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + Flags: newFlagSet(), + }, + "SA4011": { + Name: "SA4011", + Run: CheckScopedBreak, + Doc: Docs["SA4011"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA4012": { + Name: "SA4012", + Run: CheckNaNComparison, + Doc: Docs["SA4012"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + Flags: newFlagSet(), + }, + "SA4013": { + Name: "SA4013", + Run: CheckDoubleNegation, + Doc: Docs["SA4013"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA4014": { + Name: "SA4014", + Run: CheckRepeatedIfElse, + Doc: Docs["SA4014"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA4015": { + Name: "SA4015", + Run: callChecker(checkMathIntRules), + Doc: Docs["SA4015"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, valueRangesAnalyzer}, + Flags: newFlagSet(), + }, + "SA4016": { + Name: "SA4016", + Run: CheckSillyBitwiseOps, + Doc: Docs["SA4016"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, facts.TokenFile}, + Flags: newFlagSet(), + }, + "SA4017": { + Name: "SA4017", + Run: CheckPureFunctions, + Doc: Docs["SA4017"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, facts.Purity}, + Flags: newFlagSet(), + }, + "SA4018": { + Name: "SA4018", + Run: CheckSelfAssignment, + Doc: Docs["SA4018"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated, facts.TokenFile}, + Flags: newFlagSet(), + }, + "SA4019": { + Name: "SA4019", + Run: CheckDuplicateBuildConstraints, + Doc: Docs["SA4019"].String(), + Requires: []*analysis.Analyzer{facts.Generated}, + Flags: newFlagSet(), + }, + "SA4020": { + Name: "SA4020", + Run: CheckUnreachableTypeCases, + Doc: Docs["SA4020"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA4021": { + Name: "SA4021", + Run: CheckSingleArgAppend, + Doc: Docs["SA4021"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated, facts.TokenFile}, + Flags: newFlagSet(), + }, + + "SA5000": { + Name: "SA5000", + Run: CheckNilMaps, + Doc: Docs["SA5000"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + Flags: newFlagSet(), + }, + "SA5001": { + Name: "SA5001", + Run: CheckEarlyDefer, + Doc: Docs["SA5001"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA5002": { + Name: "SA5002", + Run: CheckInfiniteEmptyLoop, + Doc: Docs["SA5002"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA5003": { + Name: "SA5003", + Run: CheckDeferInInfiniteLoop, + Doc: Docs["SA5003"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA5004": { + Name: "SA5004", + Run: CheckLoopEmptyDefault, + Doc: Docs["SA5004"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA5005": { + Name: "SA5005", + Run: CheckCyclicFinalizer, + Doc: Docs["SA5005"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + Flags: newFlagSet(), + }, + "SA5007": { + Name: "SA5007", + Run: CheckInfiniteRecursion, + Doc: Docs["SA5007"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + Flags: newFlagSet(), + }, + "SA5008": { + Name: "SA5008", + Run: CheckStructTags, + Doc: Docs["SA5008"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA5009": { + Name: "SA5009", + Run: callChecker(checkPrintfRules), + Doc: Docs["SA5009"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, valueRangesAnalyzer}, + Flags: newFlagSet(), + }, + + "SA6000": { + Name: "SA6000", + Run: callChecker(checkRegexpMatchLoopRules), + Doc: Docs["SA6000"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, valueRangesAnalyzer}, + Flags: newFlagSet(), + }, + "SA6001": { + Name: "SA6001", + Run: CheckMapBytesKey, + Doc: Docs["SA6001"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + Flags: newFlagSet(), + }, + "SA6002": { + Name: "SA6002", + Run: callChecker(checkSyncPoolValueRules), + Doc: Docs["SA6002"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, valueRangesAnalyzer}, + Flags: newFlagSet(), + }, + "SA6003": { + Name: "SA6003", + Run: CheckRangeStringRunes, + Doc: Docs["SA6003"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + Flags: newFlagSet(), + }, + "SA6005": { + Name: "SA6005", + Run: CheckToLowerToUpperComparison, + Doc: Docs["SA6005"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + + "SA9001": { + Name: "SA9001", + Run: CheckDubiousDeferInChannelRangeLoop, + Doc: Docs["SA9001"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA9002": { + Name: "SA9002", + Run: CheckNonOctalFileMode, + Doc: Docs["SA9002"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + "SA9003": { + Name: "SA9003", + Run: CheckEmptyBranch, + Doc: Docs["SA9003"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, facts.TokenFile, facts.Generated}, + Flags: newFlagSet(), + }, + "SA9004": { + Name: "SA9004", + Run: CheckMissingEnumTypesInDeclaration, + Doc: Docs["SA9004"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, + // Filtering generated code because it may include empty structs generated from data models. + "SA9005": { + Name: "SA9005", + Run: callChecker(checkNoopMarshal), + Doc: Docs["SA9005"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, valueRangesAnalyzer, facts.Generated, facts.TokenFile}, + Flags: newFlagSet(), + }, +} diff --git a/vendor/github.com/golangci/go-tools/staticcheck/buildtag.go b/vendor/honnef.co/go/tools/staticcheck/buildtag.go similarity index 88% rename from vendor/github.com/golangci/go-tools/staticcheck/buildtag.go rename to vendor/honnef.co/go/tools/staticcheck/buildtag.go index d617d60298b6..888d3e9dc056 100644 --- a/vendor/github.com/golangci/go-tools/staticcheck/buildtag.go +++ b/vendor/honnef.co/go/tools/staticcheck/buildtag.go @@ -4,7 +4,7 @@ import ( "go/ast" "strings" - . "github.com/golangci/go-tools/lint/lintdsl" + . "honnef.co/go/tools/lint/lintdsl" ) func buildTags(f *ast.File) [][]string { diff --git a/vendor/honnef.co/go/tools/staticcheck/doc.go b/vendor/honnef.co/go/tools/staticcheck/doc.go new file mode 100644 index 000000000000..4a87d4a24cea --- /dev/null +++ b/vendor/honnef.co/go/tools/staticcheck/doc.go @@ -0,0 +1,764 @@ +package staticcheck + +import "honnef.co/go/tools/lint" + +var Docs = map[string]*lint.Documentation{ + "SA1000": &lint.Documentation{ + Title: `Invalid regular expression`, + Since: "2017.1", + }, + + "SA1001": &lint.Documentation{ + Title: `Invalid template`, + Since: "2017.1", + }, + + "SA1002": &lint.Documentation{ + Title: `Invalid format in time.Parse`, + Since: "2017.1", + }, + + "SA1003": &lint.Documentation{ + Title: `Unsupported argument to functions in encoding/binary`, + Text: `The encoding/binary package can only serialize types with known sizes. +This precludes the use of the int and uint types, as their sizes +differ on different architectures. Furthermore, it doesn't support +serializing maps, channels, strings, or functions. + +Before Go 1.8, bool wasn't supported, either.`, + Since: "2017.1", + }, + + "SA1004": &lint.Documentation{ + Title: `Suspiciously small untyped constant in time.Sleep`, + Text: `The time.Sleep function takes a time.Duration as its only argument. +Durations are expressed in nanoseconds. Thus, calling time.Sleep(1) +will sleep for 1 nanosecond. This is a common source of bugs, as sleep +functions in other languages often accept seconds or milliseconds. + +The time package provides constants such as time.Second to express +large durations. These can be combined with arithmetic to express +arbitrary durations, for example '5 * time.Second' for 5 seconds. + +If you truly meant to sleep for a tiny amount of time, use +'n * time.Nanosecond' to signal to staticcheck that you did mean to sleep +for some amount of nanoseconds.`, + Since: "2017.1", + }, + + "SA1005": &lint.Documentation{ + Title: `Invalid first argument to exec.Command`, + Text: `os/exec runs programs directly (using variants of the fork and exec +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")`, + Since: "2017.1", + }, + + "SA1006": &lint.Documentation{ + Title: `Printf with dynamic first argument and no further arguments`, + Text: `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 parameter 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.`, + Since: "2017.1", + }, + + "SA1007": &lint.Documentation{ + Title: `Invalid URL in net/url.Parse`, + Since: "2017.1", + }, + + "SA1008": &lint.Documentation{ + Title: `Non-canonical key in http.Header map`, + Text: `Keys in http.Header maps are canonical, meaning they follow a specific +combination of uppercase and lowercase letters. Methods such as +http.Header.Add and http.Header.Del convert inputs into this canonical +form before manipulating the map. + +When manipulating http.Header maps directly, as opposed to using the +provided methods, care should be taken to stick to canonical form in +order to avoid inconsistencies. The following piece of code +demonstrates one such inconsistency: + + h := http.Header{} + h["etag"] = []string{"1234"} + h.Add("etag", "5678") + fmt.Println(h) + + // Output: + // map[Etag:[5678] etag:[1234]] + +The easiest way of obtaining the canonical form of a key is to use +http.CanonicalHeaderKey.`, + Since: "2017.1", + }, + + "SA1010": &lint.Documentation{ + Title: `(*regexp.Regexp).FindAll called with n == 0, which will always return zero results`, + Text: `If n >= 0, the function returns at most n matches/submatches. To +return all results, specify a negative number.`, + Since: "2017.1", + }, + + "SA1011": &lint.Documentation{ + Title: `Various methods in the strings package expect valid UTF-8, but invalid input is provided`, + Since: "2017.1", + }, + + "SA1012": &lint.Documentation{ + Title: `A nil context.Context is being passed to a function, consider using context.TODO instead`, + Since: "2017.1", + }, + + "SA1013": &lint.Documentation{ + Title: `io.Seeker.Seek is being called with the whence constant as the first argument, but it should be the second`, + Since: "2017.1", + }, + + "SA1014": &lint.Documentation{ + Title: `Non-pointer value passed to Unmarshal or Decode`, + Since: "2017.1", + }, + + "SA1015": &lint.Documentation{ + Title: `Using time.Tick in a way that will leak. Consider using time.NewTicker, and only use time.Tick in tests, commands and endless functions`, + Since: "2017.1", + }, + + "SA1016": &lint.Documentation{ + Title: `Trapping a signal that cannot be trapped`, + Text: `Not all signals can be intercepted by a process. Speficially, on +UNIX-like systems, the syscall.SIGKILL and syscall.SIGSTOP signals are +never passed to the process, but instead handled directly by the +kernel. It is therefore pointless to try and handle these signals.`, + Since: "2017.1", + }, + + "SA1017": &lint.Documentation{ + Title: `Channels used with os/signal.Notify should be buffered`, + Text: `The os/signal package uses non-blocking channel sends when delivering +signals. If the receiving end of the channel isn't ready and the +channel is either unbuffered or full, the signal will be dropped. To +avoid missing signals, the channel should be buffered and of the +appropriate size. For a channel used for notification of just one +signal value, a buffer of size 1 is sufficient.`, + Since: "2017.1", + }, + + "SA1018": &lint.Documentation{ + Title: `strings.Replace called with n == 0, which does nothing`, + Text: `With n == 0, zero instances will be replaced. To replace all +instances, use a negative number, or use strings.ReplaceAll.`, + Since: "2017.1", + }, + + "SA1019": &lint.Documentation{ + Title: `Using a deprecated function, variable, constant or field`, + Since: "2017.1", + }, + + "SA1020": &lint.Documentation{ + Title: `Using an invalid host:port pair with a net.Listen-related function`, + Since: "2017.1", + }, + + "SA1021": &lint.Documentation{ + Title: `Using bytes.Equal to compare two net.IP`, + Text: `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.IPs, the net.IP.Equal method should +be used, as it takes both representations into account.`, + Since: "2017.1", + }, + + "SA1023": &lint.Documentation{ + Title: `Modifying the buffer in an io.Writer implementation`, + Text: `Write must not modify the slice data, even temporarily.`, + Since: "2017.1", + }, + + "SA1024": &lint.Documentation{ + Title: `A string cutset contains duplicate characters`, + Text: `The strings.TrimLeft and strings.TrimRight functions take cutsets, not +prefixes. A cutset is treated as a set of characters to remove from a +string. For example, + + strings.TrimLeft("42133word", "1234")) + +will result in the string "word" – any characters that are 1, 2, 3 or +4 are cut from the left of the string. + +In order to remove one string from another, use strings.TrimPrefix instead.`, + Since: "2017.1", + }, + + "SA1025": &lint.Documentation{ + Title: `It is not possible to use (*time.Timer).Reset's return value correctly`, + Since: "2019.1", + }, + + "SA1026": &lint.Documentation{ + Title: `Cannot marshal channels or functions`, + Since: "2019.2", + }, + + "SA1027": &lint.Documentation{ + Title: `Atomic access to 64-bit variable must be 64-bit aligned`, + Text: `On ARM, x86-32, and 32-bit MIPS, it is the caller's responsibility to +arrange for 64-bit alignment of 64-bit words accessed atomically. The +first word in a variable or in an allocated struct, array, or slice +can be relied upon to be 64-bit aligned. + +You can use the structlayout tool to inspect the alignment of fields +in a struct.`, + Since: "2019.2", + }, + + "SA2000": &lint.Documentation{ + Title: `sync.WaitGroup.Add called inside the goroutine, leading to a race condition`, + Since: "2017.1", + }, + + "SA2001": &lint.Documentation{ + Title: `Empty critical section, did you mean to defer the unlock?`, + Text: `Empty critical sections of the kind + + mu.Lock() + mu.Unlock() + +are very often a typo, and the following was intended instead: + + mu.Lock() + defer mu.Unlock() + +Do note that sometimes empty critical sections can be useful, as a +form of signaling to wait on another goroutine. Many times, there are +simpler ways of achieving the same effect. When that isn't the case, +the code should be amply commented to avoid confusion. Combining such +comments with a //lint:ignore directive can be used to suppress this +rare false positive.`, + Since: "2017.1", + }, + + "SA2002": &lint.Documentation{ + Title: `Called testing.T.FailNow or SkipNow in a goroutine, which isn't allowed`, + Since: "2017.1", + }, + + "SA2003": &lint.Documentation{ + Title: `Deferred Lock right after locking, likely meant to defer Unlock instead`, + Since: "2017.1", + }, + + "SA3000": &lint.Documentation{ + Title: `TestMain doesn't call os.Exit, hiding test failures`, + Text: `Test executables (and in turn 'go test') exit with a non-zero status +code if any tests failed. When specifying your own TestMain function, +it is your responsibility to arrange for this, by calling os.Exit with +the correct code. The correct code is returned by (*testing.M).Run, so +the usual way of implementing TestMain is to end it with +os.Exit(m.Run()).`, + Since: "2017.1", + }, + + "SA3001": &lint.Documentation{ + Title: `Assigning to b.N in benchmarks distorts the results`, + Text: `The testing package dynamically sets b.N to improve the reliability of +benchmarks and uses it in computations to determine the duration of a +single operation. Benchmark code must not alter b.N as this would +falsify results.`, + Since: "2017.1", + }, + + "SA4000": &lint.Documentation{ + Title: `Boolean expression has identical expressions on both sides`, + Since: "2017.1", + }, + + "SA4001": &lint.Documentation{ + Title: `&*x gets simplified to x, it does not copy x`, + Since: "2017.1", + }, + + "SA4002": &lint.Documentation{ + Title: `Comparing strings with known different sizes has predictable results`, + Since: "2017.1", + }, + + "SA4003": &lint.Documentation{ + Title: `Comparing unsigned values against negative values is pointless`, + Since: "2017.1", + }, + + "SA4004": &lint.Documentation{ + Title: `The loop exits unconditionally after one iteration`, + Since: "2017.1", + }, + + "SA4005": &lint.Documentation{ + Title: `Field assignment that will never be observed. Did you mean to use a pointer receiver?`, + Since: "2017.1", + }, + + "SA4006": &lint.Documentation{ + Title: `A value assigned to a variable is never read before being overwritten. Forgotten error check or dead code?`, + Since: "2017.1", + }, + + "SA4008": &lint.Documentation{ + Title: `The variable in the loop condition never changes, are you incrementing the wrong variable?`, + Since: "2017.1", + }, + + "SA4009": &lint.Documentation{ + Title: `A function argument is overwritten before its first use`, + Since: "2017.1", + }, + + "SA4010": &lint.Documentation{ + Title: `The result of append will never be observed anywhere`, + Since: "2017.1", + }, + + "SA4011": &lint.Documentation{ + Title: `Break statement with no effect. Did you mean to break out of an outer loop?`, + Since: "2017.1", + }, + + "SA4012": &lint.Documentation{ + Title: `Comparing a value against NaN even though no value is equal to NaN`, + Since: "2017.1", + }, + + "SA4013": &lint.Documentation{ + Title: `Negating a boolean twice (!!b) is the same as writing b. This is either redundant, or a typo.`, + Since: "2017.1", + }, + + "SA4014": &lint.Documentation{ + Title: `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`, + Since: "2017.1", + }, + + "SA4015": &lint.Documentation{ + Title: `Calling functions like math.Ceil on floats converted from integers doesn't do anything useful`, + Since: "2017.1", + }, + + "SA4016": &lint.Documentation{ + Title: `Certain bitwise operations, such as x ^ 0, do not do anything useful`, + Since: "2017.1", + }, + + "SA4017": &lint.Documentation{ + Title: `A pure function's return value is discarded, making the call pointless`, + Since: "2017.1", + }, + + "SA4018": &lint.Documentation{ + Title: `Self-assignment of variables`, + Since: "2017.1", + }, + + "SA4019": &lint.Documentation{ + Title: `Multiple, identical build constraints in the same file`, + Since: "2017.1", + }, + + "SA4020": &lint.Documentation{ + Title: `Unreachable case clause in a type switch`, + Text: `In a type switch like the following + + type T struct{} + func (T) Read(b []byte) (int, error) { return 0, nil } + + var v interface{} = T{} + + switch v.(type) { + case io.Reader: + // ... + case T: + // unreachable + } + +the second case clause can never be reached because T implements +io.Reader and case clauses are evaluated in source order. + +Another example: + + type T struct{} + func (T) Read(b []byte) (int, error) { return 0, nil } + func (T) Close() error { return nil } + + var v interface{} = T{} + + switch v.(type) { + case io.Reader: + // ... + case io.ReadCloser: + // unreachable + } + +Even though T has a Close method and thus implements io.ReadCloser, +io.Reader will always match first. The method set of io.Reader is a +subset of io.ReadCloser. Thus it is impossible to match the second +case without matching the first case. + + +Structurally equivalent interfaces + +A special case of the previous example are structurally identical +interfaces. Given these declarations + + type T error + type V error + + func doSomething() error { + err, ok := doAnotherThing() + if ok { + return T(err) + } + + return U(err) + } + +the following type switch will have an unreachable case clause: + + switch doSomething().(type) { + case T: + // ... + case V: + // unreachable + } + +T will always match before V because they are structurally equivalent +and therefore doSomething()'s return value implements both.`, + Since: "2019.2", + }, + + "SA4021": &lint.Documentation{ + Title: `x = append(y) is equivalent to x = y`, + Since: "2019.2", + }, + + "SA5000": &lint.Documentation{ + Title: `Assignment to nil map`, + Since: "2017.1", + }, + + "SA5001": &lint.Documentation{ + Title: `Defering Close before checking for a possible error`, + Since: "2017.1", + }, + + "SA5002": &lint.Documentation{ + Title: `The empty for loop (for {}) spins and can block the scheduler`, + Since: "2017.1", + }, + + "SA5003": &lint.Documentation{ + Title: `Defers in infinite loops will never execute`, + Text: `Defers are scoped to the surrounding function, not the surrounding +block. In a function that never returns, i.e. one containing an +infinite loop, defers will never execute.`, + Since: "2017.1", + }, + + "SA5004": &lint.Documentation{ + Title: `for { select { ... with an empty default branch spins`, + Since: "2017.1", + }, + + "SA5005": &lint.Documentation{ + Title: `The finalizer references the finalized object, preventing garbage collection`, + Text: `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.`, + Since: "2017.1", + }, + + "SA5006": &lint.Documentation{ + Title: `Slice index out of bounds`, + Since: "2017.1", + }, + + "SA5007": &lint.Documentation{ + Title: `Infinite recursive call`, + Text: `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 to add an +exit condition. It can also happen "on purpose". Some languages have +tail call optimization which makes certain infinite recursive calls +safe to use. Go, however, does not implement TCO, and as such a loop +should be used instead.`, + Since: "2017.1", + }, + + "SA5008": &lint.Documentation{ + Title: `Invalid struct tag`, + Since: "2019.2", + }, + + "SA5009": &lint.Documentation{ + Title: `Invalid Printf call`, + Since: "2019.2", + }, + + "SA6000": &lint.Documentation{ + Title: `Using regexp.Match or related in a loop, should use regexp.Compile`, + Since: "2017.1", + }, + + "SA6001": &lint.Documentation{ + Title: `Missing an optimization opportunity when indexing maps by byte slices`, + + Text: `Map keys must be comparable, which precludes the use of byte slices. +This usually leads to using string keys and converting byte slices to +strings. + +Normally, a conversion of a byte slice to a 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 in the Go repository.`, + Since: "2017.1", + }, + + "SA6002": &lint.Documentation{ + Title: `Storing non-pointer values in sync.Pool allocates memory`, + Text: `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 not a pointer 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.Pools, +and they're structs with 3 fields (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 https://go-review.googlesource.com/c/go/+/24371 +that discuss this problem.`, + Since: "2017.1", + }, + + "SA6003": &lint.Documentation{ + Title: `Converting a string to a slice of runes before ranging over it`, + Text: `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.`, + Since: "2017.1", + }, + + "SA6005": &lint.Documentation{ + Title: `Inefficient string comparison with strings.ToLower or strings.ToUpper`, + Text: `Converting two strings to the same case and comparing them like so + + if strings.ToLower(s1) == strings.ToLower(s2) { + ... + } + +is significantly more expensive than comparing them with +strings.EqualFold(s1, s2). This is due to memory usage as well as +computational complexity. + +strings.ToLower will have to allocate memory for the new strings, as +well as convert both strings fully, even if they differ on the very +first byte. strings.EqualFold, on the other hand, compares the strings +one character at a time. It doesn't need to create two intermediate +strings and can return as soon as the first non-matching character has +been found. + +For a more in-depth explanation of this issue, see +https://blog.digitalocean.com/how-to-efficiently-compare-strings-in-go/`, + Since: "2019.2", + }, + + "SA9001": &lint.Documentation{ + Title: `Defers in range loops may not run when you expect them to`, + Since: "2017.1", + }, + + "SA9002": &lint.Documentation{ + Title: `Using a non-octal os.FileMode that looks like it was meant to be in octal.`, + Since: "2017.1", + }, + + "SA9003": &lint.Documentation{ + Title: `Empty body in an if or else branch`, + Since: "2017.1", + }, + + "SA9004": &lint.Documentation{ + Title: `Only the first constant has an explicit type`, + + Text: `In a constant declaration such as the following: + + const ( + First byte = 1 + Second = 2 + ) + +the constant Second does not have the same type as the constant First. +This construct shouldn't be confused with + + const ( + First byte = iota + Second + ) + +where First and Second do indeed have the same type. The type is only +passed on when no explicit value is assigned to the constant. + +When declaring enumerations with explicit values it is therefore +important not to write + + const ( + EnumFirst EnumType = 1 + EnumSecond = 2 + EnumThird = 3 + ) + +This discrepancy in types can cause various confusing behaviors and +bugs. + + +Wrong type in variable declarations + +The most obvious issue with such incorrect enumerations expresses +itself as a compile error: + + package pkg + + const ( + EnumFirst uint8 = 1 + EnumSecond = 2 + ) + + func fn(useFirst bool) { + x := EnumSecond + if useFirst { + x = EnumFirst + } + } + +fails to compile with + + ./const.go:11:5: cannot use EnumFirst (type uint8) as type int in assignment + + +Losing method sets + +A more subtle issue occurs with types that have methods and optional +interfaces. Consider the following: + + package main + + import "fmt" + + type Enum int + + func (e Enum) String() string { + return "an enum" + } + + const ( + EnumFirst Enum = 1 + EnumSecond = 2 + ) + + func main() { + fmt.Println(EnumFirst) + fmt.Println(EnumSecond) + } + +This code will output + + an enum + 2 + +as EnumSecond has no explicit type, and thus defaults to int.`, + Since: "2019.1", + }, + + "SA9005": &lint.Documentation{ + Title: `Trying to marshal a struct with no public fields nor custom marshaling`, + Text: `The encoding/json and encoding/xml packages only operate on exported +fields in structs, not unexported ones. It is usually an error to try +to (un)marshal structs that only consist of unexported fields. + +This check will not flag calls involving types that define custom +marshaling behavior, e.g. via MarshalJSON methods. It will also not +flag empty structs.`, + Since: "2019.2", + }, +} diff --git a/vendor/honnef.co/go/tools/staticcheck/knowledge.go b/vendor/honnef.co/go/tools/staticcheck/knowledge.go new file mode 100644 index 000000000000..4c12b866a204 --- /dev/null +++ b/vendor/honnef.co/go/tools/staticcheck/knowledge.go @@ -0,0 +1,25 @@ +package staticcheck + +import ( + "reflect" + + "golang.org/x/tools/go/analysis" + "honnef.co/go/tools/internal/passes/buildssa" + "honnef.co/go/tools/ssa" + "honnef.co/go/tools/staticcheck/vrp" +) + +var valueRangesAnalyzer = &analysis.Analyzer{ + Name: "vrp", + Doc: "calculate value ranges of functions", + Run: func(pass *analysis.Pass) (interface{}, error) { + m := map[*ssa.Function]vrp.Ranges{} + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + vr := vrp.BuildGraph(ssafn).Solve() + m[ssafn] = vr + } + return m, nil + }, + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + ResultType: reflect.TypeOf(map[*ssa.Function]vrp.Ranges{}), +} diff --git a/vendor/honnef.co/go/tools/staticcheck/lint.go b/vendor/honnef.co/go/tools/staticcheck/lint.go new file mode 100644 index 000000000000..1558cbf94150 --- /dev/null +++ b/vendor/honnef.co/go/tools/staticcheck/lint.go @@ -0,0 +1,3360 @@ +// Package staticcheck contains a linter for Go source code. +package staticcheck // import "honnef.co/go/tools/staticcheck" + +import ( + "fmt" + "go/ast" + "go/constant" + "go/token" + "go/types" + htmltemplate "html/template" + "net/http" + "reflect" + "regexp" + "regexp/syntax" + "sort" + "strconv" + "strings" + texttemplate "text/template" + "unicode" + + . "honnef.co/go/tools/arg" + "honnef.co/go/tools/deprecated" + "honnef.co/go/tools/facts" + "honnef.co/go/tools/functions" + "honnef.co/go/tools/internal/passes/buildssa" + "honnef.co/go/tools/internal/sharedcheck" + "honnef.co/go/tools/lint" + . "honnef.co/go/tools/lint/lintdsl" + "honnef.co/go/tools/printf" + "honnef.co/go/tools/ssa" + "honnef.co/go/tools/ssautil" + "honnef.co/go/tools/staticcheck/vrp" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/inspect" + "golang.org/x/tools/go/ast/astutil" + "golang.org/x/tools/go/ast/inspector" + "golang.org/x/tools/go/types/typeutil" +) + +func validRegexp(call *Call) { + arg := call.Args[0] + err := ValidateRegexp(arg.Value) + if err != nil { + arg.Invalid(err.Error()) + } +} + +type runeSlice []rune + +func (rs runeSlice) Len() int { return len(rs) } +func (rs runeSlice) Less(i int, j int) bool { return rs[i] < rs[j] } +func (rs runeSlice) Swap(i int, j int) { rs[i], rs[j] = rs[j], rs[i] } + +func utf8Cutset(call *Call) { + arg := call.Args[1] + if InvalidUTF8(arg.Value) { + arg.Invalid(MsgInvalidUTF8) + } +} + +func uniqueCutset(call *Call) { + arg := call.Args[1] + if !UniqueStringCutset(arg.Value) { + arg.Invalid(MsgNonUniqueCutset) + } +} + +func unmarshalPointer(name string, arg int) CallCheck { + return func(call *Call) { + if !Pointer(call.Args[arg].Value) { + call.Args[arg].Invalid(fmt.Sprintf("%s expects to unmarshal into a pointer, but the provided value is not a pointer", name)) + } + } +} + +func pointlessIntMath(call *Call) { + if ConvertedFromInt(call.Args[0].Value) { + call.Invalid(fmt.Sprintf("calling %s on a converted integer is pointless", CallName(call.Instr.Common()))) + } +} + +func checkValidHostPort(arg int) CallCheck { + return func(call *Call) { + if !ValidHostPort(call.Args[arg].Value) { + call.Args[arg].Invalid(MsgInvalidHostPort) + } + } +} + +var ( + checkRegexpRules = map[string]CallCheck{ + "regexp.MustCompile": validRegexp, + "regexp.Compile": validRegexp, + "regexp.Match": validRegexp, + "regexp.MatchReader": validRegexp, + "regexp.MatchString": validRegexp, + } + + checkTimeParseRules = map[string]CallCheck{ + "time.Parse": func(call *Call) { + arg := call.Args[Arg("time.Parse.layout")] + err := ValidateTimeLayout(arg.Value) + if err != nil { + arg.Invalid(err.Error()) + } + }, + } + + checkEncodingBinaryRules = map[string]CallCheck{ + "encoding/binary.Write": func(call *Call) { + arg := call.Args[Arg("encoding/binary.Write.data")] + if !CanBinaryMarshal(call.Pass, arg.Value) { + arg.Invalid(fmt.Sprintf("value of type %s cannot be used with binary.Write", arg.Value.Value.Type())) + } + }, + } + + checkURLsRules = map[string]CallCheck{ + "net/url.Parse": func(call *Call) { + arg := call.Args[Arg("net/url.Parse.rawurl")] + err := ValidateURL(arg.Value) + if err != nil { + arg.Invalid(err.Error()) + } + }, + } + + checkSyncPoolValueRules = map[string]CallCheck{ + "(*sync.Pool).Put": func(call *Call) { + arg := call.Args[Arg("(*sync.Pool).Put.x")] + typ := arg.Value.Value.Type() + if !IsPointerLike(typ) { + arg.Invalid("argument should be pointer-like to avoid allocations") + } + }, + } + + checkRegexpFindAllRules = map[string]CallCheck{ + "(*regexp.Regexp).FindAll": RepeatZeroTimes("a FindAll method", 1), + "(*regexp.Regexp).FindAllIndex": RepeatZeroTimes("a FindAll method", 1), + "(*regexp.Regexp).FindAllString": RepeatZeroTimes("a FindAll method", 1), + "(*regexp.Regexp).FindAllStringIndex": RepeatZeroTimes("a FindAll method", 1), + "(*regexp.Regexp).FindAllStringSubmatch": RepeatZeroTimes("a FindAll method", 1), + "(*regexp.Regexp).FindAllStringSubmatchIndex": RepeatZeroTimes("a FindAll method", 1), + "(*regexp.Regexp).FindAllSubmatch": RepeatZeroTimes("a FindAll method", 1), + "(*regexp.Regexp).FindAllSubmatchIndex": RepeatZeroTimes("a FindAll method", 1), + } + + checkUTF8CutsetRules = map[string]CallCheck{ + "strings.IndexAny": utf8Cutset, + "strings.LastIndexAny": utf8Cutset, + "strings.ContainsAny": utf8Cutset, + "strings.Trim": utf8Cutset, + "strings.TrimLeft": utf8Cutset, + "strings.TrimRight": utf8Cutset, + } + + checkUniqueCutsetRules = map[string]CallCheck{ + "strings.Trim": uniqueCutset, + "strings.TrimLeft": uniqueCutset, + "strings.TrimRight": uniqueCutset, + } + + checkUnmarshalPointerRules = map[string]CallCheck{ + "encoding/xml.Unmarshal": unmarshalPointer("xml.Unmarshal", 1), + "(*encoding/xml.Decoder).Decode": unmarshalPointer("Decode", 0), + "(*encoding/xml.Decoder).DecodeElement": unmarshalPointer("DecodeElement", 0), + "encoding/json.Unmarshal": unmarshalPointer("json.Unmarshal", 1), + "(*encoding/json.Decoder).Decode": unmarshalPointer("Decode", 0), + } + + checkUnbufferedSignalChanRules = map[string]CallCheck{ + "os/signal.Notify": func(call *Call) { + arg := call.Args[Arg("os/signal.Notify.c")] + if UnbufferedChannel(arg.Value) { + arg.Invalid("the channel used with signal.Notify should be buffered") + } + }, + } + + checkMathIntRules = map[string]CallCheck{ + "math.Ceil": pointlessIntMath, + "math.Floor": pointlessIntMath, + "math.IsNaN": pointlessIntMath, + "math.Trunc": pointlessIntMath, + "math.IsInf": pointlessIntMath, + } + + checkStringsReplaceZeroRules = map[string]CallCheck{ + "strings.Replace": RepeatZeroTimes("strings.Replace", 3), + "bytes.Replace": RepeatZeroTimes("bytes.Replace", 3), + } + + checkListenAddressRules = map[string]CallCheck{ + "net/http.ListenAndServe": checkValidHostPort(0), + "net/http.ListenAndServeTLS": checkValidHostPort(0), + } + + checkBytesEqualIPRules = map[string]CallCheck{ + "bytes.Equal": func(call *Call) { + if ConvertedFrom(call.Args[Arg("bytes.Equal.a")].Value, "net.IP") && + ConvertedFrom(call.Args[Arg("bytes.Equal.b")].Value, "net.IP") { + call.Invalid("use net.IP.Equal to compare net.IPs, not bytes.Equal") + } + }, + } + + checkRegexpMatchLoopRules = map[string]CallCheck{ + "regexp.Match": loopedRegexp("regexp.Match"), + "regexp.MatchReader": loopedRegexp("regexp.MatchReader"), + "regexp.MatchString": loopedRegexp("regexp.MatchString"), + } + + checkNoopMarshal = map[string]CallCheck{ + // TODO(dh): should we really flag XML? Even an empty struct + // produces a non-zero amount of data, namely its type name. + // Let's see if we encounter any false positives. + // + // Also, should we flag gob? + "encoding/json.Marshal": checkNoopMarshalImpl(Arg("json.Marshal.v"), "MarshalJSON", "MarshalText"), + "encoding/xml.Marshal": checkNoopMarshalImpl(Arg("xml.Marshal.v"), "MarshalXML", "MarshalText"), + "(*encoding/json.Encoder).Encode": checkNoopMarshalImpl(Arg("(*encoding/json.Encoder).Encode.v"), "MarshalJSON", "MarshalText"), + "(*encoding/xml.Encoder).Encode": checkNoopMarshalImpl(Arg("(*encoding/xml.Encoder).Encode.v"), "MarshalXML", "MarshalText"), + + "encoding/json.Unmarshal": checkNoopMarshalImpl(Arg("json.Unmarshal.v"), "UnmarshalJSON", "UnmarshalText"), + "encoding/xml.Unmarshal": checkNoopMarshalImpl(Arg("xml.Unmarshal.v"), "UnmarshalXML", "UnmarshalText"), + "(*encoding/json.Decoder).Decode": checkNoopMarshalImpl(Arg("(*encoding/json.Decoder).Decode.v"), "UnmarshalJSON", "UnmarshalText"), + "(*encoding/xml.Decoder).Decode": checkNoopMarshalImpl(Arg("(*encoding/xml.Decoder).Decode.v"), "UnmarshalXML", "UnmarshalText"), + } + + checkUnsupportedMarshal = map[string]CallCheck{ + "encoding/json.Marshal": checkUnsupportedMarshalImpl(Arg("json.Marshal.v"), "json", "MarshalJSON", "MarshalText"), + "encoding/xml.Marshal": checkUnsupportedMarshalImpl(Arg("xml.Marshal.v"), "xml", "MarshalXML", "MarshalText"), + "(*encoding/json.Encoder).Encode": checkUnsupportedMarshalImpl(Arg("(*encoding/json.Encoder).Encode.v"), "json", "MarshalJSON", "MarshalText"), + "(*encoding/xml.Encoder).Encode": checkUnsupportedMarshalImpl(Arg("(*encoding/xml.Encoder).Encode.v"), "xml", "MarshalXML", "MarshalText"), + } + + checkAtomicAlignment = map[string]CallCheck{ + "sync/atomic.AddInt64": checkAtomicAlignmentImpl, + "sync/atomic.AddUint64": checkAtomicAlignmentImpl, + "sync/atomic.CompareAndSwapInt64": checkAtomicAlignmentImpl, + "sync/atomic.CompareAndSwapUint64": checkAtomicAlignmentImpl, + "sync/atomic.LoadInt64": checkAtomicAlignmentImpl, + "sync/atomic.LoadUint64": checkAtomicAlignmentImpl, + "sync/atomic.StoreInt64": checkAtomicAlignmentImpl, + "sync/atomic.StoreUint64": checkAtomicAlignmentImpl, + "sync/atomic.SwapInt64": checkAtomicAlignmentImpl, + "sync/atomic.SwapUint64": checkAtomicAlignmentImpl, + } + + // TODO(dh): detect printf wrappers + checkPrintfRules = map[string]CallCheck{ + "fmt.Errorf": func(call *Call) { checkPrintfCall(call, 0, 1) }, + "fmt.Printf": func(call *Call) { checkPrintfCall(call, 0, 1) }, + "fmt.Sprintf": func(call *Call) { checkPrintfCall(call, 0, 1) }, + "fmt.Fprintf": func(call *Call) { checkPrintfCall(call, 1, 2) }, + } +) + +func checkPrintfCall(call *Call, fIdx, vIdx int) { + f := call.Args[fIdx] + var args []ssa.Value + switch v := call.Args[vIdx].Value.Value.(type) { + case *ssa.Slice: + var ok bool + args, ok = ssautil.Vararg(v) + if !ok { + // We don't know what the actual arguments to the function are + return + } + case *ssa.Const: + // nil, i.e. no arguments + default: + // We don't know what the actual arguments to the function are + return + } + checkPrintfCallImpl(call, f.Value.Value, args) +} + +type verbFlag int + +const ( + isInt verbFlag = 1 << iota + isBool + isFP + isString + isPointer + isPseudoPointer + isSlice + isAny + noRecurse +) + +var verbs = [...]verbFlag{ + 'b': isPseudoPointer | isInt | isFP, + 'c': isInt, + 'd': isPseudoPointer | isInt, + 'e': isFP, + 'E': isFP, + 'f': isFP, + 'F': isFP, + 'g': isFP, + 'G': isFP, + 'o': isPseudoPointer | isInt, + 'p': isSlice | isPointer | noRecurse, + 'q': isInt | isString, + 's': isString, + 't': isBool, + 'T': isAny, + 'U': isInt, + 'v': isAny, + 'X': isPseudoPointer | isInt | isString, + 'x': isPseudoPointer | isInt | isString, +} + +func checkPrintfCallImpl(call *Call, f ssa.Value, args []ssa.Value) { + var msCache *typeutil.MethodSetCache + if f.Parent() != nil { + msCache = &f.Parent().Prog.MethodSets + } + + elem := func(T types.Type, verb rune) ([]types.Type, bool) { + if verbs[verb]&noRecurse != 0 { + return []types.Type{T}, false + } + switch T := T.(type) { + case *types.Slice: + if verbs[verb]&isSlice != 0 { + return []types.Type{T}, false + } + if verbs[verb]&isString != 0 && IsType(T.Elem().Underlying(), "byte") { + return []types.Type{T}, false + } + return []types.Type{T.Elem()}, true + case *types.Map: + key := T.Key() + val := T.Elem() + return []types.Type{key, val}, true + case *types.Struct: + out := make([]types.Type, 0, T.NumFields()) + for i := 0; i < T.NumFields(); i++ { + out = append(out, T.Field(i).Type()) + } + return out, true + case *types.Array: + return []types.Type{T.Elem()}, true + default: + return []types.Type{T}, false + } + } + isInfo := func(T types.Type, info types.BasicInfo) bool { + basic, ok := T.Underlying().(*types.Basic) + return ok && basic.Info()&info != 0 + } + + isStringer := func(T types.Type, ms *types.MethodSet) bool { + sel := ms.Lookup(nil, "String") + if sel == nil { + return false + } + fn, ok := sel.Obj().(*types.Func) + if !ok { + // should be unreachable + return false + } + sig := fn.Type().(*types.Signature) + if sig.Params().Len() != 0 { + return false + } + if sig.Results().Len() != 1 { + return false + } + if !IsType(sig.Results().At(0).Type(), "string") { + return false + } + return true + } + isError := func(T types.Type, ms *types.MethodSet) bool { + sel := ms.Lookup(nil, "Error") + if sel == nil { + return false + } + fn, ok := sel.Obj().(*types.Func) + if !ok { + // should be unreachable + return false + } + sig := fn.Type().(*types.Signature) + if sig.Params().Len() != 0 { + return false + } + if sig.Results().Len() != 1 { + return false + } + if !IsType(sig.Results().At(0).Type(), "string") { + return false + } + return true + } + + isFormatter := func(T types.Type, ms *types.MethodSet) bool { + sel := ms.Lookup(nil, "Format") + if sel == nil { + return false + } + fn, ok := sel.Obj().(*types.Func) + if !ok { + // should be unreachable + return false + } + sig := fn.Type().(*types.Signature) + if sig.Params().Len() != 2 { + return false + } + // TODO(dh): check the types of the arguments for more + // precision + if sig.Results().Len() != 0 { + return false + } + return true + } + + seen := map[types.Type]bool{} + var checkType func(verb rune, T types.Type, top bool) bool + checkType = func(verb rune, T types.Type, top bool) bool { + if top { + for k := range seen { + delete(seen, k) + } + } + if seen[T] { + return true + } + seen[T] = true + if int(verb) >= len(verbs) { + // Unknown verb + return true + } + + flags := verbs[verb] + if flags == 0 { + // Unknown verb + return true + } + + ms := msCache.MethodSet(T) + if isFormatter(T, ms) { + // the value is responsible for formatting itself + return true + } + + if flags&isString != 0 && (isStringer(T, ms) || isError(T, ms)) { + // Check for stringer early because we're about to dereference + return true + } + + T = T.Underlying() + if flags&(isPointer|isPseudoPointer) == 0 && top { + T = Dereference(T) + } + if flags&isPseudoPointer != 0 && top { + t := Dereference(T) + if _, ok := t.Underlying().(*types.Struct); ok { + T = t + } + } + + if _, ok := T.(*types.Interface); ok { + // We don't know what's in the interface + return true + } + + var info types.BasicInfo + if flags&isInt != 0 { + info |= types.IsInteger + } + if flags&isBool != 0 { + info |= types.IsBoolean + } + if flags&isFP != 0 { + info |= types.IsFloat | types.IsComplex + } + if flags&isString != 0 { + info |= types.IsString + } + + if info != 0 && isInfo(T, info) { + return true + } + + if flags&isString != 0 && (IsType(T, "[]byte") || isStringer(T, ms) || isError(T, ms)) { + return true + } + + if flags&isPointer != 0 && IsPointerLike(T) { + return true + } + if flags&isPseudoPointer != 0 { + switch U := T.Underlying().(type) { + case *types.Pointer: + if !top { + return true + } + + if _, ok := U.Elem().Underlying().(*types.Struct); !ok { + return true + } + case *types.Chan, *types.Signature: + return true + } + } + + if flags&isSlice != 0 { + if _, ok := T.(*types.Slice); ok { + return true + } + } + + if flags&isAny != 0 { + return true + } + + elems, ok := elem(T.Underlying(), verb) + if !ok { + return false + } + for _, elem := range elems { + if !checkType(verb, elem, false) { + return false + } + } + + return true + } + + k, ok := f.(*ssa.Const) + if !ok { + return + } + actions, err := printf.Parse(constant.StringVal(k.Value)) + if err != nil { + call.Invalid("couldn't parse format string") + return + } + + ptr := 1 + hasExplicit := false + + checkStar := func(verb printf.Verb, star printf.Argument) bool { + if star, ok := star.(printf.Star); ok { + idx := 0 + if star.Index == -1 { + idx = ptr + ptr++ + } else { + hasExplicit = true + idx = star.Index + ptr = star.Index + 1 + } + if idx == 0 { + call.Invalid(fmt.Sprintf("Printf format %s reads invalid arg 0; indices are 1-based", verb.Raw)) + return false + } + if idx > len(args) { + call.Invalid( + fmt.Sprintf("Printf format %s reads arg #%d, but call has only %d args", + verb.Raw, idx, len(args))) + return false + } + if arg, ok := args[idx-1].(*ssa.MakeInterface); ok { + if !isInfo(arg.X.Type(), types.IsInteger) { + call.Invalid(fmt.Sprintf("Printf format %s reads non-int arg #%d as argument of *", verb.Raw, idx)) + } + } + } + return true + } + + // We only report one problem per format string. Making a + // mistake with an index tends to invalidate all future + // implicit indices. + for _, action := range actions { + verb, ok := action.(printf.Verb) + if !ok { + continue + } + + if !checkStar(verb, verb.Width) || !checkStar(verb, verb.Precision) { + return + } + + off := ptr + if verb.Value != -1 { + hasExplicit = true + off = verb.Value + } + if off > len(args) { + call.Invalid( + fmt.Sprintf("Printf format %s reads arg #%d, but call has only %d args", + verb.Raw, off, len(args))) + return + } else if verb.Value == 0 && verb.Letter != '%' { + call.Invalid(fmt.Sprintf("Printf format %s reads invalid arg 0; indices are 1-based", verb.Raw)) + return + } else if off != 0 { + arg, ok := args[off-1].(*ssa.MakeInterface) + if ok { + if !checkType(verb.Letter, arg.X.Type(), true) { + call.Invalid(fmt.Sprintf("Printf format %s has arg #%d of wrong type %s", + verb.Raw, ptr, args[ptr-1].(*ssa.MakeInterface).X.Type())) + return + } + } + } + + switch verb.Value { + case -1: + // Consume next argument + ptr++ + case 0: + // Don't consume any arguments + default: + ptr = verb.Value + 1 + } + } + + if !hasExplicit && ptr <= len(args) { + call.Invalid(fmt.Sprintf("Printf call needs %d args but has %d args", ptr-1, len(args))) + } +} + +func checkAtomicAlignmentImpl(call *Call) { + sizes := call.Pass.TypesSizes + if sizes.Sizeof(types.Typ[types.Uintptr]) != 4 { + // Not running on a 32-bit platform + return + } + v, ok := call.Args[0].Value.Value.(*ssa.FieldAddr) + if !ok { + // TODO(dh): also check indexing into arrays and slices + return + } + T := v.X.Type().Underlying().(*types.Pointer).Elem().Underlying().(*types.Struct) + fields := make([]*types.Var, 0, T.NumFields()) + for i := 0; i < T.NumFields() && i <= v.Field; i++ { + fields = append(fields, T.Field(i)) + } + + off := sizes.Offsetsof(fields)[v.Field] + if off%8 != 0 { + msg := fmt.Sprintf("address of non 64-bit aligned field %s passed to %s", + T.Field(v.Field).Name(), + CallName(call.Instr.Common())) + call.Invalid(msg) + } +} + +func checkNoopMarshalImpl(argN int, meths ...string) CallCheck { + return func(call *Call) { + if IsGenerated(call.Pass, call.Instr.Pos()) { + return + } + arg := call.Args[argN] + T := arg.Value.Value.Type() + Ts, ok := Dereference(T).Underlying().(*types.Struct) + if !ok { + return + } + if Ts.NumFields() == 0 { + return + } + fields := FlattenFields(Ts) + for _, field := range fields { + if field.Var.Exported() { + return + } + } + // OPT(dh): we could use a method set cache here + ms := call.Instr.Parent().Prog.MethodSets.MethodSet(T) + // TODO(dh): we're not checking the signature, which can cause false negatives. + // This isn't a huge problem, however, since vet complains about incorrect signatures. + for _, meth := range meths { + if ms.Lookup(nil, meth) != nil { + return + } + } + arg.Invalid("struct doesn't have any exported fields, nor custom marshaling") + } +} + +func checkUnsupportedMarshalImpl(argN int, tag string, meths ...string) CallCheck { + // TODO(dh): flag slices and maps of unsupported types + return func(call *Call) { + msCache := &call.Instr.Parent().Prog.MethodSets + + arg := call.Args[argN] + T := arg.Value.Value.Type() + Ts, ok := Dereference(T).Underlying().(*types.Struct) + if !ok { + return + } + ms := msCache.MethodSet(T) + // TODO(dh): we're not checking the signature, which can cause false negatives. + // This isn't a huge problem, however, since vet complains about incorrect signatures. + for _, meth := range meths { + if ms.Lookup(nil, meth) != nil { + return + } + } + fields := FlattenFields(Ts) + for _, field := range fields { + if !(field.Var.Exported()) { + continue + } + if reflect.StructTag(field.Tag).Get(tag) == "-" { + continue + } + ms := msCache.MethodSet(field.Var.Type()) + // TODO(dh): we're not checking the signature, which can cause false negatives. + // This isn't a huge problem, however, since vet complains about incorrect signatures. + for _, meth := range meths { + if ms.Lookup(nil, meth) != nil { + return + } + } + switch field.Var.Type().Underlying().(type) { + case *types.Chan, *types.Signature: + arg.Invalid(fmt.Sprintf("trying to marshal chan or func value, field %s", fieldPath(T, field.Path))) + } + } + } +} + +func fieldPath(start types.Type, indices []int) string { + p := start.String() + for _, idx := range indices { + field := Dereference(start).Underlying().(*types.Struct).Field(idx) + start = field.Type() + p += "." + field.Name() + } + return p +} + +func isInLoop(b *ssa.BasicBlock) bool { + sets := functions.FindLoops(b.Parent()) + for _, set := range sets { + if set.Has(b) { + return true + } + } + return false +} + +func CheckUntrappableSignal(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + call := node.(*ast.CallExpr) + if !IsCallToAnyAST(pass, call, + "os/signal.Ignore", "os/signal.Notify", "os/signal.Reset") { + return + } + for _, arg := range call.Args { + if conv, ok := arg.(*ast.CallExpr); ok && isName(pass, conv.Fun, "os.Signal") { + arg = conv.Args[0] + } + + if isName(pass, arg, "os.Kill") || isName(pass, arg, "syscall.SIGKILL") { + ReportNodef(pass, arg, "%s cannot be trapped (did you mean syscall.SIGTERM?)", Render(pass, arg)) + } + if isName(pass, arg, "syscall.SIGSTOP") { + ReportNodef(pass, arg, "%s signal cannot be trapped", Render(pass, arg)) + } + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn) + return nil, nil +} + +func CheckTemplate(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + call := node.(*ast.CallExpr) + var kind string + if IsCallToAST(pass, call, "(*text/template.Template).Parse") { + kind = "text" + } else if IsCallToAST(pass, call, "(*html/template.Template).Parse") { + kind = "html" + } else { + return + } + sel := call.Fun.(*ast.SelectorExpr) + if !IsCallToAST(pass, sel.X, "text/template.New") && + !IsCallToAST(pass, sel.X, "html/template.New") { + // TODO(dh): this is a cheap workaround for templates with + // different delims. A better solution with less false + // negatives would use data flow analysis to see where the + // template comes from and where it has been + return + } + s, ok := ExprToString(pass, call.Args[Arg("(*text/template.Template).Parse.text")]) + if !ok { + return + } + var err error + switch kind { + case "text": + _, err = texttemplate.New("").Parse(s) + case "html": + _, err = htmltemplate.New("").Parse(s) + } + if err != nil { + // TODO(dominikh): whitelist other parse errors, if any + if strings.Contains(err.Error(), "unexpected") { + ReportNodef(pass, call.Args[Arg("(*text/template.Template).Parse.text")], "%s", err) + } + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn) + return nil, nil +} + +func CheckTimeSleepConstant(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + call := node.(*ast.CallExpr) + if !IsCallToAST(pass, call, "time.Sleep") { + return + } + lit, ok := call.Args[Arg("time.Sleep.d")].(*ast.BasicLit) + if !ok { + return + } + n, err := strconv.Atoi(lit.Value) + if err != nil { + return + } + if n == 0 || n > 120 { + // time.Sleep(0) is a seldom used pattern in concurrency + // tests. >120 might be intentional. 120 was chosen + // because the user could've meant 2 minutes. + return + } + recommendation := "time.Sleep(time.Nanosecond)" + if n != 1 { + recommendation = fmt.Sprintf("time.Sleep(%d * time.Nanosecond)", n) + } + ReportNodef(pass, call.Args[Arg("time.Sleep.d")], + "sleeping for %d nanoseconds is probably a bug. Be explicit if it isn't: %s", n, recommendation) + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn) + return nil, nil +} + +func CheckWaitgroupAdd(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + g := node.(*ast.GoStmt) + fun, ok := g.Call.Fun.(*ast.FuncLit) + if !ok { + return + } + if len(fun.Body.List) == 0 { + return + } + stmt, ok := fun.Body.List[0].(*ast.ExprStmt) + if !ok { + return + } + if IsCallToAST(pass, stmt.X, "(*sync.WaitGroup).Add") { + ReportNodef(pass, stmt, "should call %s before starting the goroutine to avoid a race", + Render(pass, stmt)) + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.GoStmt)(nil)}, fn) + return nil, nil +} + +func CheckInfiniteEmptyLoop(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + loop := node.(*ast.ForStmt) + if len(loop.Body.List) != 0 || loop.Post != nil { + return + } + + if loop.Init != nil { + // TODO(dh): this isn't strictly necessary, it just makes + // the check easier. + return + } + // An empty loop is bad news in two cases: 1) The loop has no + // condition. In that case, it's just a loop that spins + // forever and as fast as it can, keeping a core busy. 2) The + // loop condition only consists of variable or field reads and + // operators on those. The only way those could change their + // value is with unsynchronised access, which constitutes a + // data race. + // + // If the condition contains any function calls, its behaviour + // is dynamic and the loop might terminate. Similarly for + // channel receives. + + if loop.Cond != nil { + if hasSideEffects(loop.Cond) { + return + } + if ident, ok := loop.Cond.(*ast.Ident); ok { + if k, ok := pass.TypesInfo.ObjectOf(ident).(*types.Const); ok { + if !constant.BoolVal(k.Val()) { + // don't flag `for false {}` loops. They're a debug aid. + return + } + } + } + ReportNodef(pass, loop, "loop condition never changes or has a race condition") + } + ReportNodef(pass, loop, "this loop will spin, using 100%% CPU") + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.ForStmt)(nil)}, fn) + return nil, nil +} + +func CheckDeferInInfiniteLoop(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + mightExit := false + var defers []ast.Stmt + loop := node.(*ast.ForStmt) + if loop.Cond != nil { + return + } + fn2 := func(node ast.Node) bool { + switch stmt := node.(type) { + case *ast.ReturnStmt: + mightExit = true + return false + case *ast.BranchStmt: + // TODO(dominikh): if this sees a break in a switch or + // select, it doesn't check if it breaks the loop or + // just the select/switch. This causes some false + // negatives. + if stmt.Tok == token.BREAK { + mightExit = true + return false + } + case *ast.DeferStmt: + defers = append(defers, stmt) + case *ast.FuncLit: + // Don't look into function bodies + return false + } + return true + } + ast.Inspect(loop.Body, fn2) + if mightExit { + return + } + for _, stmt := range defers { + ReportNodef(pass, stmt, "defers in this infinite loop will never run") + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.ForStmt)(nil)}, fn) + return nil, nil +} + +func CheckDubiousDeferInChannelRangeLoop(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + loop := node.(*ast.RangeStmt) + typ := pass.TypesInfo.TypeOf(loop.X) + _, ok := typ.Underlying().(*types.Chan) + if !ok { + return + } + fn2 := func(node ast.Node) bool { + switch stmt := node.(type) { + case *ast.DeferStmt: + ReportNodef(pass, stmt, "defers in this range loop won't run unless the channel gets closed") + case *ast.FuncLit: + // Don't look into function bodies + return false + } + return true + } + ast.Inspect(loop.Body, fn2) + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.RangeStmt)(nil)}, fn) + return nil, nil +} + +func CheckTestMainExit(pass *analysis.Pass) (interface{}, error) { + var ( + fnmain ast.Node + callsExit bool + callsRun bool + arg types.Object + ) + fn := func(node ast.Node, push bool) bool { + if !push { + if fnmain != nil && node == fnmain { + if !callsExit && callsRun { + ReportNodef(pass, fnmain, "TestMain should call os.Exit to set exit code") + } + fnmain = nil + callsExit = false + callsRun = false + arg = nil + } + return true + } + + switch node := node.(type) { + case *ast.FuncDecl: + if fnmain != nil { + return true + } + if !isTestMain(pass, node) { + return false + } + fnmain = node + arg = pass.TypesInfo.ObjectOf(node.Type.Params.List[0].Names[0]) + return true + case *ast.CallExpr: + if IsCallToAST(pass, node, "os.Exit") { + callsExit = true + return false + } + sel, ok := node.Fun.(*ast.SelectorExpr) + if !ok { + return true + } + ident, ok := sel.X.(*ast.Ident) + if !ok { + return true + } + if arg != pass.TypesInfo.ObjectOf(ident) { + return true + } + if sel.Sel.Name == "Run" { + callsRun = true + return false + } + return true + default: + // unreachable + return true + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Nodes([]ast.Node{(*ast.FuncDecl)(nil), (*ast.CallExpr)(nil)}, fn) + return nil, nil +} + +func isTestMain(pass *analysis.Pass, decl *ast.FuncDecl) bool { + if decl.Name.Name != "TestMain" { + return false + } + if len(decl.Type.Params.List) != 1 { + return false + } + arg := decl.Type.Params.List[0] + if len(arg.Names) != 1 { + return false + } + return IsOfType(pass, arg.Type, "*testing.M") +} + +func CheckExec(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + call := node.(*ast.CallExpr) + if !IsCallToAST(pass, call, "os/exec.Command") { + return + } + val, ok := ExprToString(pass, call.Args[Arg("os/exec.Command.name")]) + if !ok { + return + } + if !strings.Contains(val, " ") || strings.Contains(val, `\`) || strings.Contains(val, "/") { + return + } + ReportNodef(pass, call.Args[Arg("os/exec.Command.name")], + "first argument to exec.Command looks like a shell command, but a program name or path are expected") + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn) + return nil, nil +} + +func CheckLoopEmptyDefault(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + loop := node.(*ast.ForStmt) + if len(loop.Body.List) != 1 || loop.Cond != nil || loop.Init != nil { + return + } + sel, ok := loop.Body.List[0].(*ast.SelectStmt) + if !ok { + return + } + for _, c := range sel.Body.List { + if comm, ok := c.(*ast.CommClause); ok && comm.Comm == nil && len(comm.Body) == 0 { + ReportNodef(pass, comm, "should not have an empty default case in a for+select loop. The loop will spin.") + } + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.ForStmt)(nil)}, fn) + return nil, nil +} + +func CheckLhsRhsIdentical(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + op := node.(*ast.BinaryExpr) + switch op.Op { + case token.EQL, token.NEQ: + if basic, ok := pass.TypesInfo.TypeOf(op.X).Underlying().(*types.Basic); ok { + if kind := basic.Kind(); kind == types.Float32 || kind == types.Float64 { + // f == f and f != f might be used to check for NaN + return + } + } + case token.SUB, token.QUO, token.AND, token.REM, token.OR, token.XOR, token.AND_NOT, + token.LAND, token.LOR, token.LSS, token.GTR, token.LEQ, token.GEQ: + default: + // For some ops, such as + and *, it can make sense to + // have identical operands + return + } + + if Render(pass, op.X) != Render(pass, op.Y) { + return + } + l1, ok1 := op.X.(*ast.BasicLit) + l2, ok2 := op.Y.(*ast.BasicLit) + if ok1 && ok2 && l1.Kind == token.INT && l2.Kind == l1.Kind && l1.Value == "0" && l2.Value == l1.Value && IsGenerated(pass, l1.Pos()) { + // cgo generates the following function call: + // _cgoCheckPointer(_cgoBase0, 0 == 0) – it uses 0 == 0 + // instead of true in case the user shadowed the + // identifier. Ideally we'd restrict this exception to + // calls of _cgoCheckPointer, but it's not worth the + // hassle of keeping track of the stack. + // are very rare to begin with, and we're mostly checking + // for them to catch typos such as 1 == 1 where the user + // meant to type i == 1. The odds of a false negative for + // 0 == 0 are slim. + return + } + ReportNodef(pass, op, "identical expressions on the left and right side of the '%s' operator", op.Op) + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.BinaryExpr)(nil)}, fn) + return nil, nil +} + +func CheckScopedBreak(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + var body *ast.BlockStmt + switch node := node.(type) { + case *ast.ForStmt: + body = node.Body + case *ast.RangeStmt: + body = node.Body + default: + panic(fmt.Sprintf("unreachable: %T", node)) + } + for _, stmt := range body.List { + var blocks [][]ast.Stmt + switch stmt := stmt.(type) { + case *ast.SwitchStmt: + for _, c := range stmt.Body.List { + blocks = append(blocks, c.(*ast.CaseClause).Body) + } + case *ast.SelectStmt: + for _, c := range stmt.Body.List { + blocks = append(blocks, c.(*ast.CommClause).Body) + } + default: + continue + } + + for _, body := range blocks { + if len(body) == 0 { + continue + } + lasts := []ast.Stmt{body[len(body)-1]} + // TODO(dh): unfold all levels of nested block + // statements, not just a single level if statement + if ifs, ok := lasts[0].(*ast.IfStmt); ok { + if len(ifs.Body.List) == 0 { + continue + } + lasts[0] = ifs.Body.List[len(ifs.Body.List)-1] + + if block, ok := ifs.Else.(*ast.BlockStmt); ok { + if len(block.List) != 0 { + lasts = append(lasts, block.List[len(block.List)-1]) + } + } + } + for _, last := range lasts { + branch, ok := last.(*ast.BranchStmt) + if !ok || branch.Tok != token.BREAK || branch.Label != nil { + continue + } + ReportNodef(pass, branch, "ineffective break statement. Did you mean to break out of the outer loop?") + } + } + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.ForStmt)(nil), (*ast.RangeStmt)(nil)}, fn) + return nil, nil +} + +func CheckUnsafePrintf(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + call := node.(*ast.CallExpr) + var arg int + if IsCallToAnyAST(pass, call, "fmt.Printf", "fmt.Sprintf", "log.Printf") { + arg = Arg("fmt.Printf.format") + } else if IsCallToAnyAST(pass, call, "fmt.Fprintf") { + arg = Arg("fmt.Fprintf.format") + } else { + return + } + if len(call.Args) != arg+1 { + return + } + switch call.Args[arg].(type) { + case *ast.CallExpr, *ast.Ident: + default: + return + } + ReportNodef(pass, call.Args[arg], + "printf-style function with dynamic format string and no further arguments should use print-style function instead") + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn) + return nil, nil +} + +func CheckEarlyDefer(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + block := node.(*ast.BlockStmt) + if len(block.List) < 2 { + return + } + for i, stmt := range block.List { + if i == len(block.List)-1 { + break + } + assign, ok := stmt.(*ast.AssignStmt) + if !ok { + continue + } + if len(assign.Rhs) != 1 { + continue + } + if len(assign.Lhs) < 2 { + continue + } + if lhs, ok := assign.Lhs[len(assign.Lhs)-1].(*ast.Ident); ok && lhs.Name == "_" { + continue + } + call, ok := assign.Rhs[0].(*ast.CallExpr) + if !ok { + continue + } + sig, ok := pass.TypesInfo.TypeOf(call.Fun).(*types.Signature) + if !ok { + continue + } + if sig.Results().Len() < 2 { + continue + } + last := sig.Results().At(sig.Results().Len() - 1) + // FIXME(dh): check that it's error from universe, not + // another type of the same name + if last.Type().String() != "error" { + continue + } + lhs, ok := assign.Lhs[0].(*ast.Ident) + if !ok { + continue + } + def, ok := block.List[i+1].(*ast.DeferStmt) + if !ok { + continue + } + sel, ok := def.Call.Fun.(*ast.SelectorExpr) + if !ok { + continue + } + ident, ok := selectorX(sel).(*ast.Ident) + if !ok { + continue + } + if ident.Obj != lhs.Obj { + continue + } + if sel.Sel.Name != "Close" { + continue + } + ReportNodef(pass, def, "should check returned error before deferring %s", Render(pass, def.Call)) + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.BlockStmt)(nil)}, fn) + return nil, nil +} + +func selectorX(sel *ast.SelectorExpr) ast.Node { + switch x := sel.X.(type) { + case *ast.SelectorExpr: + return selectorX(x) + default: + return x + } +} + +func CheckEmptyCriticalSection(pass *analysis.Pass) (interface{}, error) { + // Initially it might seem like this check would be easier to + // implement in SSA. After all, we're only checking for two + // consecutive method calls. In reality, however, there may be any + // number of other instructions between the lock and unlock, while + // still constituting an empty critical section. For example, + // given `m.x().Lock(); m.x().Unlock()`, there will be a call to + // x(). In the AST-based approach, this has a tiny potential for a + // false positive (the second call to x might be doing work that + // is protected by the mutex). In an SSA-based approach, however, + // it would miss a lot of real bugs. + + mutexParams := func(s ast.Stmt) (x ast.Expr, funcName string, ok bool) { + expr, ok := s.(*ast.ExprStmt) + if !ok { + return nil, "", false + } + call, ok := expr.X.(*ast.CallExpr) + if !ok { + return nil, "", false + } + sel, ok := call.Fun.(*ast.SelectorExpr) + if !ok { + return nil, "", false + } + + fn, ok := pass.TypesInfo.ObjectOf(sel.Sel).(*types.Func) + if !ok { + return nil, "", false + } + sig := fn.Type().(*types.Signature) + if sig.Params().Len() != 0 || sig.Results().Len() != 0 { + return nil, "", false + } + + return sel.X, fn.Name(), true + } + + fn := func(node ast.Node) { + block := node.(*ast.BlockStmt) + if len(block.List) < 2 { + return + } + for i := range block.List[:len(block.List)-1] { + sel1, method1, ok1 := mutexParams(block.List[i]) + sel2, method2, ok2 := mutexParams(block.List[i+1]) + + if !ok1 || !ok2 || Render(pass, sel1) != Render(pass, sel2) { + continue + } + if (method1 == "Lock" && method2 == "Unlock") || + (method1 == "RLock" && method2 == "RUnlock") { + ReportNodef(pass, block.List[i+1], "empty critical section") + } + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.BlockStmt)(nil)}, fn) + return nil, nil +} + +// cgo produces code like fn(&*_Cvar_kSomeCallbacks) which we don't +// want to flag. +var cgoIdent = regexp.MustCompile(`^_C(func|var)_.+$`) + +func CheckIneffectiveCopy(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + if unary, ok := node.(*ast.UnaryExpr); ok { + if star, ok := unary.X.(*ast.StarExpr); ok && unary.Op == token.AND { + ident, ok := star.X.(*ast.Ident) + if !ok || !cgoIdent.MatchString(ident.Name) { + ReportNodef(pass, unary, "&*x will be simplified to x. It will not copy x.") + } + } + } + + if star, ok := node.(*ast.StarExpr); ok { + if unary, ok := star.X.(*ast.UnaryExpr); ok && unary.Op == token.AND { + ReportNodef(pass, star, "*&x will be simplified to x. It will not copy x.") + } + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.UnaryExpr)(nil), (*ast.StarExpr)(nil)}, fn) + return nil, nil +} + +func CheckDiffSizeComparison(pass *analysis.Pass) (interface{}, error) { + ranges := pass.ResultOf[valueRangesAnalyzer].(map[*ssa.Function]vrp.Ranges) + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + for _, b := range ssafn.Blocks { + for _, ins := range b.Instrs { + binop, ok := ins.(*ssa.BinOp) + if !ok { + continue + } + if binop.Op != token.EQL && binop.Op != token.NEQ { + continue + } + _, ok1 := binop.X.(*ssa.Slice) + _, ok2 := binop.Y.(*ssa.Slice) + if !ok1 && !ok2 { + continue + } + r := ranges[ssafn] + r1, ok1 := r.Get(binop.X).(vrp.StringInterval) + r2, ok2 := r.Get(binop.Y).(vrp.StringInterval) + if !ok1 || !ok2 { + continue + } + if r1.Length.Intersection(r2.Length).Empty() { + pass.Reportf(binop.Pos(), "comparing strings of different sizes for equality will always return false") + } + } + } + } + return nil, nil +} + +func CheckCanonicalHeaderKey(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node, push bool) bool { + if !push { + return false + } + assign, ok := node.(*ast.AssignStmt) + if ok { + // TODO(dh): This risks missing some Header reads, for + // example in `h1["foo"] = h2["foo"]` – these edge + // cases are probably rare enough to ignore for now. + for _, expr := range assign.Lhs { + op, ok := expr.(*ast.IndexExpr) + if !ok { + continue + } + if IsOfType(pass, op.X, "net/http.Header") { + return false + } + } + return true + } + op, ok := node.(*ast.IndexExpr) + if !ok { + return true + } + if !IsOfType(pass, op.X, "net/http.Header") { + return true + } + s, ok := ExprToString(pass, op.Index) + if !ok { + return true + } + if s == http.CanonicalHeaderKey(s) { + return true + } + ReportNodef(pass, op, "keys in http.Header are canonicalized, %q is not canonical; fix the constant or use http.CanonicalHeaderKey", s) + return true + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Nodes([]ast.Node{(*ast.AssignStmt)(nil), (*ast.IndexExpr)(nil)}, fn) + return nil, nil +} + +func CheckBenchmarkN(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + assign := node.(*ast.AssignStmt) + if len(assign.Lhs) != 1 || len(assign.Rhs) != 1 { + return + } + sel, ok := assign.Lhs[0].(*ast.SelectorExpr) + if !ok { + return + } + if sel.Sel.Name != "N" { + return + } + if !IsOfType(pass, sel.X, "*testing.B") { + return + } + ReportNodef(pass, assign, "should not assign to %s", Render(pass, sel)) + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.AssignStmt)(nil)}, fn) + return nil, nil +} + +func CheckUnreadVariableValues(pass *analysis.Pass) (interface{}, error) { + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + if IsExample(ssafn) { + continue + } + node := ssafn.Syntax() + if node == nil { + continue + } + if gen, ok := Generator(pass, node.Pos()); ok && gen == facts.Goyacc { + // Don't flag unused values in code generated by goyacc. + // There may be hundreds of those due to the way the state + // machine is constructed. + continue + } + + switchTags := map[ssa.Value]struct{}{} + ast.Inspect(node, func(node ast.Node) bool { + s, ok := node.(*ast.SwitchStmt) + if !ok { + return true + } + v, _ := ssafn.ValueForExpr(s.Tag) + switchTags[v] = struct{}{} + return true + }) + + hasUse := func(v ssa.Value) bool { + if _, ok := switchTags[v]; ok { + return true + } + refs := v.Referrers() + if refs == nil { + // TODO investigate why refs can be nil + return true + } + return len(FilterDebug(*refs)) > 0 + } + + ast.Inspect(node, func(node ast.Node) bool { + assign, ok := node.(*ast.AssignStmt) + if !ok { + return true + } + if len(assign.Lhs) > 1 && len(assign.Rhs) == 1 { + // Either a function call with multiple return values, + // or a comma-ok assignment + + val, _ := ssafn.ValueForExpr(assign.Rhs[0]) + if val == nil { + return true + } + refs := val.Referrers() + if refs == nil { + return true + } + for _, ref := range *refs { + ex, ok := ref.(*ssa.Extract) + if !ok { + continue + } + if !hasUse(ex) { + lhs := assign.Lhs[ex.Index] + if ident, ok := lhs.(*ast.Ident); !ok || ok && ident.Name == "_" { + continue + } + ReportNodef(pass, lhs, "this value of %s is never used", lhs) + } + } + return true + } + for i, lhs := range assign.Lhs { + rhs := assign.Rhs[i] + if ident, ok := lhs.(*ast.Ident); !ok || ok && ident.Name == "_" { + continue + } + val, _ := ssafn.ValueForExpr(rhs) + if val == nil { + continue + } + + if !hasUse(val) { + ReportNodef(pass, lhs, "this value of %s is never used", lhs) + } + } + return true + }) + } + return nil, nil +} + +func CheckPredeterminedBooleanExprs(pass *analysis.Pass) (interface{}, error) { + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + for _, block := range ssafn.Blocks { + for _, ins := range block.Instrs { + ssabinop, ok := ins.(*ssa.BinOp) + if !ok { + continue + } + switch ssabinop.Op { + case token.GTR, token.LSS, token.EQL, token.NEQ, token.LEQ, token.GEQ: + default: + continue + } + + xs, ok1 := consts(ssabinop.X, nil, nil) + ys, ok2 := consts(ssabinop.Y, nil, nil) + if !ok1 || !ok2 || len(xs) == 0 || len(ys) == 0 { + continue + } + + trues := 0 + for _, x := range xs { + for _, y := range ys { + if x.Value == nil { + if y.Value == nil { + trues++ + } + continue + } + if constant.Compare(x.Value, ssabinop.Op, y.Value) { + trues++ + } + } + } + b := trues != 0 + if trues == 0 || trues == len(xs)*len(ys) { + pass.Reportf(ssabinop.Pos(), "binary expression is always %t for all possible values (%s %s %s)", + b, xs, ssabinop.Op, ys) + } + } + } + } + return nil, nil +} + +func CheckNilMaps(pass *analysis.Pass) (interface{}, error) { + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + for _, block := range ssafn.Blocks { + for _, ins := range block.Instrs { + mu, ok := ins.(*ssa.MapUpdate) + if !ok { + continue + } + c, ok := mu.Map.(*ssa.Const) + if !ok { + continue + } + if c.Value != nil { + continue + } + pass.Reportf(mu.Pos(), "assignment to nil map") + } + } + } + return nil, nil +} + +func CheckExtremeComparison(pass *analysis.Pass) (interface{}, error) { + isobj := func(expr ast.Expr, name string) bool { + sel, ok := expr.(*ast.SelectorExpr) + if !ok { + return false + } + return IsObject(pass.TypesInfo.ObjectOf(sel.Sel), name) + } + + fn := func(node ast.Node) { + expr := node.(*ast.BinaryExpr) + tx := pass.TypesInfo.TypeOf(expr.X) + basic, ok := tx.Underlying().(*types.Basic) + if !ok { + return + } + + var max string + var min string + + switch basic.Kind() { + case types.Uint8: + max = "math.MaxUint8" + case types.Uint16: + max = "math.MaxUint16" + case types.Uint32: + max = "math.MaxUint32" + case types.Uint64: + max = "math.MaxUint64" + case types.Uint: + max = "math.MaxUint64" + + case types.Int8: + min = "math.MinInt8" + max = "math.MaxInt8" + case types.Int16: + min = "math.MinInt16" + max = "math.MaxInt16" + case types.Int32: + min = "math.MinInt32" + max = "math.MaxInt32" + case types.Int64: + min = "math.MinInt64" + max = "math.MaxInt64" + case types.Int: + min = "math.MinInt64" + max = "math.MaxInt64" + } + + if (expr.Op == token.GTR || expr.Op == token.GEQ) && isobj(expr.Y, max) || + (expr.Op == token.LSS || expr.Op == token.LEQ) && isobj(expr.X, max) { + ReportNodef(pass, expr, "no value of type %s is greater than %s", basic, max) + } + if expr.Op == token.LEQ && isobj(expr.Y, max) || + expr.Op == token.GEQ && isobj(expr.X, max) { + ReportNodef(pass, expr, "every value of type %s is <= %s", basic, max) + } + + if (basic.Info() & types.IsUnsigned) != 0 { + if (expr.Op == token.LSS || expr.Op == token.LEQ) && IsIntLiteral(expr.Y, "0") || + (expr.Op == token.GTR || expr.Op == token.GEQ) && IsIntLiteral(expr.X, "0") { + ReportNodef(pass, expr, "no value of type %s is less than 0", basic) + } + if expr.Op == token.GEQ && IsIntLiteral(expr.Y, "0") || + expr.Op == token.LEQ && IsIntLiteral(expr.X, "0") { + ReportNodef(pass, expr, "every value of type %s is >= 0", basic) + } + } else { + if (expr.Op == token.LSS || expr.Op == token.LEQ) && isobj(expr.Y, min) || + (expr.Op == token.GTR || expr.Op == token.GEQ) && isobj(expr.X, min) { + ReportNodef(pass, expr, "no value of type %s is less than %s", basic, min) + } + if expr.Op == token.GEQ && isobj(expr.Y, min) || + expr.Op == token.LEQ && isobj(expr.X, min) { + ReportNodef(pass, expr, "every value of type %s is >= %s", basic, min) + } + } + + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.BinaryExpr)(nil)}, fn) + return nil, nil +} + +func consts(val ssa.Value, out []*ssa.Const, visitedPhis map[string]bool) ([]*ssa.Const, bool) { + if visitedPhis == nil { + visitedPhis = map[string]bool{} + } + var ok bool + switch val := val.(type) { + case *ssa.Phi: + if visitedPhis[val.Name()] { + break + } + visitedPhis[val.Name()] = true + vals := val.Operands(nil) + for _, phival := range vals { + out, ok = consts(*phival, out, visitedPhis) + if !ok { + return nil, false + } + } + case *ssa.Const: + out = append(out, val) + case *ssa.Convert: + out, ok = consts(val.X, out, visitedPhis) + if !ok { + return nil, false + } + default: + return nil, false + } + if len(out) < 2 { + return out, true + } + uniq := []*ssa.Const{out[0]} + for _, val := range out[1:] { + if val.Value == uniq[len(uniq)-1].Value { + continue + } + uniq = append(uniq, val) + } + return uniq, true +} + +func CheckLoopCondition(pass *analysis.Pass) (interface{}, error) { + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + fn := func(node ast.Node) bool { + loop, ok := node.(*ast.ForStmt) + if !ok { + return true + } + if loop.Init == nil || loop.Cond == nil || loop.Post == nil { + return true + } + init, ok := loop.Init.(*ast.AssignStmt) + if !ok || len(init.Lhs) != 1 || len(init.Rhs) != 1 { + return true + } + cond, ok := loop.Cond.(*ast.BinaryExpr) + if !ok { + return true + } + x, ok := cond.X.(*ast.Ident) + if !ok { + return true + } + lhs, ok := init.Lhs[0].(*ast.Ident) + if !ok { + return true + } + if x.Obj != lhs.Obj { + return true + } + if _, ok := loop.Post.(*ast.IncDecStmt); !ok { + return true + } + + v, isAddr := ssafn.ValueForExpr(cond.X) + if v == nil || isAddr { + return true + } + switch v := v.(type) { + case *ssa.Phi: + ops := v.Operands(nil) + if len(ops) != 2 { + return true + } + _, ok := (*ops[0]).(*ssa.Const) + if !ok { + return true + } + sigma, ok := (*ops[1]).(*ssa.Sigma) + if !ok { + return true + } + if sigma.X != v { + return true + } + case *ssa.UnOp: + return true + } + ReportNodef(pass, cond, "variable in loop condition never changes") + + return true + } + Inspect(ssafn.Syntax(), fn) + } + return nil, nil +} + +func CheckArgOverwritten(pass *analysis.Pass) (interface{}, error) { + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + fn := func(node ast.Node) bool { + var typ *ast.FuncType + var body *ast.BlockStmt + switch fn := node.(type) { + case *ast.FuncDecl: + typ = fn.Type + body = fn.Body + case *ast.FuncLit: + typ = fn.Type + body = fn.Body + } + if body == nil { + return true + } + if len(typ.Params.List) == 0 { + return true + } + for _, field := range typ.Params.List { + for _, arg := range field.Names { + obj := pass.TypesInfo.ObjectOf(arg) + var ssaobj *ssa.Parameter + for _, param := range ssafn.Params { + if param.Object() == obj { + ssaobj = param + break + } + } + if ssaobj == nil { + continue + } + refs := ssaobj.Referrers() + if refs == nil { + continue + } + if len(FilterDebug(*refs)) != 0 { + continue + } + + assigned := false + ast.Inspect(body, func(node ast.Node) bool { + assign, ok := node.(*ast.AssignStmt) + if !ok { + return true + } + for _, lhs := range assign.Lhs { + ident, ok := lhs.(*ast.Ident) + if !ok { + continue + } + if pass.TypesInfo.ObjectOf(ident) == obj { + assigned = true + return false + } + } + return true + }) + if assigned { + ReportNodef(pass, arg, "argument %s is overwritten before first use", arg) + } + } + } + return true + } + Inspect(ssafn.Syntax(), fn) + } + return nil, nil +} + +func CheckIneffectiveLoop(pass *analysis.Pass) (interface{}, error) { + // This check detects some, but not all unconditional loop exits. + // We give up in the following cases: + // + // - a goto anywhere in the loop. The goto might skip over our + // return, and we don't check that it doesn't. + // + // - any nested, unlabelled continue, even if it is in another + // loop or closure. + fn := func(node ast.Node) { + var body *ast.BlockStmt + switch fn := node.(type) { + case *ast.FuncDecl: + body = fn.Body + case *ast.FuncLit: + body = fn.Body + default: + panic(fmt.Sprintf("unreachable: %T", node)) + } + if body == nil { + return + } + labels := map[*ast.Object]ast.Stmt{} + ast.Inspect(body, func(node ast.Node) bool { + label, ok := node.(*ast.LabeledStmt) + if !ok { + return true + } + labels[label.Label.Obj] = label.Stmt + return true + }) + + ast.Inspect(body, func(node ast.Node) bool { + var loop ast.Node + var body *ast.BlockStmt + switch node := node.(type) { + case *ast.ForStmt: + body = node.Body + loop = node + case *ast.RangeStmt: + typ := pass.TypesInfo.TypeOf(node.X) + if _, ok := typ.Underlying().(*types.Map); ok { + // looping once over a map is a valid pattern for + // getting an arbitrary element. + return true + } + body = node.Body + loop = node + default: + return true + } + if len(body.List) < 2 { + // avoid flagging the somewhat common pattern of using + // a range loop to get the first element in a slice, + // or the first rune in a string. + return true + } + var unconditionalExit ast.Node + hasBranching := false + for _, stmt := range body.List { + switch stmt := stmt.(type) { + case *ast.BranchStmt: + switch stmt.Tok { + case token.BREAK: + if stmt.Label == nil || labels[stmt.Label.Obj] == loop { + unconditionalExit = stmt + } + case token.CONTINUE: + if stmt.Label == nil || labels[stmt.Label.Obj] == loop { + unconditionalExit = nil + return false + } + } + case *ast.ReturnStmt: + unconditionalExit = stmt + case *ast.IfStmt, *ast.ForStmt, *ast.RangeStmt, *ast.SwitchStmt, *ast.SelectStmt: + hasBranching = true + } + } + if unconditionalExit == nil || !hasBranching { + return false + } + ast.Inspect(body, func(node ast.Node) bool { + if branch, ok := node.(*ast.BranchStmt); ok { + + switch branch.Tok { + case token.GOTO: + unconditionalExit = nil + return false + case token.CONTINUE: + if branch.Label != nil && labels[branch.Label.Obj] != loop { + return true + } + unconditionalExit = nil + return false + } + } + return true + }) + if unconditionalExit != nil { + ReportNodef(pass, unconditionalExit, "the surrounding loop is unconditionally terminated") + } + return true + }) + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.FuncDecl)(nil), (*ast.FuncLit)(nil)}, fn) + return nil, nil +} + +func CheckNilContext(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + call := node.(*ast.CallExpr) + if len(call.Args) == 0 { + return + } + if typ, ok := pass.TypesInfo.TypeOf(call.Args[0]).(*types.Basic); !ok || typ.Kind() != types.UntypedNil { + return + } + sig, ok := pass.TypesInfo.TypeOf(call.Fun).(*types.Signature) + if !ok { + return + } + if sig.Params().Len() == 0 { + return + } + if !IsType(sig.Params().At(0).Type(), "context.Context") { + return + } + ReportNodef(pass, call.Args[0], + "do not pass a nil Context, even if a function permits it; pass context.TODO if you are unsure about which Context to use") + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn) + return nil, nil +} + +func CheckSeeker(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + call := node.(*ast.CallExpr) + sel, ok := call.Fun.(*ast.SelectorExpr) + if !ok { + return + } + if sel.Sel.Name != "Seek" { + return + } + if len(call.Args) != 2 { + return + } + arg0, ok := call.Args[Arg("(io.Seeker).Seek.offset")].(*ast.SelectorExpr) + if !ok { + return + } + switch arg0.Sel.Name { + case "SeekStart", "SeekCurrent", "SeekEnd": + default: + return + } + pkg, ok := arg0.X.(*ast.Ident) + if !ok { + return + } + if pkg.Name != "io" { + return + } + ReportNodef(pass, call, "the first argument of io.Seeker is the offset, but an io.Seek* constant is being used instead") + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn) + return nil, nil +} + +func CheckIneffectiveAppend(pass *analysis.Pass) (interface{}, error) { + isAppend := func(ins ssa.Value) bool { + call, ok := ins.(*ssa.Call) + if !ok { + return false + } + if call.Call.IsInvoke() { + return false + } + if builtin, ok := call.Call.Value.(*ssa.Builtin); !ok || builtin.Name() != "append" { + return false + } + return true + } + + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + for _, block := range ssafn.Blocks { + for _, ins := range block.Instrs { + val, ok := ins.(ssa.Value) + if !ok || !isAppend(val) { + continue + } + + isUsed := false + visited := map[ssa.Instruction]bool{} + var walkRefs func(refs []ssa.Instruction) + walkRefs = func(refs []ssa.Instruction) { + loop: + for _, ref := range refs { + if visited[ref] { + continue + } + visited[ref] = true + if _, ok := ref.(*ssa.DebugRef); ok { + continue + } + switch ref := ref.(type) { + case *ssa.Phi: + walkRefs(*ref.Referrers()) + case *ssa.Sigma: + walkRefs(*ref.Referrers()) + case ssa.Value: + if !isAppend(ref) { + isUsed = true + } else { + walkRefs(*ref.Referrers()) + } + case ssa.Instruction: + isUsed = true + break loop + } + } + } + refs := val.Referrers() + if refs == nil { + continue + } + walkRefs(*refs) + if !isUsed { + pass.Reportf(ins.Pos(), "this result of append is never used, except maybe in other appends") + } + } + } + } + return nil, nil +} + +func CheckConcurrentTesting(pass *analysis.Pass) (interface{}, error) { + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + for _, block := range ssafn.Blocks { + for _, ins := range block.Instrs { + gostmt, ok := ins.(*ssa.Go) + if !ok { + continue + } + var fn *ssa.Function + switch val := gostmt.Call.Value.(type) { + case *ssa.Function: + fn = val + case *ssa.MakeClosure: + fn = val.Fn.(*ssa.Function) + default: + continue + } + if fn.Blocks == nil { + continue + } + for _, block := range fn.Blocks { + for _, ins := range block.Instrs { + call, ok := ins.(*ssa.Call) + if !ok { + continue + } + if call.Call.IsInvoke() { + continue + } + callee := call.Call.StaticCallee() + if callee == nil { + continue + } + recv := callee.Signature.Recv() + if recv == nil { + continue + } + if !IsType(recv.Type(), "*testing.common") { + continue + } + fn, ok := call.Call.StaticCallee().Object().(*types.Func) + if !ok { + continue + } + name := fn.Name() + switch name { + case "FailNow", "Fatal", "Fatalf", "SkipNow", "Skip", "Skipf": + default: + continue + } + pass.Reportf(gostmt.Pos(), "the goroutine calls T.%s, which must be called in the same goroutine as the test", name) + } + } + } + } + } + return nil, nil +} + +func eachCall(ssafn *ssa.Function, fn func(caller *ssa.Function, site ssa.CallInstruction, callee *ssa.Function)) { + for _, b := range ssafn.Blocks { + for _, instr := range b.Instrs { + if site, ok := instr.(ssa.CallInstruction); ok { + if g := site.Common().StaticCallee(); g != nil { + fn(ssafn, site, g) + } + } + } + } +} + +func CheckCyclicFinalizer(pass *analysis.Pass) (interface{}, error) { + fn := func(caller *ssa.Function, site ssa.CallInstruction, callee *ssa.Function) { + if callee.RelString(nil) != "runtime.SetFinalizer" { + return + } + arg0 := site.Common().Args[Arg("runtime.SetFinalizer.obj")] + if iface, ok := arg0.(*ssa.MakeInterface); ok { + arg0 = iface.X + } + unop, ok := arg0.(*ssa.UnOp) + if !ok { + return + } + v, ok := unop.X.(*ssa.Alloc) + if !ok { + return + } + arg1 := site.Common().Args[Arg("runtime.SetFinalizer.finalizer")] + if iface, ok := arg1.(*ssa.MakeInterface); ok { + arg1 = iface.X + } + mc, ok := arg1.(*ssa.MakeClosure) + if !ok { + return + } + for _, b := range mc.Bindings { + if b == v { + pos := lint.DisplayPosition(pass.Fset, mc.Fn.Pos()) + pass.Reportf(site.Pos(), "the finalizer closes over the object, preventing the finalizer from ever running (at %s)", pos) + } + } + } + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + eachCall(ssafn, fn) + } + return nil, nil +} + +/* +func CheckSliceOutOfBounds(pass *analysis.Pass) (interface{}, error) { + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + for _, block := range ssafn.Blocks { + for _, ins := range block.Instrs { + ia, ok := ins.(*ssa.IndexAddr) + if !ok { + continue + } + if _, ok := ia.X.Type().Underlying().(*types.Slice); !ok { + continue + } + sr, ok1 := c.funcDescs.Get(ssafn).Ranges[ia.X].(vrp.SliceInterval) + idxr, ok2 := c.funcDescs.Get(ssafn).Ranges[ia.Index].(vrp.IntInterval) + if !ok1 || !ok2 || !sr.IsKnown() || !idxr.IsKnown() || sr.Length.Empty() || idxr.Empty() { + continue + } + if idxr.Lower.Cmp(sr.Length.Upper) >= 0 { + ReportNodef(pass, ia, "index out of bounds") + } + } + } + } + return nil, nil +} +*/ + +func CheckDeferLock(pass *analysis.Pass) (interface{}, error) { + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + for _, block := range ssafn.Blocks { + instrs := FilterDebug(block.Instrs) + if len(instrs) < 2 { + continue + } + for i, ins := range instrs[:len(instrs)-1] { + call, ok := ins.(*ssa.Call) + if !ok { + continue + } + if !IsCallTo(call.Common(), "(*sync.Mutex).Lock") && !IsCallTo(call.Common(), "(*sync.RWMutex).RLock") { + continue + } + nins, ok := instrs[i+1].(*ssa.Defer) + if !ok { + continue + } + if !IsCallTo(&nins.Call, "(*sync.Mutex).Lock") && !IsCallTo(&nins.Call, "(*sync.RWMutex).RLock") { + continue + } + if call.Common().Args[0] != nins.Call.Args[0] { + continue + } + name := shortCallName(call.Common()) + alt := "" + switch name { + case "Lock": + alt = "Unlock" + case "RLock": + alt = "RUnlock" + } + pass.Reportf(nins.Pos(), "deferring %s right after having locked already; did you mean to defer %s?", name, alt) + } + } + } + return nil, nil +} + +func CheckNaNComparison(pass *analysis.Pass) (interface{}, error) { + isNaN := func(v ssa.Value) bool { + call, ok := v.(*ssa.Call) + if !ok { + return false + } + return IsCallTo(call.Common(), "math.NaN") + } + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + for _, block := range ssafn.Blocks { + for _, ins := range block.Instrs { + ins, ok := ins.(*ssa.BinOp) + if !ok { + continue + } + if isNaN(ins.X) || isNaN(ins.Y) { + pass.Reportf(ins.Pos(), "no value is equal to NaN, not even NaN itself") + } + } + } + } + return nil, nil +} + +func CheckInfiniteRecursion(pass *analysis.Pass) (interface{}, error) { + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + eachCall(ssafn, func(caller *ssa.Function, site ssa.CallInstruction, callee *ssa.Function) { + if callee != ssafn { + return + } + if _, ok := site.(*ssa.Go); ok { + // Recursively spawning goroutines doesn't consume + // stack space infinitely, so don't flag it. + return + } + + block := site.Block() + canReturn := false + for _, b := range ssafn.Blocks { + if block.Dominates(b) { + continue + } + if len(b.Instrs) == 0 { + continue + } + if _, ok := b.Instrs[len(b.Instrs)-1].(*ssa.Return); ok { + canReturn = true + break + } + } + if canReturn { + return + } + pass.Reportf(site.Pos(), "infinite recursive call") + }) + } + return nil, nil +} + +func objectName(obj types.Object) string { + if obj == nil { + return "" + } + var name string + if obj.Pkg() != nil && obj.Pkg().Scope().Lookup(obj.Name()) == obj { + s := obj.Pkg().Path() + if s != "" { + name += s + "." + } + } + name += obj.Name() + return name +} + +func isName(pass *analysis.Pass, expr ast.Expr, name string) bool { + var obj types.Object + switch expr := expr.(type) { + case *ast.Ident: + obj = pass.TypesInfo.ObjectOf(expr) + case *ast.SelectorExpr: + obj = pass.TypesInfo.ObjectOf(expr.Sel) + } + return objectName(obj) == name +} + +func CheckLeakyTimeTick(pass *analysis.Pass) (interface{}, error) { + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + if IsInMain(pass, ssafn) || IsInTest(pass, ssafn) { + continue + } + for _, block := range ssafn.Blocks { + for _, ins := range block.Instrs { + call, ok := ins.(*ssa.Call) + if !ok || !IsCallTo(call.Common(), "time.Tick") { + continue + } + if !functions.Terminates(call.Parent()) { + continue + } + pass.Reportf(call.Pos(), "using time.Tick leaks the underlying ticker, consider using it only in endless functions, tests and the main package, and use time.NewTicker here") + } + } + } + return nil, nil +} + +func CheckDoubleNegation(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + unary1 := node.(*ast.UnaryExpr) + unary2, ok := unary1.X.(*ast.UnaryExpr) + if !ok { + return + } + if unary1.Op != token.NOT || unary2.Op != token.NOT { + return + } + ReportNodef(pass, unary1, "negating a boolean twice has no effect; is this a typo?") + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.UnaryExpr)(nil)}, fn) + return nil, nil +} + +func hasSideEffects(node ast.Node) bool { + dynamic := false + ast.Inspect(node, func(node ast.Node) bool { + switch node := node.(type) { + case *ast.CallExpr: + dynamic = true + return false + case *ast.UnaryExpr: + if node.Op == token.ARROW { + dynamic = true + return false + } + } + return true + }) + return dynamic +} + +func CheckRepeatedIfElse(pass *analysis.Pass) (interface{}, error) { + seen := map[ast.Node]bool{} + + var collectConds func(ifstmt *ast.IfStmt, inits []ast.Stmt, conds []ast.Expr) ([]ast.Stmt, []ast.Expr) + collectConds = func(ifstmt *ast.IfStmt, inits []ast.Stmt, conds []ast.Expr) ([]ast.Stmt, []ast.Expr) { + seen[ifstmt] = true + if ifstmt.Init != nil { + inits = append(inits, ifstmt.Init) + } + conds = append(conds, ifstmt.Cond) + if elsestmt, ok := ifstmt.Else.(*ast.IfStmt); ok { + return collectConds(elsestmt, inits, conds) + } + return inits, conds + } + fn := func(node ast.Node) { + ifstmt := node.(*ast.IfStmt) + if seen[ifstmt] { + return + } + inits, conds := collectConds(ifstmt, nil, nil) + if len(inits) > 0 { + return + } + for _, cond := range conds { + if hasSideEffects(cond) { + return + } + } + counts := map[string]int{} + for _, cond := range conds { + s := Render(pass, cond) + counts[s]++ + if counts[s] == 2 { + ReportNodef(pass, cond, "this condition occurs multiple times in this if/else if chain") + } + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.IfStmt)(nil)}, fn) + return nil, nil +} + +func CheckSillyBitwiseOps(pass *analysis.Pass) (interface{}, error) { + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + for _, block := range ssafn.Blocks { + for _, ins := range block.Instrs { + ins, ok := ins.(*ssa.BinOp) + if !ok { + continue + } + + if c, ok := ins.Y.(*ssa.Const); !ok || c.Value == nil || c.Value.Kind() != constant.Int || c.Uint64() != 0 { + continue + } + switch ins.Op { + case token.AND, token.OR, token.XOR: + default: + // we do not flag shifts because too often, x<<0 is part + // of a pattern, x<<0, x<<8, x<<16, ... + continue + } + path, _ := astutil.PathEnclosingInterval(File(pass, ins), ins.Pos(), ins.Pos()) + if len(path) == 0 { + continue + } + if node, ok := path[0].(*ast.BinaryExpr); !ok || !IsZero(node.Y) { + continue + } + + switch ins.Op { + case token.AND: + pass.Reportf(ins.Pos(), "x & 0 always equals 0") + case token.OR, token.XOR: + pass.Reportf(ins.Pos(), "x %s 0 always equals x", ins.Op) + } + } + } + } + return nil, nil +} + +func CheckNonOctalFileMode(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + call := node.(*ast.CallExpr) + sig, ok := pass.TypesInfo.TypeOf(call.Fun).(*types.Signature) + if !ok { + return + } + n := sig.Params().Len() + var args []int + for i := 0; i < n; i++ { + typ := sig.Params().At(i).Type() + if IsType(typ, "os.FileMode") { + args = append(args, i) + } + } + for _, i := range args { + lit, ok := call.Args[i].(*ast.BasicLit) + if !ok { + continue + } + if len(lit.Value) == 3 && + lit.Value[0] != '0' && + lit.Value[0] >= '0' && lit.Value[0] <= '7' && + lit.Value[1] >= '0' && lit.Value[1] <= '7' && + lit.Value[2] >= '0' && lit.Value[2] <= '7' { + + v, err := strconv.ParseInt(lit.Value, 10, 64) + if err != nil { + continue + } + ReportNodef(pass, call.Args[i], "file mode '%s' evaluates to %#o; did you mean '0%s'?", lit.Value, v, lit.Value) + } + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn) + return nil, nil +} + +func CheckPureFunctions(pass *analysis.Pass) (interface{}, error) { + pure := pass.ResultOf[facts.Purity].(facts.PurityResult) + +fnLoop: + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + if IsInTest(pass, ssafn) { + params := ssafn.Signature.Params() + for i := 0; i < params.Len(); i++ { + param := params.At(i) + if IsType(param.Type(), "*testing.B") { + // Ignore discarded pure functions in code related + // to benchmarks. Instead of matching BenchmarkFoo + // functions, we match any function accepting a + // *testing.B. Benchmarks sometimes call generic + // functions for doing the actual work, and + // checking for the parameter is a lot easier and + // faster than analyzing call trees. + continue fnLoop + } + } + } + + for _, b := range ssafn.Blocks { + for _, ins := range b.Instrs { + ins, ok := ins.(*ssa.Call) + if !ok { + continue + } + refs := ins.Referrers() + if refs == nil || len(FilterDebug(*refs)) > 0 { + continue + } + callee := ins.Common().StaticCallee() + if callee == nil { + continue + } + if callee.Object() == nil { + // TODO(dh): support anonymous functions + continue + } + if _, ok := pure[callee.Object().(*types.Func)]; ok { + pass.Reportf(ins.Pos(), "%s is a pure function but its return value is ignored", callee.Name()) + continue + } + } + } + } + return nil, nil +} + +func CheckDeprecated(pass *analysis.Pass) (interface{}, error) { + deprs := pass.ResultOf[facts.Deprecated].(facts.DeprecatedResult) + + // Selectors can appear outside of function literals, e.g. when + // declaring package level variables. + + var tfn types.Object + stack := 0 + fn := func(node ast.Node, push bool) bool { + if !push { + stack-- + return false + } + stack++ + if stack == 1 { + tfn = nil + } + if fn, ok := node.(*ast.FuncDecl); ok { + tfn = pass.TypesInfo.ObjectOf(fn.Name) + } + sel, ok := node.(*ast.SelectorExpr) + if !ok { + return true + } + + obj := pass.TypesInfo.ObjectOf(sel.Sel) + if obj.Pkg() == nil { + return true + } + if pass.Pkg == obj.Pkg() || obj.Pkg().Path()+"_test" == pass.Pkg.Path() { + // Don't flag stuff in our own package + return true + } + if depr, ok := deprs.Objects[obj]; ok { + // Look for the first available alternative, not the first + // version something was deprecated in. If a function was + // deprecated in Go 1.6, an alternative has been available + // already in 1.0, and we're targeting 1.2, it still + // makes sense to use the alternative from 1.0, to be + // future-proof. + minVersion := deprecated.Stdlib[SelectorName(pass, sel)].AlternativeAvailableSince + if !IsGoVersion(pass, minVersion) { + return true + } + + if tfn != nil { + if _, ok := deprs.Objects[tfn]; ok { + // functions that are deprecated may use deprecated + // symbols + return true + } + } + ReportNodef(pass, sel, "%s is deprecated: %s", Render(pass, sel), depr.Msg) + return true + } + return true + } + + imps := map[string]*types.Package{} + for _, imp := range pass.Pkg.Imports() { + imps[imp.Path()] = imp + } + fn2 := func(node ast.Node) { + spec := node.(*ast.ImportSpec) + p := spec.Path.Value + path := p[1 : len(p)-1] + imp := imps[path] + if depr, ok := deprs.Packages[imp]; ok { + ReportNodef(pass, spec, "Package %s is deprecated: %s", path, depr.Msg) + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Nodes(nil, fn) + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.ImportSpec)(nil)}, fn2) + return nil, nil +} + +func callChecker(rules map[string]CallCheck) func(pass *analysis.Pass) (interface{}, error) { + return func(pass *analysis.Pass) (interface{}, error) { + return checkCalls(pass, rules) + } +} + +func checkCalls(pass *analysis.Pass, rules map[string]CallCheck) (interface{}, error) { + ranges := pass.ResultOf[valueRangesAnalyzer].(map[*ssa.Function]vrp.Ranges) + fn := func(caller *ssa.Function, site ssa.CallInstruction, callee *ssa.Function) { + obj, ok := callee.Object().(*types.Func) + if !ok { + return + } + + r, ok := rules[lint.FuncName(obj)] + if !ok { + return + } + var args []*Argument + ssaargs := site.Common().Args + if callee.Signature.Recv() != nil { + ssaargs = ssaargs[1:] + } + for _, arg := range ssaargs { + if iarg, ok := arg.(*ssa.MakeInterface); ok { + arg = iarg.X + } + vr := ranges[site.Parent()][arg] + args = append(args, &Argument{Value: Value{arg, vr}}) + } + call := &Call{ + Pass: pass, + Instr: site, + Args: args, + Parent: site.Parent(), + } + r(call) + for idx, arg := range call.Args { + _ = idx + for _, e := range arg.invalids { + // path, _ := astutil.PathEnclosingInterval(f.File, edge.Site.Pos(), edge.Site.Pos()) + // if len(path) < 2 { + // continue + // } + // astcall, ok := path[0].(*ast.CallExpr) + // if !ok { + // continue + // } + // pass.Reportf(astcall.Args[idx], "%s", e) + + pass.Reportf(site.Pos(), "%s", e) + } + } + for _, e := range call.invalids { + pass.Reportf(call.Instr.Common().Pos(), "%s", e) + } + } + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + eachCall(ssafn, fn) + } + return nil, nil +} + +func shortCallName(call *ssa.CallCommon) string { + if call.IsInvoke() { + return "" + } + switch v := call.Value.(type) { + case *ssa.Function: + fn, ok := v.Object().(*types.Func) + if !ok { + return "" + } + return fn.Name() + case *ssa.Builtin: + return v.Name() + } + return "" +} + +func CheckWriterBufferModified(pass *analysis.Pass) (interface{}, error) { + // TODO(dh): this might be a good candidate for taint analysis. + // Taint the argument as MUST_NOT_MODIFY, then propagate that + // through functions like bytes.Split + + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + sig := ssafn.Signature + if ssafn.Name() != "Write" || sig.Recv() == nil || sig.Params().Len() != 1 || sig.Results().Len() != 2 { + continue + } + tArg, ok := sig.Params().At(0).Type().(*types.Slice) + if !ok { + continue + } + if basic, ok := tArg.Elem().(*types.Basic); !ok || basic.Kind() != types.Byte { + continue + } + if basic, ok := sig.Results().At(0).Type().(*types.Basic); !ok || basic.Kind() != types.Int { + continue + } + if named, ok := sig.Results().At(1).Type().(*types.Named); !ok || !IsType(named, "error") { + continue + } + + for _, block := range ssafn.Blocks { + for _, ins := range block.Instrs { + switch ins := ins.(type) { + case *ssa.Store: + addr, ok := ins.Addr.(*ssa.IndexAddr) + if !ok { + continue + } + if addr.X != ssafn.Params[1] { + continue + } + pass.Reportf(ins.Pos(), "io.Writer.Write must not modify the provided buffer, not even temporarily") + case *ssa.Call: + if !IsCallTo(ins.Common(), "append") { + continue + } + if ins.Common().Args[0] != ssafn.Params[1] { + continue + } + pass.Reportf(ins.Pos(), "io.Writer.Write must not modify the provided buffer, not even temporarily") + } + } + } + } + return nil, nil +} + +func loopedRegexp(name string) CallCheck { + return func(call *Call) { + if len(extractConsts(call.Args[0].Value.Value)) == 0 { + return + } + if !isInLoop(call.Instr.Block()) { + return + } + call.Invalid(fmt.Sprintf("calling %s in a loop has poor performance, consider using regexp.Compile", name)) + } +} + +func CheckEmptyBranch(pass *analysis.Pass) (interface{}, error) { + for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + if ssafn.Syntax() == nil { + continue + } + if IsExample(ssafn) { + continue + } + fn := func(node ast.Node) bool { + ifstmt, ok := node.(*ast.IfStmt) + if !ok { + return true + } + if ifstmt.Else != nil { + b, ok := ifstmt.Else.(*ast.BlockStmt) + if !ok || len(b.List) != 0 { + return true + } + ReportfFG(pass, ifstmt.Else.Pos(), "empty branch") + } + if len(ifstmt.Body.List) != 0 { + return true + } + ReportfFG(pass, ifstmt.Pos(), "empty branch") + return true + } + Inspect(ssafn.Syntax(), fn) + } + return nil, nil +} + +func CheckMapBytesKey(pass *analysis.Pass) (interface{}, error) { + for _, fn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + for _, b := range fn.Blocks { + insLoop: + for _, ins := range b.Instrs { + // find []byte -> string conversions + conv, ok := ins.(*ssa.Convert) + if !ok || conv.Type() != types.Universe.Lookup("string").Type() { + continue + } + if s, ok := conv.X.Type().(*types.Slice); !ok || s.Elem() != types.Universe.Lookup("byte").Type() { + continue + } + refs := conv.Referrers() + // need at least two (DebugRef) references: the + // conversion and the *ast.Ident + if refs == nil || len(*refs) < 2 { + continue + } + ident := false + // skip first reference, that's the conversion itself + for _, ref := range (*refs)[1:] { + switch ref := ref.(type) { + case *ssa.DebugRef: + if _, ok := ref.Expr.(*ast.Ident); !ok { + // the string seems to be used somewhere + // unexpected; the default branch should + // catch this already, but be safe + continue insLoop + } else { + ident = true + } + case *ssa.Lookup: + default: + // the string is used somewhere else than a + // map lookup + continue insLoop + } + } + + // the result of the conversion wasn't assigned to an + // identifier + if !ident { + continue + } + pass.Reportf(conv.Pos(), "m[string(key)] would be more efficient than k := string(key); m[k]") + } + } + } + return nil, nil +} + +func CheckRangeStringRunes(pass *analysis.Pass) (interface{}, error) { + return sharedcheck.CheckRangeStringRunes(pass) +} + +func CheckSelfAssignment(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + assign := node.(*ast.AssignStmt) + if assign.Tok != token.ASSIGN || len(assign.Lhs) != len(assign.Rhs) { + return + } + for i, stmt := range assign.Lhs { + rlh := Render(pass, stmt) + rrh := Render(pass, assign.Rhs[i]) + if rlh == rrh { + ReportfFG(pass, assign.Pos(), "self-assignment of %s to %s", rrh, rlh) + } + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.AssignStmt)(nil)}, fn) + return nil, nil +} + +func buildTagsIdentical(s1, s2 []string) bool { + if len(s1) != len(s2) { + return false + } + s1s := make([]string, len(s1)) + copy(s1s, s1) + sort.Strings(s1s) + s2s := make([]string, len(s2)) + copy(s2s, s2) + sort.Strings(s2s) + for i, s := range s1s { + if s != s2s[i] { + return false + } + } + return true +} + +func CheckDuplicateBuildConstraints(pass *analysis.Pass) (interface{}, error) { + for _, f := range pass.Files { + constraints := buildTags(f) + for i, constraint1 := range constraints { + for j, constraint2 := range constraints { + if i >= j { + continue + } + if buildTagsIdentical(constraint1, constraint2) { + ReportfFG(pass, f.Pos(), "identical build constraints %q and %q", + strings.Join(constraint1, " "), + strings.Join(constraint2, " ")) + } + } + } + } + return nil, nil +} + +func CheckSillyRegexp(pass *analysis.Pass) (interface{}, error) { + // We could use the rule checking engine for this, but the + // arguments aren't really invalid. + for _, fn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + for _, b := range fn.Blocks { + for _, ins := range b.Instrs { + call, ok := ins.(*ssa.Call) + if !ok { + continue + } + switch CallName(call.Common()) { + case "regexp.MustCompile", "regexp.Compile", "regexp.Match", "regexp.MatchReader", "regexp.MatchString": + default: + continue + } + c, ok := call.Common().Args[0].(*ssa.Const) + if !ok { + continue + } + s := constant.StringVal(c.Value) + re, err := syntax.Parse(s, 0) + if err != nil { + continue + } + if re.Op != syntax.OpLiteral && re.Op != syntax.OpEmptyMatch { + continue + } + pass.Reportf(call.Pos(), "regular expression does not contain any meta characters") + } + } + } + return nil, nil +} + +func CheckMissingEnumTypesInDeclaration(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + decl := node.(*ast.GenDecl) + if !decl.Lparen.IsValid() { + return + } + if decl.Tok != token.CONST { + return + } + + groups := GroupSpecs(pass.Fset, decl.Specs) + groupLoop: + for _, group := range groups { + if len(group) < 2 { + continue + } + if group[0].(*ast.ValueSpec).Type == nil { + // first constant doesn't have a type + continue groupLoop + } + for i, spec := range group { + spec := spec.(*ast.ValueSpec) + if len(spec.Names) != 1 || len(spec.Values) != 1 { + continue groupLoop + } + switch v := spec.Values[0].(type) { + case *ast.BasicLit: + case *ast.UnaryExpr: + if _, ok := v.X.(*ast.BasicLit); !ok { + continue groupLoop + } + default: + // if it's not a literal it might be typed, such as + // time.Microsecond = 1000 * Nanosecond + continue groupLoop + } + if i == 0 { + continue + } + if spec.Type != nil { + continue groupLoop + } + } + ReportNodef(pass, group[0], "only the first constant in this group has an explicit type") + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.GenDecl)(nil)}, fn) + return nil, nil +} + +func CheckTimerResetReturnValue(pass *analysis.Pass) (interface{}, error) { + for _, fn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + for _, block := range fn.Blocks { + for _, ins := range block.Instrs { + call, ok := ins.(*ssa.Call) + if !ok { + continue + } + if !IsCallTo(call.Common(), "(*time.Timer).Reset") { + continue + } + refs := call.Referrers() + if refs == nil { + continue + } + for _, ref := range FilterDebug(*refs) { + ifstmt, ok := ref.(*ssa.If) + if !ok { + continue + } + + found := false + for _, succ := range ifstmt.Block().Succs { + if len(succ.Preds) != 1 { + // Merge point, not a branch in the + // syntactical sense. + + // FIXME(dh): this is broken for if + // statements a la "if x || y" + continue + } + ssautil.Walk(succ, func(b *ssa.BasicBlock) bool { + if !succ.Dominates(b) { + // We've reached the end of the branch + return false + } + for _, ins := range b.Instrs { + // TODO(dh): we should check that + // we're receiving from the channel of + // a time.Timer to further reduce + // false positives. Not a key + // priority, considering the rarity of + // Reset and the tiny likeliness of a + // false positive + if ins, ok := ins.(*ssa.UnOp); ok && ins.Op == token.ARROW && IsType(ins.X.Type(), "<-chan time.Time") { + found = true + return false + } + } + return true + }) + } + + if found { + pass.Reportf(call.Pos(), "it is not possible to use Reset's return value correctly, as there is a race condition between draining the channel and the new timer expiring") + } + } + } + } + } + return nil, nil +} + +func CheckToLowerToUpperComparison(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + binExpr := node.(*ast.BinaryExpr) + + var negative bool + switch binExpr.Op { + case token.EQL: + negative = false + case token.NEQ: + negative = true + default: + return + } + + const ( + lo = "strings.ToLower" + up = "strings.ToUpper" + ) + + var call string + if IsCallToAST(pass, binExpr.X, lo) && IsCallToAST(pass, binExpr.Y, lo) { + call = lo + } else if IsCallToAST(pass, binExpr.X, up) && IsCallToAST(pass, binExpr.Y, up) { + call = up + } else { + return + } + + bang := "" + if negative { + bang = "!" + } + + ReportNodef(pass, binExpr, "should use %sstrings.EqualFold(a, b) instead of %s(a) %s %s(b)", bang, call, binExpr.Op, call) + } + + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.BinaryExpr)(nil)}, fn) + return nil, nil +} + +func CheckUnreachableTypeCases(pass *analysis.Pass) (interface{}, error) { + // Check if T subsumes V in a type switch. T subsumes V if T is an interface and T's method set is a subset of V's method set. + subsumes := func(T, V types.Type) bool { + tIface, ok := T.Underlying().(*types.Interface) + if !ok { + return false + } + + return types.Implements(V, tIface) + } + + subsumesAny := func(Ts, Vs []types.Type) (types.Type, types.Type, bool) { + for _, T := range Ts { + for _, V := range Vs { + if subsumes(T, V) { + return T, V, true + } + } + } + + return nil, nil, false + } + + fn := func(node ast.Node) { + tsStmt := node.(*ast.TypeSwitchStmt) + + type ccAndTypes struct { + cc *ast.CaseClause + types []types.Type + } + + // All asserted types in the order of case clauses. + ccs := make([]ccAndTypes, 0, len(tsStmt.Body.List)) + for _, stmt := range tsStmt.Body.List { + cc, _ := stmt.(*ast.CaseClause) + + // Exclude the 'default' case. + if len(cc.List) == 0 { + continue + } + + Ts := make([]types.Type, len(cc.List)) + for i, expr := range cc.List { + Ts[i] = pass.TypesInfo.TypeOf(expr) + } + + ccs = append(ccs, ccAndTypes{cc: cc, types: Ts}) + } + + if len(ccs) <= 1 { + // Zero or one case clauses, nothing to check. + return + } + + // Check if case clauses following cc have types that are subsumed by cc. + for i, cc := range ccs[:len(ccs)-1] { + for _, next := range ccs[i+1:] { + if T, V, yes := subsumesAny(cc.types, next.types); yes { + ReportNodef(pass, next.cc, "unreachable case clause: %s will always match before %s", T.String(), V.String()) + } + } + } + } + + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.TypeSwitchStmt)(nil)}, fn) + return nil, nil +} + +func CheckSingleArgAppend(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + if !IsCallToAST(pass, node, "append") { + return + } + call := node.(*ast.CallExpr) + if len(call.Args) != 1 { + return + } + ReportfFG(pass, call.Pos(), "x = append(y) is equivalent to x = y") + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn) + return nil, nil +} + +func CheckStructTags(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + for _, field := range node.(*ast.StructType).Fields.List { + if field.Tag == nil { + continue + } + tags, err := parseStructTag(field.Tag.Value[1 : len(field.Tag.Value)-1]) + if err != nil { + ReportNodef(pass, field.Tag, "unparseable struct tag: %s", err) + continue + } + for k, v := range tags { + if len(v) > 1 { + ReportNodef(pass, field.Tag, "duplicate struct tag %q", k) + continue + } + + switch k { + case "json": + checkJSONTag(pass, field, v[0]) + case "xml": + checkXMLTag(pass, field, v[0]) + } + } + } + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.StructType)(nil)}, fn) + return nil, nil +} + +func checkJSONTag(pass *analysis.Pass, field *ast.Field, tag string) { + //lint:ignore SA9003 TODO(dh): should we flag empty tags? + if len(tag) == 0 { + } + fields := strings.Split(tag, ",") + for _, r := range fields[0] { + if !unicode.IsLetter(r) && !unicode.IsDigit(r) && !strings.ContainsRune("!#$%&()*+-./:<=>?@[]^_{|}~ ", r) { + ReportNodef(pass, field.Tag, "invalid JSON field name %q", fields[0]) + } + } + var co, cs, ci int + for _, s := range fields[1:] { + switch s { + case "omitempty": + co++ + case "": + // allow stuff like "-," + case "string": + cs++ + // only for string, floating point, integer and bool + T := Dereference(pass.TypesInfo.TypeOf(field.Type).Underlying()).Underlying() + basic, ok := T.(*types.Basic) + if !ok || (basic.Info()&(types.IsBoolean|types.IsInteger|types.IsFloat|types.IsString)) == 0 { + ReportNodef(pass, field.Tag, "the JSON string option only applies to fields of type string, floating point, integer or bool, or pointers to those") + } + case "inline": + ci++ + default: + ReportNodef(pass, field.Tag, "unknown JSON option %q", s) + } + } + if co > 1 { + ReportNodef(pass, field.Tag, `duplicate JSON option "omitempty"`) + } + if cs > 1 { + ReportNodef(pass, field.Tag, `duplicate JSON option "string"`) + } + if ci > 1 { + ReportNodef(pass, field.Tag, `duplicate JSON option "inline"`) + } +} + +func checkXMLTag(pass *analysis.Pass, field *ast.Field, tag string) { + //lint:ignore SA9003 TODO(dh): should we flag empty tags? + if len(tag) == 0 { + } + fields := strings.Split(tag, ",") + counts := map[string]int{} + var exclusives []string + for _, s := range fields[1:] { + switch s { + case "attr", "chardata", "cdata", "innerxml", "comment": + counts[s]++ + if counts[s] == 1 { + exclusives = append(exclusives, s) + } + case "omitempty", "any": + counts[s]++ + case "": + default: + ReportNodef(pass, field.Tag, "unknown XML option %q", s) + } + } + for k, v := range counts { + if v > 1 { + ReportNodef(pass, field.Tag, "duplicate XML option %q", k) + } + } + if len(exclusives) > 1 { + ReportNodef(pass, field.Tag, "XML options %s are mutually exclusive", strings.Join(exclusives, " and ")) + } +} diff --git a/vendor/github.com/golangci/go-tools/staticcheck/rules.go b/vendor/honnef.co/go/tools/staticcheck/rules.go similarity index 91% rename from vendor/github.com/golangci/go-tools/staticcheck/rules.go rename to vendor/honnef.co/go/tools/staticcheck/rules.go index 1e6ef8ce3e8a..0152cac1af13 100644 --- a/vendor/github.com/golangci/go-tools/staticcheck/rules.go +++ b/vendor/honnef.co/go/tools/staticcheck/rules.go @@ -13,10 +13,10 @@ import ( "time" "unicode/utf8" - "github.com/golangci/go-tools/lint" - . "github.com/golangci/go-tools/lint/lintdsl" - "github.com/golangci/go-tools/ssa" - "github.com/golangci/go-tools/staticcheck/vrp" + "golang.org/x/tools/go/analysis" + . "honnef.co/go/tools/lint/lintdsl" + "honnef.co/go/tools/ssa" + "honnef.co/go/tools/staticcheck/vrp" ) const ( @@ -26,12 +26,11 @@ const ( ) type Call struct { - Job *lint.Job + Pass *analysis.Pass Instr ssa.CallInstruction Args []*Argument - Checker *Checker - Parent *ssa.Function + Parent *ssa.Function invalids []string } @@ -184,7 +183,7 @@ func ConvertedFromInt(v Value) bool { return true } -func validEncodingBinaryType(j *lint.Job, typ types.Type) bool { +func validEncodingBinaryType(pass *analysis.Pass, typ types.Type) bool { typ = typ.Underlying() switch typ := typ.(type) { case *types.Basic: @@ -194,19 +193,19 @@ func validEncodingBinaryType(j *lint.Job, typ types.Type) bool { types.Float32, types.Float64, types.Complex64, types.Complex128, types.Invalid: return true case types.Bool: - return IsGoVersion(j, 8) + return IsGoVersion(pass, 8) } return false case *types.Struct: n := typ.NumFields() for i := 0; i < n; i++ { - if !validEncodingBinaryType(j, typ.Field(i).Type()) { + if !validEncodingBinaryType(pass, typ.Field(i).Type()) { return false } } return true case *types.Array: - return validEncodingBinaryType(j, typ.Elem()) + return validEncodingBinaryType(pass, typ.Elem()) case *types.Interface: // we can't determine if it's a valid type or not return true @@ -214,7 +213,7 @@ func validEncodingBinaryType(j *lint.Job, typ types.Type) bool { return false } -func CanBinaryMarshal(j *lint.Job, v Value) bool { +func CanBinaryMarshal(pass *analysis.Pass, v Value) bool { typ := v.Value.Type().Underlying() if ttyp, ok := typ.(*types.Pointer); ok { typ = ttyp.Elem().Underlying() @@ -227,7 +226,7 @@ func CanBinaryMarshal(j *lint.Job, v Value) bool { } } - return validEncodingBinaryType(j, typ) + return validEncodingBinaryType(pass, typ) } func RepeatZeroTimes(name string, arg int) CallCheck { diff --git a/vendor/honnef.co/go/tools/staticcheck/structtag.go b/vendor/honnef.co/go/tools/staticcheck/structtag.go new file mode 100644 index 000000000000..38830a22c639 --- /dev/null +++ b/vendor/honnef.co/go/tools/staticcheck/structtag.go @@ -0,0 +1,58 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Copyright 2019 Dominik Honnef. All rights reserved. + +package staticcheck + +import "strconv" + +func parseStructTag(tag string) (map[string][]string, error) { + // FIXME(dh): detect missing closing quote + out := map[string][]string{} + + for tag != "" { + // Skip leading space. + i := 0 + for i < len(tag) && tag[i] == ' ' { + i++ + } + tag = tag[i:] + if tag == "" { + break + } + + // Scan to colon. A space, a quote or a control character is a syntax error. + // Strictly speaking, control chars include the range [0x7f, 0x9f], not just + // [0x00, 0x1f], but in practice, we ignore the multi-byte control characters + // as it is simpler to inspect the tag's bytes than the tag's runes. + i = 0 + for i < len(tag) && tag[i] > ' ' && tag[i] != ':' && tag[i] != '"' && tag[i] != 0x7f { + i++ + } + if i == 0 || i+1 >= len(tag) || tag[i] != ':' || tag[i+1] != '"' { + break + } + name := string(tag[:i]) + tag = tag[i+1:] + + // Scan quoted string to find value. + i = 1 + for i < len(tag) && tag[i] != '"' { + if tag[i] == '\\' { + i++ + } + i++ + } + if i >= len(tag) { + break + } + qvalue := string(tag[:i+1]) + tag = tag[i+1:] + + value, err := strconv.Unquote(qvalue) + if err != nil { + return nil, err + } + out[name] = append(out[name], value) + } + return out, nil +} diff --git a/vendor/github.com/golangci/go-tools/staticcheck/vrp/channel.go b/vendor/honnef.co/go/tools/staticcheck/vrp/channel.go similarity index 97% rename from vendor/github.com/golangci/go-tools/staticcheck/vrp/channel.go rename to vendor/honnef.co/go/tools/staticcheck/vrp/channel.go index 8f8a8fdfdb3c..0ef73787ba39 100644 --- a/vendor/github.com/golangci/go-tools/staticcheck/vrp/channel.go +++ b/vendor/honnef.co/go/tools/staticcheck/vrp/channel.go @@ -3,7 +3,7 @@ package vrp import ( "fmt" - "github.com/golangci/go-tools/ssa" + "honnef.co/go/tools/ssa" ) type ChannelInterval struct { diff --git a/vendor/github.com/golangci/go-tools/staticcheck/vrp/int.go b/vendor/honnef.co/go/tools/staticcheck/vrp/int.go similarity index 99% rename from vendor/github.com/golangci/go-tools/staticcheck/vrp/int.go rename to vendor/honnef.co/go/tools/staticcheck/vrp/int.go index 630965930e40..926bb7af3d62 100644 --- a/vendor/github.com/golangci/go-tools/staticcheck/vrp/int.go +++ b/vendor/honnef.co/go/tools/staticcheck/vrp/int.go @@ -6,7 +6,7 @@ import ( "go/types" "math/big" - "github.com/golangci/go-tools/ssa" + "honnef.co/go/tools/ssa" ) type Zs []Z diff --git a/vendor/github.com/golangci/go-tools/staticcheck/vrp/slice.go b/vendor/honnef.co/go/tools/staticcheck/vrp/slice.go similarity index 99% rename from vendor/github.com/golangci/go-tools/staticcheck/vrp/slice.go rename to vendor/honnef.co/go/tools/staticcheck/vrp/slice.go index 75ab9465f31c..40658dd8d860 100644 --- a/vendor/github.com/golangci/go-tools/staticcheck/vrp/slice.go +++ b/vendor/honnef.co/go/tools/staticcheck/vrp/slice.go @@ -7,7 +7,7 @@ import ( "fmt" "go/types" - "github.com/golangci/go-tools/ssa" + "honnef.co/go/tools/ssa" ) type SliceInterval struct { diff --git a/vendor/github.com/golangci/go-tools/staticcheck/vrp/string.go b/vendor/honnef.co/go/tools/staticcheck/vrp/string.go similarity index 99% rename from vendor/github.com/golangci/go-tools/staticcheck/vrp/string.go rename to vendor/honnef.co/go/tools/staticcheck/vrp/string.go index 42e556ba77af..e05877f9f782 100644 --- a/vendor/github.com/golangci/go-tools/staticcheck/vrp/string.go +++ b/vendor/honnef.co/go/tools/staticcheck/vrp/string.go @@ -5,7 +5,7 @@ import ( "go/token" "go/types" - "github.com/golangci/go-tools/ssa" + "honnef.co/go/tools/ssa" ) type StringInterval struct { diff --git a/vendor/github.com/golangci/go-tools/staticcheck/vrp/vrp.go b/vendor/honnef.co/go/tools/staticcheck/vrp/vrp.go similarity index 98% rename from vendor/github.com/golangci/go-tools/staticcheck/vrp/vrp.go rename to vendor/honnef.co/go/tools/staticcheck/vrp/vrp.go index d05c43e44ea4..3c138e51229a 100644 --- a/vendor/github.com/golangci/go-tools/staticcheck/vrp/vrp.go +++ b/vendor/honnef.co/go/tools/staticcheck/vrp/vrp.go @@ -12,7 +12,8 @@ import ( "sort" "strings" - "github.com/golangci/go-tools/ssa" + "honnef.co/go/tools/lint" + "honnef.co/go/tools/ssa" ) type Future interface { @@ -291,7 +292,7 @@ func BuildGraph(f *ssa.Function) *Graph { case *ssa.Call: if static := ins.Common().StaticCallee(); static != nil { if fn, ok := static.Object().(*types.Func); ok { - switch fn.FullName() { + switch lint.FuncName(fn) { case "bytes.Index", "bytes.IndexAny", "bytes.IndexByte", "bytes.IndexFunc", "bytes.IndexRune", "bytes.LastIndex", "bytes.LastIndexAny", "bytes.LastIndexByte", "bytes.LastIndexFunc", @@ -721,16 +722,22 @@ func (g *Graph) widen(c Constraint, consts []Z) bool { } nlc := NInfinity nuc := PInfinity - for _, co := range consts { - if co.Cmp(ni.Lower) <= 0 { - nlc = co - break + + // Don't get stuck widening for an absurd amount of time due + // to an excess number of constants, as may be present in + // table-based scanners. + if len(consts) < 1000 { + for _, co := range consts { + if co.Cmp(ni.Lower) <= 0 { + nlc = co + break + } } - } - for _, co := range consts { - if co.Cmp(ni.Upper) >= 0 { - nuc = co - break + for _, co := range consts { + if co.Cmp(ni.Upper) >= 0 { + nuc = co + break + } } } diff --git a/vendor/honnef.co/go/tools/stylecheck/analysis.go b/vendor/honnef.co/go/tools/stylecheck/analysis.go new file mode 100644 index 000000000000..f252487f7357 --- /dev/null +++ b/vendor/honnef.co/go/tools/stylecheck/analysis.go @@ -0,0 +1,111 @@ +package stylecheck + +import ( + "flag" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/inspect" + "honnef.co/go/tools/config" + "honnef.co/go/tools/facts" + "honnef.co/go/tools/internal/passes/buildssa" + "honnef.co/go/tools/lint/lintutil" +) + +func newFlagSet() flag.FlagSet { + fs := flag.NewFlagSet("", flag.PanicOnError) + fs.Var(lintutil.NewVersionFlag(), "go", "Target Go version") + return *fs +} + +var Analyzers = map[string]*analysis.Analyzer{ + "ST1000": { + Name: "ST1000", + Run: CheckPackageComment, + Doc: Docs["ST1000"].String(), + Requires: []*analysis.Analyzer{}, + Flags: newFlagSet(), + }, + "ST1001": { + Name: "ST1001", + Run: CheckDotImports, + Doc: Docs["ST1001"].String(), + Requires: []*analysis.Analyzer{facts.Generated, config.Analyzer}, + Flags: newFlagSet(), + }, + "ST1003": { + Name: "ST1003", + Run: CheckNames, + Doc: Docs["ST1003"].String(), + Requires: []*analysis.Analyzer{facts.Generated, config.Analyzer}, + Flags: newFlagSet(), + }, + "ST1005": { + Name: "ST1005", + Run: CheckErrorStrings, + Doc: Docs["ST1005"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + Flags: newFlagSet(), + }, + "ST1006": { + Name: "ST1006", + Run: CheckReceiverNames, + Doc: Docs["ST1006"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer, facts.Generated}, + Flags: newFlagSet(), + }, + "ST1008": { + Name: "ST1008", + Run: CheckErrorReturn, + Doc: Docs["ST1008"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + Flags: newFlagSet(), + }, + "ST1011": { + Name: "ST1011", + Run: CheckTimeNames, + Doc: Docs["ST1011"].String(), + Flags: newFlagSet(), + }, + "ST1012": { + Name: "ST1012", + Run: CheckErrorVarNames, + Doc: Docs["ST1012"].String(), + Requires: []*analysis.Analyzer{config.Analyzer}, + Flags: newFlagSet(), + }, + "ST1013": { + Name: "ST1013", + Run: CheckHTTPStatusCodes, + Doc: Docs["ST1013"].String(), + Requires: []*analysis.Analyzer{facts.Generated, facts.TokenFile, config.Analyzer}, + Flags: newFlagSet(), + }, + "ST1015": { + Name: "ST1015", + Run: CheckDefaultCaseOrder, + Doc: Docs["ST1015"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated, facts.TokenFile}, + Flags: newFlagSet(), + }, + "ST1016": { + Name: "ST1016", + Run: CheckReceiverNamesIdentical, + Doc: Docs["ST1016"].String(), + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + Flags: newFlagSet(), + }, + "ST1017": { + Name: "ST1017", + Run: CheckYodaConditions, + Doc: Docs["ST1017"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer, facts.Generated, facts.TokenFile}, + Flags: newFlagSet(), + }, + "ST1018": { + Name: "ST1018", + Run: CheckInvisibleCharacters, + Doc: Docs["ST1018"].String(), + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Flags: newFlagSet(), + }, +} diff --git a/vendor/honnef.co/go/tools/stylecheck/doc.go b/vendor/honnef.co/go/tools/stylecheck/doc.go new file mode 100644 index 000000000000..9097214d9bf5 --- /dev/null +++ b/vendor/honnef.co/go/tools/stylecheck/doc.go @@ -0,0 +1,154 @@ +package stylecheck + +import "honnef.co/go/tools/lint" + +var Docs = map[string]*lint.Documentation{ + "ST1000": &lint.Documentation{ + Title: `Incorrect or missing package comment`, + Text: `Packages must have a package comment that is formatted according to +the guidelines laid out in +https://github.com/golang/go/wiki/CodeReviewComments#package-comments.`, + Since: "2019.1", + NonDefault: true, + }, + + "ST1001": &lint.Documentation{ + Title: `Dot imports are discouraged`, + Text: `Dot imports that aren't in external test packages are discouraged. + +The dot_import_whitelist option can be used to whitelist certain +imports. + +Quoting Go Code Review Comments: + + The import . form can be useful in tests that, due to circular + dependencies, cannot be made part of the package being tested: + + package foo_test + + import ( + "bar/testutil" // also imports "foo" + . "foo" + ) + + In this case, the test file cannot be in package foo because it + uses bar/testutil, which imports foo. So we use the 'import .' + form to let the file pretend to be part of package foo even though + it is not. Except for this one case, do not use import . in your + programs. It makes the programs much harder to read because it is + unclear whether a name like Quux is a top-level identifier in the + current package or in an imported package.`, + Since: "2019.1", + Options: []string{"dot_import_whitelist"}, + }, + + "ST1003": &lint.Documentation{ + Title: `Poorly chosen identifier`, + Text: `Identifiers, such as variable and package names, follow certain rules. + +See the following links for details: + +- https://golang.org/doc/effective_go.html#package-names +- https://golang.org/doc/effective_go.html#mixed-caps +- https://github.com/golang/go/wiki/CodeReviewComments#initialisms +- https://github.com/golang/go/wiki/CodeReviewComments#variable-names`, + Since: "2019.1", + NonDefault: true, + Options: []string{"initialisms"}, + }, + + "ST1005": &lint.Documentation{ + Title: `Incorrectly formatted error string`, + Text: `Error strings follow a set of guidelines to ensure uniformity and good +composability. + +Quoting Go Code Review Comments: + + Error strings should not be capitalized (unless beginning with + proper nouns or acronyms) or end with punctuation, since they are + usually printed following other context. That is, use + fmt.Errorf("something bad") not fmt.Errorf("Something bad"), so + that log.Printf("Reading %s: %v", filename, err) formats without a + spurious capital letter mid-message.`, + Since: "2019.1", + }, + + "ST1006": &lint.Documentation{ + Title: `Poorly chosen receiver name`, + Text: `Quoting Go Code Review Comments: + + The name of a method's receiver should be a reflection of its + identity; often a one or two letter abbreviation of its type + suffices (such as "c" or "cl" for "Client"). Don't use generic + names such as "me", "this" or "self", identifiers typical of + object-oriented languages that place more emphasis on methods as + opposed to functions. The name need not be as descriptive as that + of a method argument, as its role is obvious and serves no + documentary purpose. It can be very short as it will appear on + almost every line of every method of the type; familiarity admits + brevity. Be consistent, too: if you call the receiver "c" in one + method, don't call it "cl" in another.`, + Since: "2019.1", + }, + + "ST1008": &lint.Documentation{ + Title: `A function's error value should be its last return value`, + Text: `A function's error value should be its last return value.`, + Since: `2019.1`, + }, + + "ST1011": &lint.Documentation{ + Title: `Poorly chosen name for variable of type time.Duration`, + Text: `time.Duration values represent an amount of time, which is represented +as a count of nanoseconds. An expression like 5 * time.Microsecond +yields the value 5000. It is therefore not appropriate to suffix a +variable of type time.Duration with any time unit, such as Msec or +Milli.`, + Since: `2019.1`, + }, + + "ST1012": &lint.Documentation{ + Title: `Poorly chosen name for error variable`, + Text: `Error variables that are part of an API should be called errFoo or +ErrFoo.`, + Since: "2019.1", + }, + + "ST1013": &lint.Documentation{ + Title: `Should use constants for HTTP error codes, not magic numbers`, + Text: `HTTP has a tremendous number of status codes. While some of those are +well known (200, 400, 404, 500), most of them are not. The net/http +package provides constants for all status codes that are part of the +various specifications. It is recommended to use these constants +instead of hard-coding magic numbers, to vastly improve the +readability of your code.`, + Since: "2019.1", + Options: []string{"http_status_code_whitelist"}, + }, + + "ST1015": &lint.Documentation{ + Title: `A switch's default case should be the first or last case`, + Since: "2019.1", + }, + + "ST1016": &lint.Documentation{ + Title: `Use consistent method receiver names`, + Since: "2019.1", + NonDefault: true, + }, + + "ST1017": &lint.Documentation{ + Title: `Don't use Yoda conditions`, + Text: `Yoda conditions are conditions of the kind 'if 42 == x', where the +literal is on the left side of the comparison. These are a common +idiom in languages in which assignment is an expression, to avoid bugs +of the kind 'if (x = 42)'. In Go, which doesn't allow for this kind of +bug, we prefer the more idiomatic 'if x == 42'.`, + Since: "2019.2", + }, + + "ST1018": &lint.Documentation{ + Title: `Avoid zero-width and control characters in string literals`, + Since: "2019.2", + }, +} diff --git a/vendor/github.com/golangci/go-tools/stylecheck/lint.go b/vendor/honnef.co/go/tools/stylecheck/lint.go similarity index 50% rename from vendor/github.com/golangci/go-tools/stylecheck/lint.go rename to vendor/honnef.co/go/tools/stylecheck/lint.go index 620cacf3e5d1..1699d5898c07 100644 --- a/vendor/github.com/golangci/go-tools/stylecheck/lint.go +++ b/vendor/honnef.co/go/tools/stylecheck/lint.go @@ -1,4 +1,4 @@ -package stylecheck // import "github.com/golangci/go-tools/stylecheck" +package stylecheck // import "honnef.co/go/tools/stylecheck" import ( "fmt" @@ -11,47 +11,18 @@ import ( "unicode" "unicode/utf8" - "github.com/golangci/go-tools/lint" - . "github.com/golangci/go-tools/lint/lintdsl" - "github.com/golangci/go-tools/ssa" + "honnef.co/go/tools/config" + "honnef.co/go/tools/internal/passes/buildssa" + . "honnef.co/go/tools/lint/lintdsl" + "honnef.co/go/tools/ssa" + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/inspect" + "golang.org/x/tools/go/ast/inspector" "golang.org/x/tools/go/types/typeutil" ) -type Checker struct { - CheckGenerated bool -} - -func NewChecker() *Checker { - return &Checker{} -} - -func (*Checker) Name() string { return "stylecheck" } -func (*Checker) Prefix() string { return "ST" } -func (c *Checker) Init(prog *lint.Program) {} - -func (c *Checker) Checks() []lint.Check { - return []lint.Check{ - {ID: "ST1000", FilterGenerated: false, Fn: c.CheckPackageComment}, - {ID: "ST1001", FilterGenerated: true, Fn: c.CheckDotImports}, - // {ID: "ST1002", FilterGenerated: true, Fn: c.CheckBlankImports}, - {ID: "ST1003", FilterGenerated: true, Fn: c.CheckNames}, - // {ID: "ST1004", FilterGenerated: false, Fn: nil, }, - {ID: "ST1005", FilterGenerated: false, Fn: c.CheckErrorStrings}, - {ID: "ST1006", FilterGenerated: false, Fn: c.CheckReceiverNames}, - // {ID: "ST1007", FilterGenerated: true, Fn: c.CheckIncDec}, - {ID: "ST1008", FilterGenerated: false, Fn: c.CheckErrorReturn}, - // {ID: "ST1009", FilterGenerated: false, Fn: c.CheckUnexportedReturn}, - // {ID: "ST1010", FilterGenerated: false, Fn: c.CheckContextFirstArg}, - {ID: "ST1011", FilterGenerated: false, Fn: c.CheckTimeNames}, - {ID: "ST1012", FilterGenerated: false, Fn: c.CheckErrorVarNames}, - {ID: "ST1013", FilterGenerated: true, Fn: c.CheckHTTPStatusCodes}, - {ID: "ST1015", FilterGenerated: true, Fn: c.CheckDefaultCaseOrder}, - {ID: "ST1016", FilterGenerated: false, Fn: c.CheckReceiverNamesIdentical}, - } -} - -func (c *Checker) CheckPackageComment(j *lint.Job) { +func CheckPackageComment(pass *analysis.Pass) (interface{}, error) { // - At least one file in a non-main package should have a package comment // // - The comment should be of the form @@ -60,61 +31,59 @@ func (c *Checker) CheckPackageComment(j *lint.Job) { // which case they get appended. But that doesn't happen a lot in // the real world. - for _, pkg := range j.Program.InitialPackages { - if pkg.Name == "main" { + if pass.Pkg.Name() == "main" { + return nil, nil + } + hasDocs := false + for _, f := range pass.Files { + if IsInTest(pass, f) { continue } - hasDocs := false - for _, f := range pkg.Syntax { - if IsInTest(j, f) { - continue - } - if f.Doc != nil && len(f.Doc.List) > 0 { - hasDocs = true - prefix := "Package " + f.Name.Name + " " - if !strings.HasPrefix(strings.TrimSpace(f.Doc.Text()), prefix) { - j.Errorf(f.Doc, `package comment should be of the form "%s..."`, prefix) - } - f.Doc.Text() + if f.Doc != nil && len(f.Doc.List) > 0 { + hasDocs = true + prefix := "Package " + f.Name.Name + " " + if !strings.HasPrefix(strings.TrimSpace(f.Doc.Text()), prefix) { + ReportNodef(pass, f.Doc, `package comment should be of the form "%s..."`, prefix) } + f.Doc.Text() } + } - if !hasDocs { - for _, f := range pkg.Syntax { - if IsInTest(j, f) { - continue - } - j.Errorf(f, "at least one file in a package should have a package comment") + if !hasDocs { + for _, f := range pass.Files { + if IsInTest(pass, f) { + continue } + ReportNodef(pass, f, "at least one file in a package should have a package comment") } } + return nil, nil } -func (c *Checker) CheckDotImports(j *lint.Job) { - for _, pkg := range j.Program.InitialPackages { - for _, f := range pkg.Syntax { - imports: - for _, imp := range f.Imports { - path := imp.Path.Value - path = path[1 : len(path)-1] - for _, w := range pkg.Config.DotImportWhitelist { - if w == path { - continue imports - } +func CheckDotImports(pass *analysis.Pass) (interface{}, error) { + for _, f := range pass.Files { + imports: + for _, imp := range f.Imports { + path := imp.Path.Value + path = path[1 : len(path)-1] + for _, w := range config.For(pass).DotImportWhitelist { + if w == path { + continue imports } + } - if imp.Name != nil && imp.Name.Name == "." && !IsInTest(j, f) { - j.Errorf(imp, "should not use dot imports") - } + if imp.Name != nil && imp.Name.Name == "." && !IsInTest(pass, f) { + ReportNodefFG(pass, imp, "should not use dot imports") } } } + return nil, nil } -func (c *Checker) CheckBlankImports(j *lint.Job) { - fset := j.Program.Fset() - for _, f := range j.Program.Files { - if IsInMain(j, f) || IsInTest(j, f) { +func CheckBlankImports(pass *analysis.Pass) (interface{}, error) { + fset := pass.Fset + for _, f := range pass.Files { + if IsInMain(pass, f) || IsInTest(pass, f) { continue } @@ -163,27 +132,28 @@ func (c *Checker) CheckBlankImports(j *lint.Job) { } if imp.Doc == nil && imp.Comment == nil && !skip[imp] { - j.Errorf(imp, "a blank import should be only in a main or test package, or have a comment justifying it") + ReportNodef(pass, imp, "a blank import should be only in a main or test package, or have a comment justifying it") } } } + return nil, nil } -func (c *Checker) CheckIncDec(j *lint.Job) { +func CheckIncDec(pass *analysis.Pass) (interface{}, error) { // TODO(dh): this can be noisy for function bodies that look like this: // x += 3 // ... // x += 2 // ... // x += 1 - fn := func(node ast.Node) bool { - assign, ok := node.(*ast.AssignStmt) - if !ok || (assign.Tok != token.ADD_ASSIGN && assign.Tok != token.SUB_ASSIGN) { - return true + fn := func(node ast.Node) { + assign := node.(*ast.AssignStmt) + if assign.Tok != token.ADD_ASSIGN && assign.Tok != token.SUB_ASSIGN { + return } if (len(assign.Lhs) != 1 || len(assign.Rhs) != 1) || !IsIntLiteral(assign.Rhs[0], "1") { - return true + return } suffix := "" @@ -194,17 +164,15 @@ func (c *Checker) CheckIncDec(j *lint.Job) { suffix = "--" } - j.Errorf(assign, "should replace %s with %s%s", Render(j, assign), Render(j, assign.Lhs[0]), suffix) - return true - } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) + ReportNodef(pass, assign, "should replace %s with %s%s", Render(pass, assign), Render(pass, assign.Lhs[0]), suffix) } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.AssignStmt)(nil)}, fn) + return nil, nil } -func (c *Checker) CheckErrorReturn(j *lint.Job) { +func CheckErrorReturn(pass *analysis.Pass) (interface{}, error) { fnLoop: - for _, fn := range j.Program.InitialFunctions { + for _, fn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { sig := fn.Type().(*types.Signature) rets := sig.Results() if rets == nil || rets.Len() < 2 { @@ -218,21 +186,22 @@ fnLoop: } for i := rets.Len() - 2; i >= 0; i-- { if rets.At(i).Type() == types.Universe.Lookup("error").Type() { - j.Errorf(rets.At(i), "error should be returned as the last argument") + pass.Reportf(rets.At(i).Pos(), "error should be returned as the last argument") continue fnLoop } } } + return nil, nil } // CheckUnexportedReturn checks that exported functions on exported // types do not return unexported types. -func (c *Checker) CheckUnexportedReturn(j *lint.Job) { - for _, fn := range j.Program.InitialFunctions { +func CheckUnexportedReturn(pass *analysis.Pass) (interface{}, error) { + for _, fn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { if fn.Synthetic != "" || fn.Parent() != nil { continue } - if !ast.IsExported(fn.Name()) || IsInMain(j, fn) || IsInTest(j, fn) { + if !ast.IsExported(fn.Name()) || IsInMain(pass, fn) || IsInTest(pass, fn) { continue } sig := fn.Type().(*types.Signature) @@ -244,77 +213,78 @@ func (c *Checker) CheckUnexportedReturn(j *lint.Job) { if named, ok := DereferenceR(res.At(i).Type()).(*types.Named); ok && !ast.IsExported(named.Obj().Name()) && named != types.Universe.Lookup("error").Type() { - j.Errorf(fn, "should not return unexported type") + pass.Reportf(fn.Pos(), "should not return unexported type") } } } + return nil, nil } -func (c *Checker) CheckReceiverNames(j *lint.Job) { - for _, pkg := range j.Program.InitialPackages { - for _, m := range pkg.SSA.Members { - if T, ok := m.Object().(*types.TypeName); ok && !T.IsAlias() { - ms := typeutil.IntuitiveMethodSet(T.Type(), nil) - for _, sel := range ms { - fn := sel.Obj().(*types.Func) - recv := fn.Type().(*types.Signature).Recv() - if Dereference(recv.Type()) != T.Type() { - // skip embedded methods - continue - } - if recv.Name() == "self" || recv.Name() == "this" { - j.Errorf(recv, `receiver name should be a reflection of its identity; don't use generic names such as "this" or "self"`) - } - if recv.Name() == "_" { - j.Errorf(recv, "receiver name should not be an underscore, omit the name if it is unused") - } +func CheckReceiverNames(pass *analysis.Pass) (interface{}, error) { + ssapkg := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).Pkg + for _, m := range ssapkg.Members { + if T, ok := m.Object().(*types.TypeName); ok && !T.IsAlias() { + ms := typeutil.IntuitiveMethodSet(T.Type(), nil) + for _, sel := range ms { + fn := sel.Obj().(*types.Func) + recv := fn.Type().(*types.Signature).Recv() + if Dereference(recv.Type()) != T.Type() { + // skip embedded methods + continue + } + if recv.Name() == "self" || recv.Name() == "this" { + ReportfFG(pass, recv.Pos(), `receiver name should be a reflection of its identity; don't use generic names such as "this" or "self"`) + } + if recv.Name() == "_" { + ReportfFG(pass, recv.Pos(), "receiver name should not be an underscore, omit the name if it is unused") } } } } + return nil, nil } -func (c *Checker) CheckReceiverNamesIdentical(j *lint.Job) { - for _, pkg := range j.Program.InitialPackages { - for _, m := range pkg.SSA.Members { - names := map[string]int{} - - var firstFn *types.Func - if T, ok := m.Object().(*types.TypeName); ok && !T.IsAlias() { - ms := typeutil.IntuitiveMethodSet(T.Type(), nil) - for _, sel := range ms { - fn := sel.Obj().(*types.Func) - recv := fn.Type().(*types.Signature).Recv() - if Dereference(recv.Type()) != T.Type() { - // skip embedded methods - continue - } - if firstFn == nil { - firstFn = fn - } - if recv.Name() != "" && recv.Name() != "_" { - names[recv.Name()]++ - } +func CheckReceiverNamesIdentical(pass *analysis.Pass) (interface{}, error) { + ssapkg := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).Pkg + for _, m := range ssapkg.Members { + names := map[string]int{} + + var firstFn *types.Func + if T, ok := m.Object().(*types.TypeName); ok && !T.IsAlias() { + ms := typeutil.IntuitiveMethodSet(T.Type(), nil) + for _, sel := range ms { + fn := sel.Obj().(*types.Func) + recv := fn.Type().(*types.Signature).Recv() + if Dereference(recv.Type()) != T.Type() { + // skip embedded methods + continue } - } - - if len(names) > 1 { - var seen []string - for name, count := range names { - seen = append(seen, fmt.Sprintf("%dx %q", count, name)) + if firstFn == nil { + firstFn = fn + } + if recv.Name() != "" && recv.Name() != "_" { + names[recv.Name()]++ } + } + } - j.Errorf(firstFn, "methods on the same type should have the same receiver name (seen %s)", strings.Join(seen, ", ")) + if len(names) > 1 { + var seen []string + for name, count := range names { + seen = append(seen, fmt.Sprintf("%dx %q", count, name)) } + + pass.Reportf(firstFn.Pos(), "methods on the same type should have the same receiver name (seen %s)", strings.Join(seen, ", ")) } } + return nil, nil } -func (c *Checker) CheckContextFirstArg(j *lint.Job) { +func CheckContextFirstArg(pass *analysis.Pass) (interface{}, error) { // TODO(dh): this check doesn't apply to test helpers. Example from the stdlib: // func helperCommandContext(t *testing.T, ctx context.Context, s ...string) (cmd *exec.Cmd) { fnLoop: - for _, fn := range j.Program.InitialFunctions { + for _, fn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { if fn.Synthetic != "" || fn.Parent() != nil { continue } @@ -328,26 +298,29 @@ fnLoop: for i := 1; i < params.Len(); i++ { param := params.At(i) if types.TypeString(param.Type(), nil) == "context.Context" { - j.Errorf(param, "context.Context should be the first argument of a function") + pass.Reportf(param.Pos(), "context.Context should be the first argument of a function") continue fnLoop } } } + return nil, nil } -func (c *Checker) CheckErrorStrings(j *lint.Job) { - fnNames := map[*ssa.Package]map[string]bool{} - for _, fn := range j.Program.InitialFunctions { - m := fnNames[fn.Package()] - if m == nil { - m = map[string]bool{} - fnNames[fn.Package()] = m +func CheckErrorStrings(pass *analysis.Pass) (interface{}, error) { + objNames := map[*ssa.Package]map[string]bool{} + ssapkg := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).Pkg + objNames[ssapkg] = map[string]bool{} + for _, m := range ssapkg.Members { + if typ, ok := m.(*ssa.Type); ok { + objNames[ssapkg][typ.Name()] = true } - m[fn.Name()] = true + } + for _, fn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + objNames[fn.Package()][fn.Name()] = true } - for _, fn := range j.Program.InitialFunctions { - if IsInTest(j, fn) { + for _, fn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs { + if IsInTest(pass, fn) { // We don't care about malformed error messages in tests; // they're usually for direct human consumption, not part // of an API @@ -375,7 +348,7 @@ func (c *Checker) CheckErrorStrings(j *lint.Job) { } switch s[len(s)-1] { case '.', ':', '!', '\n': - j.Errorf(call, "error strings should not end with punctuation or a newline") + pass.Reportf(call.Pos(), "error strings should not end with punctuation or a newline") } idx := strings.IndexByte(s, ' ') if idx == -1 { @@ -398,8 +371,8 @@ func (c *Checker) CheckErrorStrings(j *lint.Job) { } word = strings.TrimRightFunc(word, func(r rune) bool { return unicode.IsPunct(r) }) - if fnNames[fn.Package()][word] { - // Word is probably the name of a function in this package + if objNames[fn.Package()][word] { + // Word is probably the name of a function or type in this package continue } // First word in error starts with a capital @@ -409,13 +382,14 @@ func (c *Checker) CheckErrorStrings(j *lint.Job) { // // It could still be a proper noun, though. - j.Errorf(call, "error strings should not be capitalized") + pass.Reportf(call.Pos(), "error strings should not be capitalized") } } } + return nil, nil } -func (c *Checker) CheckTimeNames(j *lint.Job) { +func CheckTimeNames(pass *analysis.Pass) (interface{}, error) { suffixes := []string{ "Sec", "Secs", "Seconds", "Msec", "Msecs", @@ -430,31 +404,32 @@ func (c *Checker) CheckTimeNames(j *lint.Job) { for _, name := range names { for _, suffix := range suffixes { if strings.HasSuffix(name.Name, suffix) { - j.Errorf(name, "var %s is of type %v; don't use unit-specific suffix %q", name.Name, T, suffix) + ReportNodef(pass, name, "var %s is of type %v; don't use unit-specific suffix %q", name.Name, T, suffix) break } } } } - for _, f := range j.Program.Files { + for _, f := range pass.Files { ast.Inspect(f, func(node ast.Node) bool { switch node := node.(type) { case *ast.ValueSpec: - T := TypeOf(j, node.Type) + T := pass.TypesInfo.TypeOf(node.Type) fn(T, node.Names) case *ast.FieldList: for _, field := range node.List { - T := TypeOf(j, field.Type) + T := pass.TypesInfo.TypeOf(field.Type) fn(T, field.Names) } } return true }) } + return nil, nil } -func (c *Checker) CheckErrorVarNames(j *lint.Job) { - for _, f := range j.Program.Files { +func CheckErrorVarNames(pass *analysis.Pass) (interface{}, error) { + for _, f := range pass.Files { for _, decl := range f.Decls { gen, ok := decl.(*ast.GenDecl) if !ok || gen.Tok != token.VAR { @@ -468,7 +443,7 @@ func (c *Checker) CheckErrorVarNames(j *lint.Job) { for i, name := range spec.Names { val := spec.Values[i] - if !IsCallToAST(j, val, "errors.New") && !IsCallToAST(j, val, "fmt.Errorf") { + if !IsCallToAST(pass, val, "errors.New") && !IsCallToAST(pass, val, "fmt.Errorf") { continue } @@ -477,12 +452,13 @@ func (c *Checker) CheckErrorVarNames(j *lint.Job) { prefix = "Err" } if !strings.HasPrefix(name.Name, prefix) { - j.Errorf(name, "error var %s should have name of the form %sFoo", name.Name, prefix) + ReportNodef(pass, name, "error var %s should have name of the form %sFoo", name.Name, prefix) } } } } } + return nil, nil } var httpStatusCodes = map[int]string{ @@ -547,72 +523,107 @@ var httpStatusCodes = map[int]string{ 511: "StatusNetworkAuthenticationRequired", } -func (c *Checker) CheckHTTPStatusCodes(j *lint.Job) { - for _, pkg := range j.Program.InitialPackages { - whitelist := map[string]bool{} - for _, code := range pkg.Config.HTTPStatusCodeWhitelist { - whitelist[code] = true +func CheckHTTPStatusCodes(pass *analysis.Pass) (interface{}, error) { + whitelist := map[string]bool{} + for _, code := range config.For(pass).HTTPStatusCodeWhitelist { + whitelist[code] = true + } + fn := func(node ast.Node) bool { + if node == nil { + return true + } + call, ok := node.(*ast.CallExpr) + if !ok { + return true } - fn := func(node ast.Node) bool { - call, ok := node.(*ast.CallExpr) - if !ok { - return true - } - - var arg int - switch CallNameAST(j, call) { - case "net/http.Error": - arg = 2 - case "net/http.Redirect": - arg = 3 - case "net/http.StatusText": - arg = 0 - case "net/http.RedirectHandler": - arg = 1 - default: - return true - } - lit, ok := call.Args[arg].(*ast.BasicLit) - if !ok { - return true - } - if whitelist[lit.Value] { - return true - } - n, err := strconv.Atoi(lit.Value) - if err != nil { - return true - } - s, ok := httpStatusCodes[n] - if !ok { - return true - } - j.Errorf(lit, "should use constant http.%s instead of numeric literal %d", s, n) + var arg int + switch CallNameAST(pass, call) { + case "net/http.Error": + arg = 2 + case "net/http.Redirect": + arg = 3 + case "net/http.StatusText": + arg = 0 + case "net/http.RedirectHandler": + arg = 1 + default: + return true + } + lit, ok := call.Args[arg].(*ast.BasicLit) + if !ok { return true } - for _, f := range pkg.Syntax { - ast.Inspect(f, fn) + if whitelist[lit.Value] { + return true } - } -} -func (c *Checker) CheckDefaultCaseOrder(j *lint.Job) { - fn := func(node ast.Node) bool { - stmt, ok := node.(*ast.SwitchStmt) + n, err := strconv.Atoi(lit.Value) + if err != nil { + return true + } + s, ok := httpStatusCodes[n] if !ok { return true } + ReportNodefFG(pass, lit, "should use constant http.%s instead of numeric literal %d", s, n) + return true + } + // OPT(dh): replace with inspector + for _, f := range pass.Files { + ast.Inspect(f, fn) + } + return nil, nil +} + +func CheckDefaultCaseOrder(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + stmt := node.(*ast.SwitchStmt) list := stmt.Body.List for i, c := range list { if c.(*ast.CaseClause).List == nil && i != 0 && i != len(list)-1 { - j.Errorf(c, "default case should be first or last in switch statement") + ReportNodefFG(pass, c, "default case should be first or last in switch statement") break } } - return true } - for _, f := range j.Program.Files { - ast.Inspect(f, fn) + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.SwitchStmt)(nil)}, fn) + return nil, nil +} + +func CheckYodaConditions(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + cond := node.(*ast.BinaryExpr) + if cond.Op != token.EQL && cond.Op != token.NEQ { + return + } + if _, ok := cond.X.(*ast.BasicLit); !ok { + return + } + if _, ok := cond.Y.(*ast.BasicLit); ok { + // Don't flag lit == lit conditions, just in case + return + } + ReportNodefFG(pass, cond, "don't use Yoda conditions") + } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.BinaryExpr)(nil)}, fn) + return nil, nil +} + +func CheckInvisibleCharacters(pass *analysis.Pass) (interface{}, error) { + fn := func(node ast.Node) { + lit := node.(*ast.BasicLit) + if lit.Kind != token.STRING { + return + } + for _, r := range lit.Value { + if unicode.Is(unicode.Cf, r) { + ReportNodef(pass, lit, "string literal contains the Unicode format character %U, consider using the %q escape sequence", r, r) + } else if unicode.Is(unicode.Cc, r) && r != '\n' && r != '\t' && r != '\r' { + ReportNodef(pass, lit, "string literal contains the Unicode control character %U, consider using the %q escape sequence", r, r) + } + } } + pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.BasicLit)(nil)}, fn) + return nil, nil } diff --git a/vendor/github.com/golangci/go-tools/stylecheck/names.go b/vendor/honnef.co/go/tools/stylecheck/names.go similarity index 54% rename from vendor/github.com/golangci/go-tools/stylecheck/names.go rename to vendor/honnef.co/go/tools/stylecheck/names.go index 342b708f1cd9..160f9d7ff71b 100644 --- a/vendor/github.com/golangci/go-tools/stylecheck/names.go +++ b/vendor/honnef.co/go/tools/stylecheck/names.go @@ -9,8 +9,9 @@ import ( "strings" "unicode" - "github.com/golangci/go-tools/lint" - . "github.com/golangci/go-tools/lint/lintdsl" + "golang.org/x/tools/go/analysis" + "honnef.co/go/tools/config" + . "honnef.co/go/tools/lint/lintdsl" ) // knownNameExceptions is a set of names that are known to be exempt from naming checks. @@ -21,7 +22,7 @@ var knownNameExceptions = map[string]bool{ "kWh": true, } -func (c *Checker) CheckNames(j *lint.Job) { +func CheckNames(pass *analysis.Pass) (interface{}, error) { // A large part of this function is copied from // github.com/golang/lint, Copyright (c) 2013 The Go Authors, // licensed under the BSD 3-clause license. @@ -45,7 +46,7 @@ func (c *Checker) CheckNames(j *lint.Job) { // Handle two common styles from other languages that don't belong in Go. if len(id.Name) >= 5 && allCaps(id.Name) && strings.Contains(id.Name, "_") { - j.Errorf(id, "should not use ALL_CAPS in Go names; use CamelCase instead") + ReportfFG(pass, id.Pos(), "should not use ALL_CAPS in Go names; use CamelCase instead") return } @@ -55,10 +56,10 @@ func (c *Checker) CheckNames(j *lint.Job) { } if len(id.Name) > 2 && strings.Contains(id.Name[1:len(id.Name)-1], "_") { - j.Errorf(id, "should not use underscores in Go names; %s %s should be %s", thing, id.Name, should) + ReportfFG(pass, id.Pos(), "should not use underscores in Go names; %s %s should be %s", thing, id.Name, should) return } - j.Errorf(id, "%s %s should be %s", thing, id.Name, should) + ReportfFG(pass, id.Pos(), "%s %s should be %s", thing, id.Name, should) } checkList := func(fl *ast.FieldList, thing string, initialisms map[string]bool) { if fl == nil { @@ -71,110 +72,110 @@ func (c *Checker) CheckNames(j *lint.Job) { } } - for _, pkg := range j.Program.InitialPackages { - initialisms := make(map[string]bool, len(pkg.Config.Initialisms)) - for _, word := range pkg.Config.Initialisms { - initialisms[word] = true + il := config.For(pass).Initialisms + initialisms := make(map[string]bool, len(il)) + for _, word := range il { + initialisms[word] = true + } + for _, f := range pass.Files { + // Package names need slightly different handling than other names. + if !strings.HasSuffix(f.Name.Name, "_test") && strings.Contains(f.Name.Name, "_") { + ReportfFG(pass, f.Pos(), "should not use underscores in package names") + } + if strings.IndexFunc(f.Name.Name, unicode.IsUpper) != -1 { + ReportfFG(pass, f.Pos(), "should not use MixedCaps in package name; %s should be %s", f.Name.Name, strings.ToLower(f.Name.Name)) } - for _, f := range pkg.Syntax { - // Package names need slightly different handling than other names. - if !strings.HasSuffix(f.Name.Name, "_test") && strings.Contains(f.Name.Name, "_") { - j.Errorf(f, "should not use underscores in package names") - } - if strings.IndexFunc(f.Name.Name, unicode.IsUpper) != -1 { - j.Errorf(f, "should not use MixedCaps in package name; %s should be %s", f.Name.Name, strings.ToLower(f.Name.Name)) - } - ast.Inspect(f, func(node ast.Node) bool { - switch v := node.(type) { - case *ast.AssignStmt: - if v.Tok != token.DEFINE { - return true - } - for _, exp := range v.Lhs { - if id, ok := exp.(*ast.Ident); ok { - check(id, "var", initialisms) - } - } - case *ast.FuncDecl: - // Functions with no body are defined elsewhere (in - // assembly, or via go:linkname). These are likely to - // be something very low level (such as the runtime), - // where our rules don't apply. - if v.Body == nil { - return true + ast.Inspect(f, func(node ast.Node) bool { + switch v := node.(type) { + case *ast.AssignStmt: + if v.Tok != token.DEFINE { + return true + } + for _, exp := range v.Lhs { + if id, ok := exp.(*ast.Ident); ok { + check(id, "var", initialisms) } + } + case *ast.FuncDecl: + // Functions with no body are defined elsewhere (in + // assembly, or via go:linkname). These are likely to + // be something very low level (such as the runtime), + // where our rules don't apply. + if v.Body == nil { + return true + } - if IsInTest(j, v) && (strings.HasPrefix(v.Name.Name, "Example") || strings.HasPrefix(v.Name.Name, "Test") || strings.HasPrefix(v.Name.Name, "Benchmark")) { - return true - } + if IsInTest(pass, v) && (strings.HasPrefix(v.Name.Name, "Example") || strings.HasPrefix(v.Name.Name, "Test") || strings.HasPrefix(v.Name.Name, "Benchmark")) { + return true + } - thing := "func" - if v.Recv != nil { - thing = "method" - } + thing := "func" + if v.Recv != nil { + thing = "method" + } - if !isTechnicallyExported(v) { - check(v.Name, thing, initialisms) - } + if !isTechnicallyExported(v) { + check(v.Name, thing, initialisms) + } - checkList(v.Type.Params, thing+" parameter", initialisms) - checkList(v.Type.Results, thing+" result", initialisms) - case *ast.GenDecl: - if v.Tok == token.IMPORT { - return true - } - var thing string - switch v.Tok { - case token.CONST: - thing = "const" - case token.TYPE: - thing = "type" - case token.VAR: - thing = "var" - } - for _, spec := range v.Specs { - switch s := spec.(type) { - case *ast.TypeSpec: - check(s.Name, thing, initialisms) - case *ast.ValueSpec: - for _, id := range s.Names { - check(id, thing, initialisms) - } - } - } - case *ast.InterfaceType: - // Do not check interface method names. - // They are often constrainted by the method names of concrete types. - for _, x := range v.Methods.List { - ft, ok := x.Type.(*ast.FuncType) - if !ok { // might be an embedded interface name - continue + checkList(v.Type.Params, thing+" parameter", initialisms) + checkList(v.Type.Results, thing+" result", initialisms) + case *ast.GenDecl: + if v.Tok == token.IMPORT { + return true + } + var thing string + switch v.Tok { + case token.CONST: + thing = "const" + case token.TYPE: + thing = "type" + case token.VAR: + thing = "var" + } + for _, spec := range v.Specs { + switch s := spec.(type) { + case *ast.TypeSpec: + check(s.Name, thing, initialisms) + case *ast.ValueSpec: + for _, id := range s.Names { + check(id, thing, initialisms) } - checkList(ft.Params, "interface method parameter", initialisms) - checkList(ft.Results, "interface method result", initialisms) - } - case *ast.RangeStmt: - if v.Tok == token.ASSIGN { - return true } - if id, ok := v.Key.(*ast.Ident); ok { - check(id, "range var", initialisms) - } - if id, ok := v.Value.(*ast.Ident); ok { - check(id, "range var", initialisms) + } + case *ast.InterfaceType: + // Do not check interface method names. + // They are often constrainted by the method names of concrete types. + for _, x := range v.Methods.List { + ft, ok := x.Type.(*ast.FuncType) + if !ok { // might be an embedded interface name + continue } - case *ast.StructType: - for _, f := range v.Fields.List { - for _, id := range f.Names { - check(id, "struct field", initialisms) - } + checkList(ft.Params, "interface method parameter", initialisms) + checkList(ft.Results, "interface method result", initialisms) + } + case *ast.RangeStmt: + if v.Tok == token.ASSIGN { + return true + } + if id, ok := v.Key.(*ast.Ident); ok { + check(id, "range var", initialisms) + } + if id, ok := v.Value.(*ast.Ident); ok { + check(id, "range var", initialisms) + } + case *ast.StructType: + for _, f := range v.Fields.List { + for _, id := range f.Names { + check(id, "struct field", initialisms) } } - return true - }) - } + } + return true + }) } + return nil, nil } // lintName returns a different name if it should be different. diff --git a/vendor/honnef.co/go/tools/unused/edge.go b/vendor/honnef.co/go/tools/unused/edge.go new file mode 100644 index 000000000000..02e0d09cf2ae --- /dev/null +++ b/vendor/honnef.co/go/tools/unused/edge.go @@ -0,0 +1,54 @@ +package unused + +//go:generate stringer -type edgeKind +type edgeKind uint64 + +func (e edgeKind) is(o edgeKind) bool { + return e&o != 0 +} + +const ( + edgeAlias edgeKind = 1 << iota + edgeBlankField + edgeAnonymousStruct + edgeCgoExported + edgeConstGroup + edgeElementType + edgeEmbeddedInterface + edgeExportedConstant + edgeExportedField + edgeExportedFunction + edgeExportedMethod + edgeExportedType + edgeExportedVariable + edgeExtendsExportedFields + edgeExtendsExportedMethodSet + edgeFieldAccess + edgeFunctionArgument + edgeFunctionResult + edgeFunctionSignature + edgeImplements + edgeInstructionOperand + edgeInterfaceCall + edgeInterfaceMethod + edgeKeyType + edgeLinkname + edgeMainFunction + edgeNamedType + edgeNetRPCRegister + edgeNoCopySentinel + edgeProvidesMethod + edgeReceiver + edgeRuntimeFunction + edgeSignature + edgeStructConversion + edgeTestSink + edgeTupleElement + edgeType + edgeTypeName + edgeUnderlyingType + edgePointerType + edgeUnsafeConversion + edgeUsedConstant + edgeVarDecl +) diff --git a/vendor/honnef.co/go/tools/unused/edgekind_string.go b/vendor/honnef.co/go/tools/unused/edgekind_string.go new file mode 100644 index 000000000000..7629636cf13b --- /dev/null +++ b/vendor/honnef.co/go/tools/unused/edgekind_string.go @@ -0,0 +1,109 @@ +// Code generated by "stringer -type edgeKind"; DO NOT EDIT. + +package unused + +import "strconv" + +func _() { + // An "invalid array index" compiler error signifies that the constant values have changed. + // Re-run the stringer command to generate them again. + var x [1]struct{} + _ = x[edgeAlias-1] + _ = x[edgeBlankField-2] + _ = x[edgeAnonymousStruct-4] + _ = x[edgeCgoExported-8] + _ = x[edgeConstGroup-16] + _ = x[edgeElementType-32] + _ = x[edgeEmbeddedInterface-64] + _ = x[edgeExportedConstant-128] + _ = x[edgeExportedField-256] + _ = x[edgeExportedFunction-512] + _ = x[edgeExportedMethod-1024] + _ = x[edgeExportedType-2048] + _ = x[edgeExportedVariable-4096] + _ = x[edgeExtendsExportedFields-8192] + _ = x[edgeExtendsExportedMethodSet-16384] + _ = x[edgeFieldAccess-32768] + _ = x[edgeFunctionArgument-65536] + _ = x[edgeFunctionResult-131072] + _ = x[edgeFunctionSignature-262144] + _ = x[edgeImplements-524288] + _ = x[edgeInstructionOperand-1048576] + _ = x[edgeInterfaceCall-2097152] + _ = x[edgeInterfaceMethod-4194304] + _ = x[edgeKeyType-8388608] + _ = x[edgeLinkname-16777216] + _ = x[edgeMainFunction-33554432] + _ = x[edgeNamedType-67108864] + _ = x[edgeNetRPCRegister-134217728] + _ = x[edgeNoCopySentinel-268435456] + _ = x[edgeProvidesMethod-536870912] + _ = x[edgeReceiver-1073741824] + _ = x[edgeRuntimeFunction-2147483648] + _ = x[edgeSignature-4294967296] + _ = x[edgeStructConversion-8589934592] + _ = x[edgeTestSink-17179869184] + _ = x[edgeTupleElement-34359738368] + _ = x[edgeType-68719476736] + _ = x[edgeTypeName-137438953472] + _ = x[edgeUnderlyingType-274877906944] + _ = x[edgePointerType-549755813888] + _ = x[edgeUnsafeConversion-1099511627776] + _ = x[edgeUsedConstant-2199023255552] + _ = x[edgeVarDecl-4398046511104] +} + +const _edgeKind_name = "edgeAliasedgeBlankFieldedgeAnonymousStructedgeCgoExportededgeConstGroupedgeElementTypeedgeEmbeddedInterfaceedgeExportedConstantedgeExportedFieldedgeExportedFunctionedgeExportedMethodedgeExportedTypeedgeExportedVariableedgeExtendsExportedFieldsedgeExtendsExportedMethodSetedgeFieldAccessedgeFunctionArgumentedgeFunctionResultedgeFunctionSignatureedgeImplementsedgeInstructionOperandedgeInterfaceCalledgeInterfaceMethodedgeKeyTypeedgeLinknameedgeMainFunctionedgeNamedTypeedgeNetRPCRegisteredgeNoCopySentineledgeProvidesMethodedgeReceiveredgeRuntimeFunctionedgeSignatureedgeStructConversionedgeTestSinkedgeTupleElementedgeTypeedgeTypeNameedgeUnderlyingTypeedgePointerTypeedgeUnsafeConversionedgeUsedConstantedgeVarDecl" + +var _edgeKind_map = map[edgeKind]string{ + 1: _edgeKind_name[0:9], + 2: _edgeKind_name[9:23], + 4: _edgeKind_name[23:42], + 8: _edgeKind_name[42:57], + 16: _edgeKind_name[57:71], + 32: _edgeKind_name[71:86], + 64: _edgeKind_name[86:107], + 128: _edgeKind_name[107:127], + 256: _edgeKind_name[127:144], + 512: _edgeKind_name[144:164], + 1024: _edgeKind_name[164:182], + 2048: _edgeKind_name[182:198], + 4096: _edgeKind_name[198:218], + 8192: _edgeKind_name[218:243], + 16384: _edgeKind_name[243:271], + 32768: _edgeKind_name[271:286], + 65536: _edgeKind_name[286:306], + 131072: _edgeKind_name[306:324], + 262144: _edgeKind_name[324:345], + 524288: _edgeKind_name[345:359], + 1048576: _edgeKind_name[359:381], + 2097152: _edgeKind_name[381:398], + 4194304: _edgeKind_name[398:417], + 8388608: _edgeKind_name[417:428], + 16777216: _edgeKind_name[428:440], + 33554432: _edgeKind_name[440:456], + 67108864: _edgeKind_name[456:469], + 134217728: _edgeKind_name[469:487], + 268435456: _edgeKind_name[487:505], + 536870912: _edgeKind_name[505:523], + 1073741824: _edgeKind_name[523:535], + 2147483648: _edgeKind_name[535:554], + 4294967296: _edgeKind_name[554:567], + 8589934592: _edgeKind_name[567:587], + 17179869184: _edgeKind_name[587:599], + 34359738368: _edgeKind_name[599:615], + 68719476736: _edgeKind_name[615:623], + 137438953472: _edgeKind_name[623:635], + 274877906944: _edgeKind_name[635:653], + 549755813888: _edgeKind_name[653:668], + 1099511627776: _edgeKind_name[668:688], + 2199023255552: _edgeKind_name[688:704], + 4398046511104: _edgeKind_name[704:715], +} + +func (i edgeKind) String() string { + if str, ok := _edgeKind_map[i]; ok { + return str + } + return "edgeKind(" + strconv.FormatInt(int64(i), 10) + ")" +} diff --git a/vendor/github.com/golangci/go-tools/unused/implements.go b/vendor/honnef.co/go/tools/unused/implements.go similarity index 79% rename from vendor/github.com/golangci/go-tools/unused/implements.go rename to vendor/honnef.co/go/tools/unused/implements.go index 78a545639631..835baac69253 100644 --- a/vendor/github.com/golangci/go-tools/unused/implements.go +++ b/vendor/honnef.co/go/tools/unused/implements.go @@ -37,43 +37,46 @@ func sameId(obj types.Object, pkg *types.Package, name string) bool { return pkg.Path() == obj.Pkg().Path() } -func (c *Checker) implements(V types.Type, T *types.Interface) bool { +func (g *Graph) implements(V types.Type, T *types.Interface, msV *types.MethodSet) ([]*types.Selection, bool) { // fast path for common case if T.Empty() { - return true + return nil, true } if ityp, _ := V.Underlying().(*types.Interface); ityp != nil { + // TODO(dh): is this code reachable? for i := 0; i < T.NumMethods(); i++ { m := T.Method(i) _, obj := lookupMethod(ityp, m.Pkg(), m.Name()) switch { case obj == nil: - return false + return nil, false case !types.Identical(obj.Type(), m.Type()): - return false + return nil, false } } - return true + return nil, true } // A concrete type implements T if it implements all methods of T. - ms := c.msCache.MethodSet(V) + var sels []*types.Selection for i := 0; i < T.NumMethods(); i++ { m := T.Method(i) - sel := ms.Lookup(m.Pkg(), m.Name()) + sel := msV.Lookup(m.Pkg(), m.Name()) if sel == nil { - return false + return nil, false } f, _ := sel.Obj().(*types.Func) if f == nil { - return false + return nil, false } if !types.Identical(f.Type(), m.Type()) { - return false + return nil, false } + + sels = append(sels, sel) } - return true + return sels, true } diff --git a/vendor/honnef.co/go/tools/unused/unused.go b/vendor/honnef.co/go/tools/unused/unused.go new file mode 100644 index 000000000000..152d3692dd8f --- /dev/null +++ b/vendor/honnef.co/go/tools/unused/unused.go @@ -0,0 +1,1964 @@ +package unused + +import ( + "fmt" + "go/ast" + "go/token" + "go/types" + "io" + "strings" + "sync" + "sync/atomic" + + "golang.org/x/tools/go/analysis" + "honnef.co/go/tools/go/types/typeutil" + "honnef.co/go/tools/internal/passes/buildssa" + "honnef.co/go/tools/lint" + "honnef.co/go/tools/lint/lintdsl" + "honnef.co/go/tools/ssa" +) + +// The graph we construct omits nodes along a path that do not +// contribute any new information to the solution. For example, the +// full graph for a function with a receiver would be Func -> +// Signature -> Var -> Type. However, since signatures cannot be +// unused, and receivers are always considered used, we can compact +// the graph down to Func -> Type. This makes the graph smaller, but +// harder to debug. + +// TODO(dh): conversions between structs mark fields as used, but the +// conversion itself isn't part of that subgraph. even if the function +// containing the conversion is unused, the fields will be marked as +// used. + +// TODO(dh): we cannot observe function calls in assembly files. + +/* + +- packages use: + - (1.1) exported named types (unless in package main) + - (1.2) exported functions (unless in package main) + - (1.3) exported variables (unless in package main) + - (1.4) exported constants (unless in package main) + - (1.5) init functions + - (1.6) functions exported to cgo + - (1.7) the main function iff in the main package + - (1.8) symbols linked via go:linkname + +- named types use: + - (2.1) exported methods + - (2.2) the type they're based on + - (2.3) all their aliases. we can't easily track uses of aliases + because go/types turns them into uses of the aliased types. assume + that if a type is used, so are all of its aliases. + - (2.4) the pointer type. this aids with eagerly implementing + interfaces. if a method that implements an interface is defined on + a pointer receiver, and the pointer type is never used, but the + named type is, then we still want to mark the method as used. + +- variables and constants use: + - their types + +- functions use: + - (4.1) all their arguments, return parameters and receivers + - (4.2) anonymous functions defined beneath them + - (4.3) closures and bound methods. + this implements a simplified model where a function is used merely by being referenced, even if it is never called. + that way we don't have to keep track of closures escaping functions. + - (4.4) functions they return. we assume that someone else will call the returned function + - (4.5) functions/interface methods they call + - types they instantiate or convert to + - (4.7) fields they access + - (4.8) types of all instructions + - (4.9) package-level variables they assign to iff in tests (sinks for benchmarks) + +- conversions use: + - (5.1) when converting between two equivalent structs, the fields in + either struct use each other. the fields are relevant for the + conversion, but only if the fields are also accessed outside the + conversion. + - (5.2) when converting to or from unsafe.Pointer, mark all fields as used. + +- structs use: + - (6.1) fields of type NoCopy sentinel + - (6.2) exported fields + - (6.3) embedded fields that help implement interfaces (either fully implements it, or contributes required methods) (recursively) + - (6.4) embedded fields that have exported methods (recursively) + - (6.5) embedded structs that have exported fields (recursively) + +- (7.1) field accesses use fields +- (7.2) fields use their types + +- (8.0) How we handle interfaces: + - (8.1) We do not technically care about interfaces that only consist of + exported methods. Exported methods on concrete types are always + marked as used. + - Any concrete type implements all known interfaces. Even if it isn't + assigned to any interfaces in our code, the user may receive a value + of the type and expect to pass it back to us through an interface. + + Concrete types use their methods that implement interfaces. If the + type is used, it uses those methods. Otherwise, it doesn't. This + way, types aren't incorrectly marked reachable through the edge + from method to type. + + - (8.3) All interface methods are marked as used, even if they never get + called. This is to accomodate sum types (unexported interface + method that must exist but never gets called.) + + - (8.4) All embedded interfaces are marked as used. This is an + extension of 8.3, but we have to explicitly track embedded + interfaces because in a chain C->B->A, B wouldn't be marked as + used by 8.3 just because it contributes A's methods to C. + +- Inherent uses: + - thunks and other generated wrappers call the real function + - (9.2) variables use their types + - (9.3) types use their underlying and element types + - (9.4) conversions use the type they convert to + - (9.5) instructions use their operands + - (9.6) instructions use their operands' types + - (9.7) variable _reads_ use variables, writes do not, except in tests + - (9.8) runtime functions that may be called from user code via the compiler + + +- const groups: + (10.1) if one constant out of a block of constants is used, mark all + of them used. a lot of the time, unused constants exist for the sake + of completeness. See also + https://github.com/dominikh/go-tools/issues/365 + + +- (11.1) anonymous struct types use all their fields. we cannot + deduplicate struct types, as that leads to order-dependent + reportings. we can't not deduplicate struct types while still + tracking fields, because then each instance of the unnamed type in + the data flow chain will get its own fields, causing false + positives. Thus, we only accurately track fields of named struct + types, and assume that unnamed struct types use all their fields. + + +- Differences in whole program mode: + - (e2) types aim to implement all exported interfaces from all packages + - (e3) exported identifiers aren't automatically used. for fields and + methods this poses extra issues due to reflection. We assume + that all exported fields are used. We also maintain a list of + known reflection-based method callers. + +*/ + +func assert(b bool) { + if !b { + panic("failed assertion") + } +} + +func typString(obj types.Object) string { + switch obj := obj.(type) { + case *types.Func: + return "func" + case *types.Var: + if obj.IsField() { + return "field" + } + return "var" + case *types.Const: + return "const" + case *types.TypeName: + return "type" + default: + return "identifier" + } +} + +// /usr/lib/go/src/runtime/proc.go:433:6: func badmorestackg0 is unused (U1000) + +// Functions defined in the Go runtime that may be called through +// compiler magic or via assembly. +var runtimeFuncs = map[string]bool{ + // The first part of the list is copied from + // cmd/compile/internal/gc/builtin.go, var runtimeDecls + "newobject": true, + "panicindex": true, + "panicslice": true, + "panicdivide": true, + "panicmakeslicelen": true, + "throwinit": true, + "panicwrap": true, + "gopanic": true, + "gorecover": true, + "goschedguarded": true, + "printbool": true, + "printfloat": true, + "printint": true, + "printhex": true, + "printuint": true, + "printcomplex": true, + "printstring": true, + "printpointer": true, + "printiface": true, + "printeface": true, + "printslice": true, + "printnl": true, + "printsp": true, + "printlock": true, + "printunlock": true, + "concatstring2": true, + "concatstring3": true, + "concatstring4": true, + "concatstring5": true, + "concatstrings": true, + "cmpstring": true, + "intstring": true, + "slicebytetostring": true, + "slicebytetostringtmp": true, + "slicerunetostring": true, + "stringtoslicebyte": true, + "stringtoslicerune": true, + "slicecopy": true, + "slicestringcopy": true, + "decoderune": true, + "countrunes": true, + "convI2I": true, + "convT16": true, + "convT32": true, + "convT64": true, + "convTstring": true, + "convTslice": true, + "convT2E": true, + "convT2Enoptr": true, + "convT2I": true, + "convT2Inoptr": true, + "assertE2I": true, + "assertE2I2": true, + "assertI2I": true, + "assertI2I2": true, + "panicdottypeE": true, + "panicdottypeI": true, + "panicnildottype": true, + "ifaceeq": true, + "efaceeq": true, + "fastrand": true, + "makemap64": true, + "makemap": true, + "makemap_small": true, + "mapaccess1": true, + "mapaccess1_fast32": true, + "mapaccess1_fast64": true, + "mapaccess1_faststr": true, + "mapaccess1_fat": true, + "mapaccess2": true, + "mapaccess2_fast32": true, + "mapaccess2_fast64": true, + "mapaccess2_faststr": true, + "mapaccess2_fat": true, + "mapassign": true, + "mapassign_fast32": true, + "mapassign_fast32ptr": true, + "mapassign_fast64": true, + "mapassign_fast64ptr": true, + "mapassign_faststr": true, + "mapiterinit": true, + "mapdelete": true, + "mapdelete_fast32": true, + "mapdelete_fast64": true, + "mapdelete_faststr": true, + "mapiternext": true, + "mapclear": true, + "makechan64": true, + "makechan": true, + "chanrecv1": true, + "chanrecv2": true, + "chansend1": true, + "closechan": true, + "writeBarrier": true, + "typedmemmove": true, + "typedmemclr": true, + "typedslicecopy": true, + "selectnbsend": true, + "selectnbrecv": true, + "selectnbrecv2": true, + "selectsetpc": true, + "selectgo": true, + "block": true, + "makeslice": true, + "makeslice64": true, + "growslice": true, + "memmove": true, + "memclrNoHeapPointers": true, + "memclrHasPointers": true, + "memequal": true, + "memequal8": true, + "memequal16": true, + "memequal32": true, + "memequal64": true, + "memequal128": true, + "int64div": true, + "uint64div": true, + "int64mod": true, + "uint64mod": true, + "float64toint64": true, + "float64touint64": true, + "float64touint32": true, + "int64tofloat64": true, + "uint64tofloat64": true, + "uint32tofloat64": true, + "complex128div": true, + "racefuncenter": true, + "racefuncenterfp": true, + "racefuncexit": true, + "raceread": true, + "racewrite": true, + "racereadrange": true, + "racewriterange": true, + "msanread": true, + "msanwrite": true, + "x86HasPOPCNT": true, + "x86HasSSE41": true, + "arm64HasATOMICS": true, + + // The second part of the list is extracted from assembly code in + // the standard library, with the exception of the runtime package itself + "abort": true, + "aeshashbody": true, + "args": true, + "asminit": true, + "badctxt": true, + "badmcall2": true, + "badmcall": true, + "badmorestackg0": true, + "badmorestackgsignal": true, + "badsignal2": true, + "callbackasm1": true, + "callCfunction": true, + "cgocallback_gofunc": true, + "cgocallbackg": true, + "checkgoarm": true, + "check": true, + "debugCallCheck": true, + "debugCallWrap": true, + "emptyfunc": true, + "entersyscall": true, + "exit": true, + "exits": true, + "exitsyscall": true, + "externalthreadhandler": true, + "findnull": true, + "goexit1": true, + "gostring": true, + "i386_set_ldt": true, + "_initcgo": true, + "init_thread_tls": true, + "ldt0setup": true, + "libpreinit": true, + "load_g": true, + "morestack": true, + "mstart": true, + "nacl_sysinfo": true, + "nanotimeQPC": true, + "nanotime": true, + "newosproc0": true, + "newproc": true, + "newstack": true, + "noted": true, + "nowQPC": true, + "osinit": true, + "printf": true, + "racecallback": true, + "reflectcallmove": true, + "reginit": true, + "rt0_go": true, + "save_g": true, + "schedinit": true, + "setldt": true, + "settls": true, + "sighandler": true, + "sigprofNonGo": true, + "sigtrampgo": true, + "_sigtramp": true, + "sigtramp": true, + "stackcheck": true, + "syscall_chdir": true, + "syscall_chroot": true, + "syscall_close": true, + "syscall_dup2": true, + "syscall_execve": true, + "syscall_exit": true, + "syscall_fcntl": true, + "syscall_forkx": true, + "syscall_gethostname": true, + "syscall_getpid": true, + "syscall_ioctl": true, + "syscall_pipe": true, + "syscall_rawsyscall6": true, + "syscall_rawSyscall6": true, + "syscall_rawsyscall": true, + "syscall_RawSyscall": true, + "syscall_rawsysvicall6": true, + "syscall_setgid": true, + "syscall_setgroups": true, + "syscall_setpgid": true, + "syscall_setsid": true, + "syscall_setuid": true, + "syscall_syscall6": true, + "syscall_syscall": true, + "syscall_Syscall": true, + "syscall_sysvicall6": true, + "syscall_wait4": true, + "syscall_write": true, + "traceback": true, + "tstart": true, + "usplitR0": true, + "wbBufFlush": true, + "write": true, +} + +type pkg struct { + Fset *token.FileSet + Files []*ast.File + Pkg *types.Package + TypesInfo *types.Info + TypesSizes types.Sizes + SSA *ssa.Package + SrcFuncs []*ssa.Function +} + +type Checker struct { + WholeProgram bool + Debug io.Writer + + mu sync.Mutex + initialPackages map[*types.Package]struct{} + allPackages map[*types.Package]struct{} + graph *Graph +} + +func NewChecker(wholeProgram bool) *Checker { + return &Checker{ + initialPackages: map[*types.Package]struct{}{}, + allPackages: map[*types.Package]struct{}{}, + WholeProgram: wholeProgram, + } +} + +func (c *Checker) Analyzer() *analysis.Analyzer { + name := "U1000" + if c.WholeProgram { + name = "U1001" + } + return &analysis.Analyzer{ + Name: name, + Doc: "Unused code", + Run: c.Run, + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + } +} + +func (c *Checker) Run(pass *analysis.Pass) (interface{}, error) { + c.mu.Lock() + if c.graph == nil { + c.graph = NewGraph() + c.graph.wholeProgram = c.WholeProgram + c.graph.fset = pass.Fset + } + + var visit func(pkg *types.Package) + visit = func(pkg *types.Package) { + if _, ok := c.allPackages[pkg]; ok { + return + } + c.allPackages[pkg] = struct{}{} + for _, imp := range pkg.Imports() { + visit(imp) + } + } + visit(pass.Pkg) + + c.initialPackages[pass.Pkg] = struct{}{} + c.mu.Unlock() + + ssapkg := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA) + pkg := &pkg{ + Fset: pass.Fset, + Files: pass.Files, + Pkg: pass.Pkg, + TypesInfo: pass.TypesInfo, + TypesSizes: pass.TypesSizes, + SSA: ssapkg.Pkg, + SrcFuncs: ssapkg.SrcFuncs, + } + + c.processPkg(c.graph, pkg) + + return nil, nil +} + +func (c *Checker) ProblemObject(fset *token.FileSet, obj types.Object) lint.Problem { + name := obj.Name() + if sig, ok := obj.Type().(*types.Signature); ok && sig.Recv() != nil { + switch sig.Recv().Type().(type) { + case *types.Named, *types.Pointer: + typ := types.TypeString(sig.Recv().Type(), func(*types.Package) string { return "" }) + if len(typ) > 0 && typ[0] == '*' { + name = fmt.Sprintf("(%s).%s", typ, obj.Name()) + } else if len(typ) > 0 { + name = fmt.Sprintf("%s.%s", typ, obj.Name()) + } + } + } + + checkName := "U1000" + if c.WholeProgram { + checkName = "U1001" + } + return lint.Problem{ + Pos: lint.DisplayPosition(fset, obj.Pos()), + Message: fmt.Sprintf("%s %s is unused", typString(obj), name), + Check: checkName, + } +} + +func (c *Checker) Result() []types.Object { + out := c.results() + + out2 := make([]types.Object, 0, len(out)) + for _, v := range out { + if _, ok := c.initialPackages[v.Pkg()]; !ok { + continue + } + out2 = append(out2, v) + } + + return out2 +} + +func (c *Checker) debugf(f string, v ...interface{}) { + if c.Debug != nil { + fmt.Fprintf(c.Debug, f, v...) + } +} + +func (graph *Graph) quieten(node *Node) { + if node.seen { + return + } + switch obj := node.obj.(type) { + case *types.Named: + for i := 0; i < obj.NumMethods(); i++ { + m := obj.Method(i) + if node, ok := graph.nodeMaybe(m); ok { + node.quiet = true + } + } + case *types.Struct: + for i := 0; i < obj.NumFields(); i++ { + if node, ok := graph.nodeMaybe(obj.Field(i)); ok { + node.quiet = true + } + } + case *types.Interface: + for i := 0; i < obj.NumExplicitMethods(); i++ { + m := obj.ExplicitMethod(i) + if node, ok := graph.nodeMaybe(m); ok { + node.quiet = true + } + } + } +} + +func (c *Checker) results() []types.Object { + if c.graph == nil { + // We never analyzed any packages + return nil + } + + var out []types.Object + + if c.WholeProgram { + var ifaces []*types.Interface + var notIfaces []types.Type + + // implement as many interfaces as possible + c.graph.seenTypes.Iterate(func(t types.Type, _ interface{}) { + switch t := t.(type) { + case *types.Interface: + if t.NumMethods() > 0 { + ifaces = append(ifaces, t) + } + default: + if _, ok := t.Underlying().(*types.Interface); !ok { + notIfaces = append(notIfaces, t) + } + } + }) + + for pkg := range c.allPackages { + for _, iface := range interfacesFromExportData(pkg) { + if iface.NumMethods() > 0 { + ifaces = append(ifaces, iface) + } + } + } + + ctx := &context{ + g: c.graph, + seenTypes: &c.graph.seenTypes, + } + // (8.0) handle interfaces + // (e2) types aim to implement all exported interfaces from all packages + for _, t := range notIfaces { + // OPT(dh): it is unfortunate that we do not have access + // to a populated method set at this point. + ms := types.NewMethodSet(t) + for _, iface := range ifaces { + if sels, ok := c.graph.implements(t, iface, ms); ok { + for _, sel := range sels { + c.graph.useMethod(ctx, t, sel, t, edgeImplements) + } + } + } + } + } + + if c.Debug != nil { + debugNode := func(node *Node) { + if node.obj == nil { + c.debugf("n%d [label=\"Root\"];\n", node.id) + } else { + c.debugf("n%d [label=%q];\n", node.id, fmt.Sprintf("(%T) %s", node.obj, node.obj)) + } + for _, e := range node.used { + for i := edgeKind(1); i < 64; i++ { + if e.kind.is(1 << i) { + c.debugf("n%d -> n%d [label=%q];\n", node.id, e.node.id, edgeKind(1< 1 { + cg := &ConstGroup{} + ctx.see(cg) + for _, spec := range specs { + for _, name := range spec.(*ast.ValueSpec).Names { + obj := pkg.TypesInfo.ObjectOf(name) + // (10.1) const groups + ctx.seeAndUse(obj, cg, edgeConstGroup) + ctx.use(cg, obj, edgeConstGroup) + } + } + } + } + case token.VAR: + for _, spec := range n.Specs { + v := spec.(*ast.ValueSpec) + for _, name := range v.Names { + T := pkg.TypesInfo.TypeOf(name) + if fn != nil { + ctx.seeAndUse(T, fn, edgeVarDecl) + } else { + // TODO(dh): we likely want to make + // the type used by the variable, not + // the package containing the + // variable. But then we have to take + // special care of blank identifiers. + ctx.seeAndUse(T, nil, edgeVarDecl) + } + g.typ(ctx, T, nil) + } + } + case token.TYPE: + for _, spec := range n.Specs { + // go/types doesn't provide a way to go from a + // types.Named to the named type it was based on + // (the t1 in type t2 t1). Therefore we walk the + // AST and process GenDecls. + // + // (2.2) named types use the type they're based on + v := spec.(*ast.TypeSpec) + T := pkg.TypesInfo.TypeOf(v.Type) + obj := pkg.TypesInfo.ObjectOf(v.Name) + ctx.see(obj) + ctx.see(T) + ctx.use(T, obj, edgeType) + g.typ(ctx, obj.Type(), nil) + g.typ(ctx, T, nil) + + if v.Assign != 0 { + aliasFor := obj.(*types.TypeName).Type() + // (2.3) named types use all their aliases. we can't easily track uses of aliases + if isIrrelevant(aliasFor) { + // We do not track the type this is an + // alias for (for example builtins), so + // just mark the alias used. + // + // FIXME(dh): what about aliases declared inside functions? + ctx.use(obj, nil, edgeAlias) + } else { + ctx.see(aliasFor) + ctx.seeAndUse(obj, aliasFor, edgeAlias) + } + } + } + } + } + return true + }) + } + + for _, m := range pkg.SSA.Members { + switch m := m.(type) { + case *ssa.NamedConst: + // nothing to do, we collect all constants from Defs + case *ssa.Global: + if m.Object() != nil { + ctx.see(m.Object()) + if g.trackExportedIdentifier(ctx, m.Object()) { + // (1.3) packages use exported variables (unless in package main) + ctx.use(m.Object(), nil, edgeExportedVariable) + } + } + case *ssa.Function: + mObj := owningObject(m) + if mObj != nil { + ctx.see(mObj) + } + //lint:ignore SA9003 handled implicitly + if m.Name() == "init" { + // (1.5) packages use init functions + // + // This is handled implicitly. The generated init + // function has no object, thus everything in it will + // be owned by the package. + } + // This branch catches top-level functions, not methods. + if m.Object() != nil && g.trackExportedIdentifier(ctx, m.Object()) { + // (1.2) packages use exported functions (unless in package main) + ctx.use(mObj, nil, edgeExportedFunction) + } + if m.Name() == "main" && pkg.Pkg.Name() == "main" { + // (1.7) packages use the main function iff in the main package + ctx.use(mObj, nil, edgeMainFunction) + } + if pkg.Pkg.Path() == "runtime" && runtimeFuncs[m.Name()] { + // (9.8) runtime functions that may be called from user code via the compiler + ctx.use(mObj, nil, edgeRuntimeFunction) + } + if m.Syntax() != nil { + doc := m.Syntax().(*ast.FuncDecl).Doc + if doc != nil { + for _, cmt := range doc.List { + if strings.HasPrefix(cmt.Text, "//go:cgo_export_") { + // (1.6) packages use functions exported to cgo + ctx.use(mObj, nil, edgeCgoExported) + } + } + } + } + g.function(ctx, m) + case *ssa.Type: + if m.Object() != nil { + ctx.see(m.Object()) + if g.trackExportedIdentifier(ctx, m.Object()) { + // (1.1) packages use exported named types (unless in package main) + ctx.use(m.Object(), nil, edgeExportedType) + } + } + g.typ(ctx, m.Type(), nil) + default: + panic(fmt.Sprintf("unreachable: %T", m)) + } + } + + if !g.wholeProgram { + // When not in whole program mode we reset seenTypes after each package, + // which means g.seenTypes only contains types of + // interest to us. In whole program mode, we're better off + // processing all interfaces at once, globally, both for + // performance reasons and because in whole program mode we + // actually care about all interfaces, not just the subset + // that has unexported methods. + + var ifaces []*types.Interface + var notIfaces []types.Type + + ctx.seenTypes.Iterate(func(t types.Type, _ interface{}) { + switch t := t.(type) { + case *types.Interface: + // OPT(dh): (8.1) we only need interfaces that have unexported methods + ifaces = append(ifaces, t) + default: + if _, ok := t.Underlying().(*types.Interface); !ok { + notIfaces = append(notIfaces, t) + } + } + }) + + // (8.0) handle interfaces + for _, t := range notIfaces { + ms := pkg.SSA.Prog.MethodSets.MethodSet(t) + for _, iface := range ifaces { + if sels, ok := g.implements(t, iface, ms); ok { + for _, sel := range sels { + g.useMethod(ctx, t, sel, t, edgeImplements) + } + } + } + } + } +} + +func (g *Graph) useMethod(ctx *context, t types.Type, sel *types.Selection, by interface{}, kind edgeKind) { + obj := sel.Obj() + path := sel.Index() + assert(obj != nil) + if len(path) > 1 { + base := lintdsl.Dereference(t).Underlying().(*types.Struct) + for _, idx := range path[:len(path)-1] { + next := base.Field(idx) + // (6.3) structs use embedded fields that help implement interfaces + ctx.see(base) + ctx.seeAndUse(next, base, edgeProvidesMethod) + base, _ = lintdsl.Dereference(next.Type()).Underlying().(*types.Struct) + } + } + ctx.seeAndUse(obj, by, kind) +} + +func owningObject(fn *ssa.Function) types.Object { + if fn.Object() != nil { + return fn.Object() + } + if fn.Parent() != nil { + return owningObject(fn.Parent()) + } + return nil +} + +func (g *Graph) function(ctx *context, fn *ssa.Function) { + if fn.Package() != nil && fn.Package() != ctx.pkg.SSA { + return + } + + name := fn.RelString(nil) + if _, ok := ctx.seenFns[name]; ok { + return + } + ctx.seenFns[name] = struct{}{} + + // (4.1) functions use all their arguments, return parameters and receivers + g.signature(ctx, fn.Signature, owningObject(fn)) + g.instructions(ctx, fn) + for _, anon := range fn.AnonFuncs { + // (4.2) functions use anonymous functions defined beneath them + // + // This fact is expressed implicitly. Anonymous functions have + // no types.Object, so their owner is the surrounding + // function. + g.function(ctx, anon) + } +} + +func (g *Graph) typ(ctx *context, t types.Type, parent types.Type) { + if g.wholeProgram { + g.mu.Lock() + } + if ctx.seenTypes.At(t) != nil { + if g.wholeProgram { + g.mu.Unlock() + } + return + } + if g.wholeProgram { + g.mu.Unlock() + } + if t, ok := t.(*types.Named); ok && t.Obj().Pkg() != nil { + if t.Obj().Pkg() != ctx.pkg.Pkg { + return + } + } + + if g.wholeProgram { + g.mu.Lock() + } + ctx.seenTypes.Set(t, struct{}{}) + if g.wholeProgram { + g.mu.Unlock() + } + if isIrrelevant(t) { + return + } + + ctx.see(t) + switch t := t.(type) { + case *types.Struct: + for i := 0; i < t.NumFields(); i++ { + ctx.see(t.Field(i)) + if t.Field(i).Exported() { + // (6.2) structs use exported fields + ctx.use(t.Field(i), t, edgeExportedField) + } else if t.Field(i).Name() == "_" { + ctx.use(t.Field(i), t, edgeBlankField) + } else if isNoCopyType(t.Field(i).Type()) { + // (6.1) structs use fields of type NoCopy sentinel + ctx.use(t.Field(i), t, edgeNoCopySentinel) + } else if parent == nil { + // (11.1) anonymous struct types use all their fields. + ctx.use(t.Field(i), t, edgeAnonymousStruct) + } + if t.Field(i).Anonymous() { + // (e3) exported identifiers aren't automatically used. + if !g.wholeProgram { + // does the embedded field contribute exported methods to the method set? + T := t.Field(i).Type() + if _, ok := T.Underlying().(*types.Pointer); !ok { + // An embedded field is addressable, so check + // the pointer type to get the full method set + T = types.NewPointer(T) + } + ms := ctx.pkg.SSA.Prog.MethodSets.MethodSet(T) + for j := 0; j < ms.Len(); j++ { + if ms.At(j).Obj().Exported() { + // (6.4) structs use embedded fields that have exported methods (recursively) + ctx.use(t.Field(i), t, edgeExtendsExportedMethodSet) + break + } + } + } + + seen := map[*types.Struct]struct{}{} + var hasExportedField func(t types.Type) bool + hasExportedField = func(T types.Type) bool { + t, ok := lintdsl.Dereference(T).Underlying().(*types.Struct) + if !ok { + return false + } + if _, ok := seen[t]; ok { + return false + } + seen[t] = struct{}{} + for i := 0; i < t.NumFields(); i++ { + field := t.Field(i) + if field.Exported() { + return true + } + if field.Embedded() && hasExportedField(field.Type()) { + return true + } + } + return false + } + // does the embedded field contribute exported fields? + if hasExportedField(t.Field(i).Type()) { + // (6.5) structs use embedded structs that have exported fields (recursively) + ctx.use(t.Field(i), t, edgeExtendsExportedFields) + } + + } + g.variable(ctx, t.Field(i)) + } + case *types.Basic: + // Nothing to do + case *types.Named: + // (9.3) types use their underlying and element types + ctx.seeAndUse(t.Underlying(), t, edgeUnderlyingType) + ctx.seeAndUse(t.Obj(), t, edgeTypeName) + ctx.seeAndUse(t, t.Obj(), edgeNamedType) + + // (2.4) named types use the pointer type + if _, ok := t.Underlying().(*types.Interface); !ok && t.NumMethods() > 0 { + ctx.seeAndUse(types.NewPointer(t), t, edgePointerType) + } + + for i := 0; i < t.NumMethods(); i++ { + ctx.see(t.Method(i)) + // don't use trackExportedIdentifier here, we care about + // all exported methods, even in package main or in tests. + if t.Method(i).Exported() && !g.wholeProgram { + // (2.1) named types use exported methods + ctx.use(t.Method(i), t, edgeExportedMethod) + } + g.function(ctx, ctx.pkg.SSA.Prog.FuncValue(t.Method(i))) + } + + g.typ(ctx, t.Underlying(), t) + case *types.Slice: + // (9.3) types use their underlying and element types + ctx.seeAndUse(t.Elem(), t, edgeElementType) + g.typ(ctx, t.Elem(), nil) + case *types.Map: + // (9.3) types use their underlying and element types + ctx.seeAndUse(t.Elem(), t, edgeElementType) + // (9.3) types use their underlying and element types + ctx.seeAndUse(t.Key(), t, edgeKeyType) + g.typ(ctx, t.Elem(), nil) + g.typ(ctx, t.Key(), nil) + case *types.Signature: + g.signature(ctx, t, nil) + case *types.Interface: + for i := 0; i < t.NumMethods(); i++ { + m := t.Method(i) + // (8.3) All interface methods are marked as used + ctx.seeAndUse(m, t, edgeInterfaceMethod) + ctx.seeAndUse(m.Type().(*types.Signature), m, edgeSignature) + g.signature(ctx, m.Type().(*types.Signature), nil) + } + for i := 0; i < t.NumEmbeddeds(); i++ { + tt := t.EmbeddedType(i) + // (8.4) All embedded interfaces are marked as used + ctx.seeAndUse(tt, t, edgeEmbeddedInterface) + } + case *types.Array: + // (9.3) types use their underlying and element types + ctx.seeAndUse(t.Elem(), t, edgeElementType) + g.typ(ctx, t.Elem(), nil) + case *types.Pointer: + // (9.3) types use their underlying and element types + ctx.seeAndUse(t.Elem(), t, edgeElementType) + g.typ(ctx, t.Elem(), nil) + case *types.Chan: + // (9.3) types use their underlying and element types + ctx.seeAndUse(t.Elem(), t, edgeElementType) + g.typ(ctx, t.Elem(), nil) + case *types.Tuple: + for i := 0; i < t.Len(); i++ { + // (9.3) types use their underlying and element types + ctx.seeAndUse(t.At(i).Type(), t, edgeTupleElement|edgeType) + g.typ(ctx, t.At(i).Type(), nil) + } + default: + panic(fmt.Sprintf("unreachable: %T", t)) + } +} + +func (g *Graph) variable(ctx *context, v *types.Var) { + // (9.2) variables use their types + ctx.seeAndUse(v.Type(), v, edgeType) + g.typ(ctx, v.Type(), nil) +} + +func (g *Graph) signature(ctx *context, sig *types.Signature, fn types.Object) { + var user interface{} = fn + if fn == nil { + user = sig + ctx.see(sig) + } + if sig.Recv() != nil { + ctx.seeAndUse(sig.Recv().Type(), user, edgeReceiver|edgeType) + g.typ(ctx, sig.Recv().Type(), nil) + } + for i := 0; i < sig.Params().Len(); i++ { + param := sig.Params().At(i) + ctx.seeAndUse(param.Type(), user, edgeFunctionArgument|edgeType) + g.typ(ctx, param.Type(), nil) + } + for i := 0; i < sig.Results().Len(); i++ { + param := sig.Results().At(i) + ctx.seeAndUse(param.Type(), user, edgeFunctionResult|edgeType) + g.typ(ctx, param.Type(), nil) + } +} + +func (g *Graph) instructions(ctx *context, fn *ssa.Function) { + fnObj := owningObject(fn) + for _, b := range fn.Blocks { + for _, instr := range b.Instrs { + ops := instr.Operands(nil) + switch instr.(type) { + case *ssa.Store: + // (9.7) variable _reads_ use variables, writes do not + ops = ops[1:] + case *ssa.DebugRef: + ops = nil + } + for _, arg := range ops { + walkPhi(*arg, func(v ssa.Value) { + switch v := v.(type) { + case *ssa.Function: + // (4.3) functions use closures and bound methods. + // (4.5) functions use functions they call + // (9.5) instructions use their operands + // (4.4) functions use functions they return. we assume that someone else will call the returned function + if owningObject(v) != nil { + ctx.seeAndUse(owningObject(v), fnObj, edgeInstructionOperand) + } + g.function(ctx, v) + case *ssa.Const: + // (9.6) instructions use their operands' types + ctx.seeAndUse(v.Type(), fnObj, edgeType) + g.typ(ctx, v.Type(), nil) + case *ssa.Global: + if v.Object() != nil { + // (9.5) instructions use their operands + ctx.seeAndUse(v.Object(), fnObj, edgeInstructionOperand) + } + } + }) + } + if v, ok := instr.(ssa.Value); ok { + if _, ok := v.(*ssa.Range); !ok { + // See https://github.com/golang/go/issues/19670 + + // (4.8) instructions use their types + // (9.4) conversions use the type they convert to + ctx.seeAndUse(v.Type(), fnObj, edgeType) + g.typ(ctx, v.Type(), nil) + } + } + switch instr := instr.(type) { + case *ssa.Field: + st := instr.X.Type().Underlying().(*types.Struct) + field := st.Field(instr.Field) + // (4.7) functions use fields they access + ctx.seeAndUse(field, fnObj, edgeFieldAccess) + case *ssa.FieldAddr: + st := lintdsl.Dereference(instr.X.Type()).Underlying().(*types.Struct) + field := st.Field(instr.Field) + // (4.7) functions use fields they access + ctx.seeAndUse(field, fnObj, edgeFieldAccess) + case *ssa.Store: + // nothing to do, handled generically by operands + case *ssa.Call: + c := instr.Common() + if !c.IsInvoke() { + // handled generically as an instruction operand + + if g.wholeProgram { + // (e3) special case known reflection-based method callers + switch lintdsl.CallName(c) { + case "net/rpc.Register", "net/rpc.RegisterName", "(*net/rpc.Server).Register", "(*net/rpc.Server).RegisterName": + var arg ssa.Value + switch lintdsl.CallName(c) { + case "net/rpc.Register": + arg = c.Args[0] + case "net/rpc.RegisterName": + arg = c.Args[1] + case "(*net/rpc.Server).Register": + arg = c.Args[1] + case "(*net/rpc.Server).RegisterName": + arg = c.Args[2] + } + walkPhi(arg, func(v ssa.Value) { + if v, ok := v.(*ssa.MakeInterface); ok { + walkPhi(v.X, func(vv ssa.Value) { + ms := ctx.pkg.SSA.Prog.MethodSets.MethodSet(vv.Type()) + for i := 0; i < ms.Len(); i++ { + if ms.At(i).Obj().Exported() { + g.useMethod(ctx, vv.Type(), ms.At(i), fnObj, edgeNetRPCRegister) + } + } + }) + } + }) + } + } + } else { + // (4.5) functions use functions/interface methods they call + ctx.seeAndUse(c.Method, fnObj, edgeInterfaceCall) + } + case *ssa.Return: + // nothing to do, handled generically by operands + case *ssa.ChangeType: + // conversion type handled generically + + s1, ok1 := lintdsl.Dereference(instr.Type()).Underlying().(*types.Struct) + s2, ok2 := lintdsl.Dereference(instr.X.Type()).Underlying().(*types.Struct) + if ok1 && ok2 { + // Converting between two structs. The fields are + // relevant for the conversion, but only if the + // fields are also used outside of the conversion. + // Mark fields as used by each other. + + assert(s1.NumFields() == s2.NumFields()) + for i := 0; i < s1.NumFields(); i++ { + ctx.see(s1.Field(i)) + ctx.see(s2.Field(i)) + // (5.1) when converting between two equivalent structs, the fields in + // either struct use each other. the fields are relevant for the + // conversion, but only if the fields are also accessed outside the + // conversion. + ctx.seeAndUse(s1.Field(i), s2.Field(i), edgeStructConversion) + ctx.seeAndUse(s2.Field(i), s1.Field(i), edgeStructConversion) + } + } + case *ssa.MakeInterface: + // nothing to do, handled generically by operands + case *ssa.Slice: + // nothing to do, handled generically by operands + case *ssa.RunDefers: + // nothing to do, the deferred functions are already marked use by defering them. + case *ssa.Convert: + // to unsafe.Pointer + if typ, ok := instr.Type().(*types.Basic); ok && typ.Kind() == types.UnsafePointer { + if ptr, ok := instr.X.Type().Underlying().(*types.Pointer); ok { + if st, ok := ptr.Elem().Underlying().(*types.Struct); ok { + for i := 0; i < st.NumFields(); i++ { + // (5.2) when converting to or from unsafe.Pointer, mark all fields as used. + ctx.seeAndUse(st.Field(i), fnObj, edgeUnsafeConversion) + } + } + } + } + // from unsafe.Pointer + if typ, ok := instr.X.Type().(*types.Basic); ok && typ.Kind() == types.UnsafePointer { + if ptr, ok := instr.Type().Underlying().(*types.Pointer); ok { + if st, ok := ptr.Elem().Underlying().(*types.Struct); ok { + for i := 0; i < st.NumFields(); i++ { + // (5.2) when converting to or from unsafe.Pointer, mark all fields as used. + ctx.seeAndUse(st.Field(i), fnObj, edgeUnsafeConversion) + } + } + } + } + case *ssa.TypeAssert: + // nothing to do, handled generically by instruction + // type (possibly a tuple, which contains the asserted + // to type). redundantly handled by the type of + // ssa.Extract, too + case *ssa.MakeClosure: + // nothing to do, handled generically by operands + case *ssa.Alloc: + // nothing to do + case *ssa.UnOp: + // nothing to do + case *ssa.BinOp: + // nothing to do + case *ssa.If: + // nothing to do + case *ssa.Jump: + // nothing to do + case *ssa.IndexAddr: + // nothing to do + case *ssa.Extract: + // nothing to do + case *ssa.Panic: + // nothing to do + case *ssa.DebugRef: + // nothing to do + case *ssa.BlankStore: + // nothing to do + case *ssa.Phi: + // nothing to do + case *ssa.MakeMap: + // nothing to do + case *ssa.MapUpdate: + // nothing to do + case *ssa.Lookup: + // nothing to do + case *ssa.MakeSlice: + // nothing to do + case *ssa.Send: + // nothing to do + case *ssa.MakeChan: + // nothing to do + case *ssa.Range: + // nothing to do + case *ssa.Next: + // nothing to do + case *ssa.Index: + // nothing to do + case *ssa.Select: + // nothing to do + case *ssa.ChangeInterface: + // nothing to do + case *ssa.Go: + // nothing to do, handled generically by operands + case *ssa.Defer: + // nothing to do, handled generically by operands + default: + panic(fmt.Sprintf("unreachable: %T", instr)) + } + } + } +} + +// isNoCopyType reports whether a type represents the NoCopy sentinel +// type. The NoCopy type is a named struct with no fields and exactly +// one method `func Lock()` that is empty. +// +// FIXME(dh): currently we're not checking that the function body is +// empty. +func isNoCopyType(typ types.Type) bool { + st, ok := typ.Underlying().(*types.Struct) + if !ok { + return false + } + if st.NumFields() != 0 { + return false + } + + named, ok := typ.(*types.Named) + if !ok { + return false + } + if named.NumMethods() != 1 { + return false + } + meth := named.Method(0) + if meth.Name() != "Lock" { + return false + } + sig := meth.Type().(*types.Signature) + if sig.Params().Len() != 0 || sig.Results().Len() != 0 { + return false + } + return true +} + +func walkPhi(v ssa.Value, fn func(v ssa.Value)) { + phi, ok := v.(*ssa.Phi) + if !ok { + fn(v) + return + } + + seen := map[ssa.Value]struct{}{} + var impl func(v *ssa.Phi) + impl = func(v *ssa.Phi) { + if _, ok := seen[v]; ok { + return + } + seen[v] = struct{}{} + for _, e := range v.Edges { + if ev, ok := e.(*ssa.Phi); ok { + impl(ev) + } else { + fn(e) + } + } + } + impl(phi) +} + +func interfacesFromExportData(pkg *types.Package) []*types.Interface { + var out []*types.Interface + scope := pkg.Scope() + for _, name := range scope.Names() { + obj := scope.Lookup(name) + out = append(out, interfacesFromObject(obj)...) + } + return out +} + +func interfacesFromObject(obj types.Object) []*types.Interface { + var out []*types.Interface + switch obj := obj.(type) { + case *types.Func: + sig := obj.Type().(*types.Signature) + for i := 0; i < sig.Results().Len(); i++ { + out = append(out, interfacesFromObject(sig.Results().At(i))...) + } + for i := 0; i < sig.Params().Len(); i++ { + out = append(out, interfacesFromObject(sig.Params().At(i))...) + } + case *types.TypeName: + if named, ok := obj.Type().(*types.Named); ok { + for i := 0; i < named.NumMethods(); i++ { + out = append(out, interfacesFromObject(named.Method(i))...) + } + + if iface, ok := named.Underlying().(*types.Interface); ok { + out = append(out, iface) + } + } + case *types.Var: + // No call to Underlying here. We want unnamed interfaces + // only. Named interfaces are gotten directly from the + // package's scope. + if iface, ok := obj.Type().(*types.Interface); ok { + out = append(out, iface) + } + case *types.Const: + case *types.Builtin: + default: + panic(fmt.Sprintf("unhandled type: %T", obj)) + } + return out +} diff --git a/vendor/honnef.co/go/tools/version/buildinfo.go b/vendor/honnef.co/go/tools/version/buildinfo.go new file mode 100644 index 000000000000..b6034bb7dcd8 --- /dev/null +++ b/vendor/honnef.co/go/tools/version/buildinfo.go @@ -0,0 +1,46 @@ +// +build go1.12 + +package version + +import ( + "fmt" + "runtime/debug" +) + +func printBuildInfo() { + if info, ok := debug.ReadBuildInfo(); ok { + fmt.Println("Main module:") + printModule(&info.Main) + fmt.Println("Dependencies:") + for _, dep := range info.Deps { + printModule(dep) + } + } else { + fmt.Println("Built without Go modules") + } +} + +func buildInfoVersion() (string, bool) { + info, ok := debug.ReadBuildInfo() + if !ok { + return "", false + } + if info.Main.Version == "(devel)" { + return "", false + } + return info.Main.Version, true +} + +func printModule(m *debug.Module) { + fmt.Printf("\t%s", m.Path) + if m.Version != "(devel)" { + fmt.Printf("@%s", m.Version) + } + if m.Sum != "" { + fmt.Printf(" (sum: %s)", m.Sum) + } + if m.Replace != nil { + fmt.Printf(" (replace: %s)", m.Replace.Path) + } + fmt.Println() +} diff --git a/vendor/honnef.co/go/tools/version/buildinfo111.go b/vendor/honnef.co/go/tools/version/buildinfo111.go new file mode 100644 index 000000000000..06aae1e65bb3 --- /dev/null +++ b/vendor/honnef.co/go/tools/version/buildinfo111.go @@ -0,0 +1,6 @@ +// +build !go1.12 + +package version + +func printBuildInfo() {} +func buildInfoVersion() (string, bool) { return "", false } diff --git a/vendor/honnef.co/go/tools/version/version.go b/vendor/honnef.co/go/tools/version/version.go new file mode 100644 index 000000000000..468e8efd6e1c --- /dev/null +++ b/vendor/honnef.co/go/tools/version/version.go @@ -0,0 +1,42 @@ +package version + +import ( + "fmt" + "os" + "path/filepath" + "runtime" +) + +const Version = "2019.2.3" + +// version returns a version descriptor and reports whether the +// version is a known release. +func version() (string, bool) { + if Version != "devel" { + return Version, true + } + v, ok := buildInfoVersion() + if ok { + return v, false + } + return "devel", false +} + +func Print() { + v, release := version() + + if release { + fmt.Printf("%s %s\n", filepath.Base(os.Args[0]), v) + } else if v == "devel" { + fmt.Printf("%s (no version)\n", filepath.Base(os.Args[0])) + } else { + fmt.Printf("%s (devel, %s)\n", filepath.Base(os.Args[0]), v) + } +} + +func Verbose() { + Print() + fmt.Println() + fmt.Println("Compiled with Go version:", runtime.Version()) + printBuildInfo() +} diff --git a/vendor/modules.txt b/vendor/modules.txt index b41f476aee2b..01332ead3198 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -61,27 +61,6 @@ github.com/golangci/errcheck/golangci github.com/golangci/errcheck/internal/errcheck # github.com/golangci/go-misc v0.0.0-20180628070357-927a3d87b613 github.com/golangci/go-misc/deadcode -# github.com/golangci/go-tools v0.0.0-20190318055746-e32c54105b7c -github.com/golangci/go-tools/arg -github.com/golangci/go-tools/callgraph -github.com/golangci/go-tools/callgraph/static -github.com/golangci/go-tools/config -github.com/golangci/go-tools/deprecated -github.com/golangci/go-tools/functions -github.com/golangci/go-tools/internal/sharedcheck -github.com/golangci/go-tools/lint -github.com/golangci/go-tools/lint/lintdsl -github.com/golangci/go-tools/lint/lintutil -github.com/golangci/go-tools/lint/lintutil/format -github.com/golangci/go-tools/simple -github.com/golangci/go-tools/ssa -github.com/golangci/go-tools/ssa/ssautil -github.com/golangci/go-tools/ssautil -github.com/golangci/go-tools/staticcheck -github.com/golangci/go-tools/staticcheck/vrp -github.com/golangci/go-tools/stylecheck -github.com/golangci/go-tools/unused -github.com/golangci/go-tools/version # github.com/golangci/goconst v0.0.0-20180610141641-041c5f2b40f3 github.com/golangci/goconst # github.com/golangci/gocyclo v0.0.0-20180528134321-2becd97e67ee @@ -202,7 +181,7 @@ golang.org/x/sys/windows golang.org/x/text/transform golang.org/x/text/unicode/norm golang.org/x/text/width -# golang.org/x/tools v0.0.0-20190912215617-3720d1ec3678 => github.com/golangci/tools v0.0.0-20190914130248-e9260b99c8f1 +# golang.org/x/tools v0.0.0-20190912215617-3720d1ec3678 => github.com/golangci/tools v0.0.0-20190915081525-6aa350649b1c golang.org/x/tools/go/analysis golang.org/x/tools/go/analysis/passes/asmdecl golang.org/x/tools/go/analysis/passes/assign @@ -244,6 +223,7 @@ golang.org/x/tools/go/loader golang.org/x/tools/go/packages golang.org/x/tools/go/ssa golang.org/x/tools/go/ssa/ssautil +golang.org/x/tools/go/types/objectpath golang.org/x/tools/go/types/typeutil golang.org/x/tools/imports golang.org/x/tools/internal/fastwalk @@ -253,6 +233,31 @@ golang.org/x/tools/internal/module golang.org/x/tools/internal/semver # gopkg.in/yaml.v2 v2.2.2 gopkg.in/yaml.v2 +# honnef.co/go/tools v0.0.1-2019.2.3 +honnef.co/go/tools/arg +honnef.co/go/tools/config +honnef.co/go/tools/deprecated +honnef.co/go/tools/facts +honnef.co/go/tools/functions +honnef.co/go/tools/go/types/typeutil +honnef.co/go/tools/internal/cache +honnef.co/go/tools/internal/passes/buildssa +honnef.co/go/tools/internal/renameio +honnef.co/go/tools/internal/sharedcheck +honnef.co/go/tools/lint +honnef.co/go/tools/lint/lintdsl +honnef.co/go/tools/lint/lintutil +honnef.co/go/tools/lint/lintutil/format +honnef.co/go/tools/loader +honnef.co/go/tools/printf +honnef.co/go/tools/simple +honnef.co/go/tools/ssa +honnef.co/go/tools/ssautil +honnef.co/go/tools/staticcheck +honnef.co/go/tools/staticcheck/vrp +honnef.co/go/tools/stylecheck +honnef.co/go/tools/unused +honnef.co/go/tools/version # mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed mvdan.cc/interfacer/check # mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b