support ineffassign
This commit is contained in:
parent
7c4ab92d00
commit
364dd68ed8
8
Gopkg.lock
generated
8
Gopkg.lock
generated
@ -108,6 +108,12 @@
|
||||
]
|
||||
revision = "31bebf17d90d40b728c41fecdd5acf5fb8654fc7"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "github.com/golangci/ineffassign"
|
||||
packages = ["."]
|
||||
revision = "7b41b0f84881918dab3c16c5e5148d8aa55d27b4"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "github.com/golangci/maligned"
|
||||
@ -252,6 +258,6 @@
|
||||
[solve-meta]
|
||||
analyzer-name = "dep"
|
||||
analyzer-version = 1
|
||||
inputs-digest = "6ded01ff91bda8204ee2c22da5f6665af28fc5e9551a8b3782ff65582f08d738"
|
||||
inputs-digest = "aa0c6cd4a6cd5957ffdeeb0f06753a92f33f5393045845e45bc63a10ead24a85"
|
||||
solver-name = "gps-cdcl"
|
||||
solver-version = 1
|
||||
|
@ -65,6 +65,7 @@ func GetAllSupportedLinterConfigs() []LinterConfig {
|
||||
enabledByDefault(golinters.Varcheck{}, "Finds unused global variables and constants", true),
|
||||
enabledByDefault(golinters.Megacheck{}, "Megacheck: 3 sub-linters in one: staticcheck, gosimple and unused", true),
|
||||
enabledByDefault(golinters.Dupl{}, "Tool for code clone detection", false),
|
||||
enabledByDefault(golinters.Ineffassign{}, "Detects when assignments to existing variables are not used", false),
|
||||
|
||||
disabledByDefault(golinters.Gofmt{}, "Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification", false),
|
||||
disabledByDefault(golinters.Gofmt{UseGoimports: true}, "Goimports does everything that gofmt does. Additionally it checks unused imports", false),
|
||||
|
30
pkg/golinters/ineffassign.go
Normal file
30
pkg/golinters/ineffassign.go
Normal file
@ -0,0 +1,30 @@
|
||||
package golinters
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/golangci/golangci-lint/pkg/result"
|
||||
ineffassignAPI "github.com/golangci/ineffassign"
|
||||
)
|
||||
|
||||
type Ineffassign struct{}
|
||||
|
||||
func (Ineffassign) Name() string {
|
||||
return "ineffassign"
|
||||
}
|
||||
|
||||
func (lint Ineffassign) Run(ctx context.Context, lintCtx *Context) (*result.Result, error) {
|
||||
issues := ineffassignAPI.Run(lintCtx.Paths.Files)
|
||||
|
||||
res := &result.Result{}
|
||||
for _, i := range issues {
|
||||
res.Issues = append(res.Issues, result.Issue{
|
||||
File: i.Pos.Filename,
|
||||
LineNumber: i.Pos.Line,
|
||||
Text: fmt.Sprintf("ineffectual assignment to %s", formatCode(i.IdentName, lintCtx.RunCfg())),
|
||||
FromLinter: lint.Name(),
|
||||
})
|
||||
}
|
||||
return res, nil
|
||||
}
|
10
pkg/golinters/testdata/ineffassign.go
vendored
Normal file
10
pkg/golinters/testdata/ineffassign.go
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
package testdata
|
||||
|
||||
func _() {
|
||||
x := 0
|
||||
for {
|
||||
_ = x
|
||||
x = 0 // ERROR "ineffectual assignment to `x`"
|
||||
x = 0
|
||||
}
|
||||
}
|
2
pkg/golinters/testdata/megacheck.go
vendored
2
pkg/golinters/testdata/megacheck.go
vendored
@ -2,5 +2,5 @@ package testdata
|
||||
|
||||
func Megacheck() {
|
||||
var x int
|
||||
x = x // ERROR "self-assignment of x to x"
|
||||
x = x // nolint:ineffassign // ERROR "self-assignment of x to x"
|
||||
}
|
||||
|
30
vendor/github.com/golangci/ineffassign/.gitignore
generated
vendored
Normal file
30
vendor/github.com/golangci/ineffassign/.gitignore
generated
vendored
Normal file
@ -0,0 +1,30 @@
|
||||
ineffassign
|
||||
|
||||
# Created by https://www.gitignore.io/api/go
|
||||
|
||||
### Go ###
|
||||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
[568vq].out
|
||||
|
||||
*.cgo1.go
|
||||
*.cgo2.c
|
||||
_cgo_defun.c
|
||||
_cgo_gotypes.go
|
||||
_cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
||||
*.test
|
||||
*.prof
|
||||
|
21
vendor/github.com/golangci/ineffassign/LICENSE
generated
vendored
Normal file
21
vendor/github.com/golangci/ineffassign/LICENSE
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2016 Gordon Klaus and contributors
|
||||
|
||||
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.
|
4
vendor/github.com/golangci/ineffassign/README.md
generated
vendored
Normal file
4
vendor/github.com/golangci/ineffassign/README.md
generated
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
# ineffassign
|
||||
Detect ineffectual assignments in Go code.
|
||||
|
||||
This tool misses some cases because does not consider any type information in its analysis. (For example, assignments to struct fields are never marked as ineffectual.) It should, however, never give any false positives.
|
7
vendor/github.com/golangci/ineffassign/bugs
generated
vendored
Normal file
7
vendor/github.com/golangci/ineffassign/bugs
generated
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
cmd/compile/internal/big/floatconv.go:367:2 m
|
||||
cmd/cover/cover_test.go:62:2 err
|
||||
cmd/pprof/internal/profile/profile.go:131:10 err
|
||||
math/big/ftoa.go:285:2 m
|
||||
net/file_unix.go:66:7 err
|
||||
golang.org/x/mobile/app/android.go:175:2 queue
|
||||
golang.org/x/net/icmp/listen_posix.go:83:6 err
|
600
vendor/github.com/golangci/ineffassign/ineffassign.go
generated
vendored
Normal file
600
vendor/github.com/golangci/ineffassign/ineffassign.go
generated
vendored
Normal file
@ -0,0 +1,600 @@
|
||||
package ineffassign
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const invalidArgumentExitCode = 3
|
||||
|
||||
var dontRecurseFlag = flag.Bool("ineffassign.n", false, "don't recursively check paths")
|
||||
|
||||
type Issue struct {
|
||||
Pos token.Position
|
||||
IdentName string
|
||||
}
|
||||
|
||||
func Run(files []string) []Issue {
|
||||
var issues []Issue
|
||||
for _, path := range files {
|
||||
fset, _, ineff := checkPath(path)
|
||||
for _, id := range ineff {
|
||||
issues = append(issues, Issue{
|
||||
Pos: fset.Position(id.Pos()),
|
||||
IdentName: id.Name,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return issues
|
||||
}
|
||||
|
||||
func walkPath(root string) bool {
|
||||
lintFailed := false
|
||||
filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
fmt.Printf("Error during filesystem walk: %v\n", err)
|
||||
return nil
|
||||
}
|
||||
if fi.IsDir() {
|
||||
if path != root && (*dontRecurseFlag ||
|
||||
filepath.Base(path) == "testdata" ||
|
||||
filepath.Base(path) == "vendor") {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if !strings.HasSuffix(path, ".go") {
|
||||
return nil
|
||||
}
|
||||
fset, _, ineff := checkPath(path)
|
||||
for _, id := range ineff {
|
||||
fmt.Printf("%s: ineffectual assignment to %s\n", fset.Position(id.Pos()), id.Name)
|
||||
lintFailed = true
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return lintFailed
|
||||
}
|
||||
|
||||
func checkPath(path string) (*token.FileSet, []*ast.CommentGroup, []*ast.Ident) {
|
||||
fset := token.NewFileSet()
|
||||
f, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
|
||||
if err != nil {
|
||||
return nil, nil, nil
|
||||
}
|
||||
|
||||
bld := &builder{vars: map[*ast.Object]*variable{}}
|
||||
bld.walk(f)
|
||||
|
||||
chk := &checker{vars: bld.vars, seen: map[*block]bool{}}
|
||||
for _, b := range bld.roots {
|
||||
chk.check(b)
|
||||
}
|
||||
sort.Sort(chk.ineff)
|
||||
|
||||
return fset, f.Comments, chk.ineff
|
||||
}
|
||||
|
||||
type builder struct {
|
||||
roots []*block
|
||||
block *block
|
||||
vars map[*ast.Object]*variable
|
||||
results []*ast.FieldList
|
||||
breaks branchStack
|
||||
continues branchStack
|
||||
gotos branchStack
|
||||
labelStmt *ast.LabeledStmt
|
||||
}
|
||||
|
||||
type block struct {
|
||||
children []*block
|
||||
ops map[*ast.Object][]operation
|
||||
}
|
||||
|
||||
func (b *block) addChild(c *block) {
|
||||
b.children = append(b.children, c)
|
||||
}
|
||||
|
||||
type operation struct {
|
||||
id *ast.Ident
|
||||
assign bool
|
||||
}
|
||||
|
||||
type variable struct {
|
||||
fundept int
|
||||
escapes bool
|
||||
}
|
||||
|
||||
func (bld *builder) walk(n ast.Node) {
|
||||
if n != nil {
|
||||
ast.Walk(bld, n)
|
||||
}
|
||||
}
|
||||
|
||||
func (bld *builder) Visit(n ast.Node) ast.Visitor {
|
||||
switch n := n.(type) {
|
||||
case *ast.FuncDecl:
|
||||
if n.Body != nil {
|
||||
bld.fun(n.Type, n.Body)
|
||||
}
|
||||
case *ast.FuncLit:
|
||||
bld.fun(n.Type, n.Body)
|
||||
case *ast.IfStmt:
|
||||
bld.walk(n.Init)
|
||||
bld.walk(n.Cond)
|
||||
b0 := bld.block
|
||||
bld.newBlock(b0)
|
||||
bld.walk(n.Body)
|
||||
b1 := bld.block
|
||||
if n.Else != nil {
|
||||
bld.newBlock(b0)
|
||||
bld.walk(n.Else)
|
||||
b0 = bld.block
|
||||
}
|
||||
bld.newBlock(b0, b1)
|
||||
case *ast.ForStmt:
|
||||
lbl := bld.stmtLabel(n)
|
||||
brek := bld.breaks.push(lbl)
|
||||
continu := bld.continues.push(lbl)
|
||||
bld.walk(n.Init)
|
||||
start := bld.newBlock(bld.block)
|
||||
bld.walk(n.Cond)
|
||||
cond := bld.block
|
||||
bld.newBlock(cond)
|
||||
bld.walk(n.Body)
|
||||
continu.setDestination(bld.newBlock(bld.block))
|
||||
bld.walk(n.Post)
|
||||
bld.block.addChild(start)
|
||||
brek.setDestination(bld.newBlock(cond))
|
||||
bld.breaks.pop()
|
||||
bld.continues.pop()
|
||||
case *ast.RangeStmt:
|
||||
lbl := bld.stmtLabel(n)
|
||||
brek := bld.breaks.push(lbl)
|
||||
continu := bld.continues.push(lbl)
|
||||
bld.walk(n.X)
|
||||
pre := bld.newBlock(bld.block)
|
||||
start := bld.newBlock(pre)
|
||||
if n.Key != nil {
|
||||
lhs := []ast.Expr{n.Key}
|
||||
if n.Value != nil {
|
||||
lhs = append(lhs, n.Value)
|
||||
}
|
||||
bld.walk(&ast.AssignStmt{Lhs: lhs, Tok: n.Tok, TokPos: n.TokPos, Rhs: []ast.Expr{&ast.Ident{NamePos: n.X.End()}}})
|
||||
}
|
||||
bld.walk(n.Body)
|
||||
bld.block.addChild(start)
|
||||
continu.setDestination(pre)
|
||||
brek.setDestination(bld.newBlock(pre, bld.block))
|
||||
bld.breaks.pop()
|
||||
bld.continues.pop()
|
||||
case *ast.SwitchStmt:
|
||||
bld.walk(n.Init)
|
||||
bld.walk(n.Tag)
|
||||
bld.swtch(n, n.Body.List)
|
||||
case *ast.TypeSwitchStmt:
|
||||
bld.walk(n.Init)
|
||||
bld.walk(n.Assign)
|
||||
bld.swtch(n, n.Body.List)
|
||||
case *ast.SelectStmt:
|
||||
brek := bld.breaks.push(bld.stmtLabel(n))
|
||||
for _, c := range n.Body.List {
|
||||
c := c.(*ast.CommClause).Comm
|
||||
if s, ok := c.(*ast.AssignStmt); ok {
|
||||
bld.walk(s.Rhs[0])
|
||||
} else {
|
||||
bld.walk(c)
|
||||
}
|
||||
}
|
||||
b0 := bld.block
|
||||
exits := make([]*block, len(n.Body.List))
|
||||
dfault := false
|
||||
for i, c := range n.Body.List {
|
||||
c := c.(*ast.CommClause)
|
||||
bld.newBlock(b0)
|
||||
bld.walk(c)
|
||||
exits[i] = bld.block
|
||||
dfault = dfault || c.Comm == nil
|
||||
}
|
||||
if !dfault {
|
||||
exits = append(exits, b0)
|
||||
}
|
||||
brek.setDestination(bld.newBlock(exits...))
|
||||
bld.breaks.pop()
|
||||
case *ast.LabeledStmt:
|
||||
bld.gotos.get(n.Label).setDestination(bld.newBlock(bld.block))
|
||||
bld.labelStmt = n
|
||||
bld.walk(n.Stmt)
|
||||
case *ast.BranchStmt:
|
||||
switch n.Tok {
|
||||
case token.BREAK:
|
||||
bld.breaks.get(n.Label).addSource(bld.block)
|
||||
bld.newBlock()
|
||||
case token.CONTINUE:
|
||||
bld.continues.get(n.Label).addSource(bld.block)
|
||||
bld.newBlock()
|
||||
case token.GOTO:
|
||||
bld.gotos.get(n.Label).addSource(bld.block)
|
||||
bld.newBlock()
|
||||
}
|
||||
|
||||
case *ast.AssignStmt:
|
||||
if n.Tok == token.QUO_ASSIGN || n.Tok == token.REM_ASSIGN {
|
||||
bld.maybePanic()
|
||||
}
|
||||
|
||||
for _, x := range n.Rhs {
|
||||
bld.walk(x)
|
||||
}
|
||||
for i, x := range n.Lhs {
|
||||
if id, ok := ident(x); ok {
|
||||
if n.Tok >= token.ADD_ASSIGN && n.Tok <= token.AND_NOT_ASSIGN {
|
||||
bld.use(id)
|
||||
}
|
||||
// Don't treat explicit initialization to zero as assignment; it is often used as shorthand for a bare declaration.
|
||||
if n.Tok == token.DEFINE && i < len(n.Rhs) && isZeroLiteral(n.Rhs[i]) {
|
||||
bld.use(id)
|
||||
} else {
|
||||
bld.assign(id)
|
||||
}
|
||||
} else {
|
||||
bld.walk(x)
|
||||
}
|
||||
}
|
||||
case *ast.GenDecl:
|
||||
if n.Tok == token.VAR {
|
||||
for _, s := range n.Specs {
|
||||
s := s.(*ast.ValueSpec)
|
||||
for _, x := range s.Values {
|
||||
bld.walk(x)
|
||||
}
|
||||
for _, id := range s.Names {
|
||||
if len(s.Values) > 0 {
|
||||
bld.assign(id)
|
||||
} else {
|
||||
bld.use(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
case *ast.IncDecStmt:
|
||||
if id, ok := ident(n.X); ok {
|
||||
bld.use(id)
|
||||
bld.assign(id)
|
||||
} else {
|
||||
bld.walk(n.X)
|
||||
}
|
||||
case *ast.Ident:
|
||||
bld.use(n)
|
||||
case *ast.ReturnStmt:
|
||||
for _, x := range n.Results {
|
||||
bld.walk(x)
|
||||
}
|
||||
res := bld.results[len(bld.results)-1]
|
||||
if res == nil {
|
||||
break
|
||||
}
|
||||
for _, f := range res.List {
|
||||
for _, id := range f.Names {
|
||||
if n.Results != nil {
|
||||
bld.assign(id)
|
||||
}
|
||||
bld.use(id)
|
||||
}
|
||||
}
|
||||
case *ast.SendStmt:
|
||||
bld.maybePanic()
|
||||
return bld
|
||||
|
||||
case *ast.BinaryExpr:
|
||||
if n.Op == token.EQL || n.Op == token.QUO || n.Op == token.REM {
|
||||
bld.maybePanic()
|
||||
}
|
||||
return bld
|
||||
case *ast.CallExpr:
|
||||
bld.maybePanic()
|
||||
return bld
|
||||
case *ast.IndexExpr:
|
||||
bld.maybePanic()
|
||||
return bld
|
||||
case *ast.UnaryExpr:
|
||||
id, ok := ident(n.X)
|
||||
if ix, isIx := n.X.(*ast.IndexExpr); isIx {
|
||||
// We don't care about indexing into slices, but without type information we can do no better.
|
||||
id, ok = ident(ix.X)
|
||||
}
|
||||
if ok && n.Op == token.AND {
|
||||
if v, ok := bld.vars[id.Obj]; ok {
|
||||
v.escapes = true
|
||||
}
|
||||
}
|
||||
return bld
|
||||
case *ast.SelectorExpr:
|
||||
bld.maybePanic()
|
||||
// A method call (possibly delayed via a method value) might implicitly take
|
||||
// the address of its receiver, causing it to escape.
|
||||
// We can't do any better here without knowing the variable's type.
|
||||
if id, ok := ident(n.X); ok {
|
||||
if v, ok := bld.vars[id.Obj]; ok {
|
||||
v.escapes = true
|
||||
}
|
||||
}
|
||||
return bld
|
||||
case *ast.SliceExpr:
|
||||
bld.maybePanic()
|
||||
// We don't care about slicing into slices, but without type information we can do no better.
|
||||
if id, ok := ident(n.X); ok {
|
||||
if v, ok := bld.vars[id.Obj]; ok {
|
||||
v.escapes = true
|
||||
}
|
||||
}
|
||||
return bld
|
||||
case *ast.StarExpr:
|
||||
bld.maybePanic()
|
||||
return bld
|
||||
case *ast.TypeAssertExpr:
|
||||
bld.maybePanic()
|
||||
return bld
|
||||
|
||||
default:
|
||||
return bld
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func isZeroLiteral(x ast.Expr) bool {
|
||||
b, ok := x.(*ast.BasicLit)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
switch b.Value {
|
||||
case "0", "0.0", "0.", ".0", `""`:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (bld *builder) fun(typ *ast.FuncType, body *ast.BlockStmt) {
|
||||
for _, v := range bld.vars {
|
||||
v.fundept++
|
||||
}
|
||||
bld.results = append(bld.results, typ.Results)
|
||||
|
||||
b := bld.block
|
||||
bld.newBlock()
|
||||
bld.roots = append(bld.roots, bld.block)
|
||||
bld.walk(typ)
|
||||
bld.walk(body)
|
||||
bld.block = b
|
||||
|
||||
bld.results = bld.results[:len(bld.results)-1]
|
||||
for _, v := range bld.vars {
|
||||
v.fundept--
|
||||
}
|
||||
}
|
||||
|
||||
func (bld *builder) swtch(stmt ast.Stmt, cases []ast.Stmt) {
|
||||
brek := bld.breaks.push(bld.stmtLabel(stmt))
|
||||
b0 := bld.block
|
||||
list := b0
|
||||
exits := make([]*block, 0, len(cases)+1)
|
||||
var dfault, fallthru *block
|
||||
for _, c := range cases {
|
||||
c := c.(*ast.CaseClause)
|
||||
|
||||
if c.List != nil {
|
||||
list = bld.newBlock(list)
|
||||
for _, x := range c.List {
|
||||
bld.walk(x)
|
||||
}
|
||||
}
|
||||
|
||||
parents := []*block{}
|
||||
if c.List != nil {
|
||||
parents = append(parents, list)
|
||||
}
|
||||
if fallthru != nil {
|
||||
parents = append(parents, fallthru)
|
||||
fallthru = nil
|
||||
}
|
||||
bld.newBlock(parents...)
|
||||
if c.List == nil {
|
||||
dfault = bld.block
|
||||
}
|
||||
for _, s := range c.Body {
|
||||
bld.walk(s)
|
||||
if s, ok := s.(*ast.BranchStmt); ok && s.Tok == token.FALLTHROUGH {
|
||||
fallthru = bld.block
|
||||
}
|
||||
}
|
||||
|
||||
if fallthru == nil {
|
||||
exits = append(exits, bld.block)
|
||||
}
|
||||
}
|
||||
if dfault != nil {
|
||||
list.addChild(dfault)
|
||||
} else {
|
||||
exits = append(exits, b0)
|
||||
}
|
||||
brek.setDestination(bld.newBlock(exits...))
|
||||
bld.breaks.pop()
|
||||
}
|
||||
|
||||
// An operation that might panic marks named function results as used.
|
||||
func (bld *builder) maybePanic() {
|
||||
if len(bld.results) == 0 {
|
||||
return
|
||||
}
|
||||
res := bld.results[len(bld.results)-1]
|
||||
if res == nil {
|
||||
return
|
||||
}
|
||||
for _, f := range res.List {
|
||||
for _, id := range f.Names {
|
||||
bld.use(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (bld *builder) newBlock(parents ...*block) *block {
|
||||
bld.block = &block{ops: map[*ast.Object][]operation{}}
|
||||
for _, b := range parents {
|
||||
b.addChild(bld.block)
|
||||
}
|
||||
return bld.block
|
||||
}
|
||||
|
||||
func (bld *builder) stmtLabel(s ast.Stmt) *ast.Object {
|
||||
if ls := bld.labelStmt; ls != nil && ls.Stmt == s {
|
||||
return ls.Label.Obj
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (bld *builder) assign(id *ast.Ident) {
|
||||
bld.newOp(id, true)
|
||||
}
|
||||
|
||||
func (bld *builder) use(id *ast.Ident) {
|
||||
bld.newOp(id, false)
|
||||
}
|
||||
|
||||
func (bld *builder) newOp(id *ast.Ident, assign bool) {
|
||||
if id.Name == "_" || id.Obj == nil {
|
||||
return
|
||||
}
|
||||
|
||||
v, ok := bld.vars[id.Obj]
|
||||
if !ok {
|
||||
v = &variable{}
|
||||
bld.vars[id.Obj] = v
|
||||
}
|
||||
v.escapes = v.escapes || v.fundept > 0 || bld.block == nil
|
||||
|
||||
if b := bld.block; b != nil {
|
||||
b.ops[id.Obj] = append(b.ops[id.Obj], operation{id, assign})
|
||||
}
|
||||
}
|
||||
|
||||
type branchStack []*branch
|
||||
|
||||
type branch struct {
|
||||
label *ast.Object
|
||||
srcs []*block
|
||||
dst *block
|
||||
}
|
||||
|
||||
func (s *branchStack) push(lbl *ast.Object) *branch {
|
||||
br := &branch{label: lbl}
|
||||
*s = append(*s, br)
|
||||
return br
|
||||
}
|
||||
|
||||
func (s *branchStack) get(lbl *ast.Ident) *branch {
|
||||
for i := len(*s) - 1; i >= 0; i-- {
|
||||
if br := (*s)[i]; lbl == nil || br.label == lbl.Obj {
|
||||
return br
|
||||
}
|
||||
}
|
||||
return s.push(lbl.Obj)
|
||||
}
|
||||
|
||||
func (br *branch) addSource(src *block) {
|
||||
br.srcs = append(br.srcs, src)
|
||||
if br.dst != nil {
|
||||
src.addChild(br.dst)
|
||||
}
|
||||
}
|
||||
|
||||
func (br *branch) setDestination(dst *block) {
|
||||
br.dst = dst
|
||||
for _, src := range br.srcs {
|
||||
src.addChild(dst)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *branchStack) pop() {
|
||||
*s = (*s)[:len(*s)-1]
|
||||
}
|
||||
|
||||
func ident(x ast.Expr) (*ast.Ident, bool) {
|
||||
if p, ok := x.(*ast.ParenExpr); ok {
|
||||
return ident(p.X)
|
||||
}
|
||||
id, ok := x.(*ast.Ident)
|
||||
return id, ok
|
||||
}
|
||||
|
||||
type checker struct {
|
||||
vars map[*ast.Object]*variable
|
||||
seen map[*block]bool
|
||||
ineff idents
|
||||
}
|
||||
|
||||
func (chk *checker) check(b *block) {
|
||||
if chk.seen[b] {
|
||||
return
|
||||
}
|
||||
chk.seen[b] = true
|
||||
|
||||
for obj, ops := range b.ops {
|
||||
if chk.vars[obj].escapes {
|
||||
continue
|
||||
}
|
||||
ops:
|
||||
for i, op := range ops {
|
||||
if !op.assign {
|
||||
continue
|
||||
}
|
||||
if i+1 < len(ops) {
|
||||
if ops[i+1].assign {
|
||||
chk.ineff = append(chk.ineff, op.id)
|
||||
}
|
||||
continue
|
||||
}
|
||||
seen := map[*block]bool{}
|
||||
for _, b := range b.children {
|
||||
if used(obj, b, seen) {
|
||||
continue ops
|
||||
}
|
||||
}
|
||||
chk.ineff = append(chk.ineff, op.id)
|
||||
}
|
||||
}
|
||||
|
||||
for _, b := range b.children {
|
||||
chk.check(b)
|
||||
}
|
||||
}
|
||||
|
||||
func used(obj *ast.Object, b *block, seen map[*block]bool) bool {
|
||||
if seen[b] {
|
||||
return false
|
||||
}
|
||||
seen[b] = true
|
||||
|
||||
if ops := b.ops[obj]; len(ops) > 0 {
|
||||
return !ops[0].assign
|
||||
}
|
||||
for _, b := range b.children {
|
||||
if used(obj, b, seen) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type idents []*ast.Ident
|
||||
|
||||
func (ids idents) Len() int { return len(ids) }
|
||||
func (ids idents) Less(i, j int) bool { return ids[i].Pos() < ids[j].Pos() }
|
||||
func (ids idents) Swap(i, j int) { ids[i], ids[j] = ids[j], ids[i] }
|
25
vendor/github.com/golangci/ineffassign/list
generated
vendored
Normal file
25
vendor/github.com/golangci/ineffassign/list
generated
vendored
Normal file
@ -0,0 +1,25 @@
|
||||
/Users/gordon/go/src/code.google.com/p/freetype-go/freetype/truetype/truetype.go:493:5: offset assigned and not used
|
||||
/Users/gordon/go/src/code.google.com/p/freetype-go/freetype/truetype/truetype.go:289:11: offset assigned and not used
|
||||
/Users/gordon/go/src/code.google.com/p/freetype-go/freetype/truetype/truetype_test.go:224:2: prefix assigned and not used
|
||||
/Users/gordon/go/src/code.google.com/p/freetype-go/freetype/truetype/truetype_test.go:239:3: s assigned and not used
|
||||
/Users/gordon/go/src/github.com/gordonklaus/flux/go/types/resolver.go:372:2: seenPkgs assigned and not used
|
||||
/Users/gordon/go/src/github.com/gopherjs/gopherjs/compiler/package.go:195:7: recvType assigned and not used
|
||||
/Users/gordon/go/src/golang.org/x/crypto/ocsp/ocsp.go:340:2: rest assigned and not used
|
||||
/Users/gordon/go/src/golang.org/x/crypto/openpgp/packet/opaque_test.go:35:6: err assigned and not used
|
||||
/Users/gordon/go/src/golang.org/x/crypto/otr/otr.go:641:6: in assigned and not used
|
||||
/Users/gordon/go/src/golang.org/x/crypto/otr/otr_test.go:198:17: err assigned and not used
|
||||
/Users/gordon/go/src/golang.org/x/crypto/ssh/benchmark_test.go:94:17: err assigned and not used
|
||||
/Users/gordon/go/src/golang.org/x/mobile/app/android.go:175:2: queue assigned and not used
|
||||
/Users/gordon/go/src/golang.org/x/mobile/cmd/gomobile/bind.go:411:2: w assigned and not used
|
||||
/Users/gordon/go/src/golang.org/x/mobile/cmd/gomobile/build.go:231:8: err assigned and not used
|
||||
/Users/gordon/go/src/golang.org/x/net/icmp/listen_posix.go:83:6: err assigned and not used
|
||||
/Users/gordon/go/src/golang.org/x/net/ipv4/control_unix.go:99:5: b assigned and not used
|
||||
/Users/gordon/go/src/golang.org/x/net/ipv4/control_unix.go:148:4: b assigned and not used
|
||||
/Users/gordon/go/src/golang.org/x/net/ipv6/control_unix.go:90:4: b assigned and not used
|
||||
/Users/gordon/go/src/golang.org/x/net/ipv6/control_unix.go:162:4: b assigned and not used
|
||||
/Users/gordon/go/src/golang.org/x/net/websocket/hybi.go:298:3: n assigned and not used
|
||||
/Users/gordon/go/src/golang.org/x/tools/cmd/callgraph/main.go:164:2: args assigned and not used
|
||||
/Users/gordon/go/src/golang.org/x/tools/cmd/cover/cover_test.go:52:2: err assigned and not used
|
||||
/Users/gordon/go/src/golang.org/x/tools/go/gcimporter/exportdata.go:74:13: size assigned and not used
|
||||
/Users/gordon/go/src/golang.org/x/tools/oracle/oracle.go:268:2: iprog assigned and not used
|
||||
/Users/gordon/go/src/golang.org/x/tools/oracle/oracle_test.go:299:2: iprog assigned and not used
|
131
vendor/github.com/golangci/ineffassign/liststd
generated
vendored
Normal file
131
vendor/github.com/golangci/ineffassign/liststd
generated
vendored
Normal file
@ -0,0 +1,131 @@
|
||||
/usr/local/go/src/bufio/scan.go:388:6: ineffectual assignment to width
|
||||
/usr/local/go/src/bufio/scan.go:396:6: ineffectual assignment to width
|
||||
/usr/local/go/src/bytes/buffer_test.go:141:6: ineffectual assignment to err
|
||||
/usr/local/go/src/bytes/buffer_test.go:164:3: ineffectual assignment to c
|
||||
/usr/local/go/src/cmd/cgo/out.go:799:3: ineffectual assignment to gccResult
|
||||
/usr/local/go/src/cmd/compile/internal/big/ratconv.go:170:4: ineffectual assignment to err
|
||||
/usr/local/go/src/cmd/compile/internal/gc/bimport.go:330:2: ineffectual assignment to file
|
||||
/usr/local/go/src/cmd/compile/internal/gc/cgen.go:3332:3: ineffectual assignment to max
|
||||
/usr/local/go/src/cmd/compile/internal/gc/export.go:379:2: ineffectual assignment to size
|
||||
/usr/local/go/src/cmd/compile/internal/gc/global_test.go:51:2: ineffectual assignment to out
|
||||
/usr/local/go/src/cmd/compile/internal/gc/lex.go:281:4: ineffectual assignment to c1
|
||||
/usr/local/go/src/cmd/compile/internal/gc/reg.go:1373:2: ineffectual assignment to firstf
|
||||
/usr/local/go/src/cmd/compile/internal/gc/reg.go:1381:3: ineffectual assignment to firstf
|
||||
/usr/local/go/src/cmd/compile/internal/s390x/peep.go:1048:3: ineffectual assignment to size
|
||||
/usr/local/go/src/cmd/compile/internal/s390x/peep.go:1139:3: ineffectual assignment to size
|
||||
/usr/local/go/src/cmd/compile/internal/ssa/loopbce.go:44:3: ineffectual assignment to entry
|
||||
/usr/local/go/src/cmd/cover/html.go:64:8: ineffectual assignment to err
|
||||
/usr/local/go/src/cmd/cover/html.go:66:8: ineffectual assignment to err
|
||||
/usr/local/go/src/cmd/go/build.go:3355:3: ineffectual assignment to cgoLDFLAGS
|
||||
/usr/local/go/src/cmd/internal/goobj/read.go:532:3: ineffectual assignment to data
|
||||
/usr/local/go/src/cmd/internal/obj/arm64/obj7.go:600:2: ineffectual assignment to aoffset
|
||||
/usr/local/go/src/cmd/internal/obj/mips/asm0.go:1049:3: ineffectual assignment to v
|
||||
/usr/local/go/src/cmd/internal/obj/mips/asm0.go:1101:3: ineffectual assignment to v
|
||||
/usr/local/go/src/cmd/internal/obj/s390x/objz.go:609:3: ineffectual assignment to pLast
|
||||
/usr/local/go/src/cmd/internal/pprof/profile/encode.go:279:12: ineffectual assignment to err
|
||||
/usr/local/go/src/cmd/link/internal/ld/dwarf.go:1426:2: ineffectual assignment to unitstart
|
||||
/usr/local/go/src/cmd/link/internal/ld/dwarf.go:1427:2: ineffectual assignment to headerstart
|
||||
/usr/local/go/src/cmd/link/internal/ld/dwarf.go:1428:2: ineffectual assignment to headerend
|
||||
/usr/local/go/src/cmd/link/internal/ld/elf.go:2272:3: ineffectual assignment to resoff
|
||||
/usr/local/go/src/cmd/vet/print.go:227:9: ineffectual assignment to w
|
||||
/usr/local/go/src/cmd/yacc/yacc.go:770:2: ineffectual assignment to val
|
||||
/usr/local/go/src/cmd/yacc/yacc.go:3127:2: ineffectual assignment to i
|
||||
/usr/local/go/src/compress/bzip2/huffman.go:114:4: ineffectual assignment to length
|
||||
/usr/local/go/src/compress/flate/reader_test.go:53:3: ineffectual assignment to buf0
|
||||
/usr/local/go/src/compress/flate/writer_test.go:29:3: ineffectual assignment to buf0
|
||||
/usr/local/go/src/compress/gzip/gzip_test.go:211:5: ineffectual assignment to err
|
||||
/usr/local/go/src/compress/lzw/reader_test.go:148:4: ineffectual assignment to buf0
|
||||
/usr/local/go/src/compress/lzw/writer_test.go:146:3: ineffectual assignment to buf0
|
||||
/usr/local/go/src/container/list/list_test.go:286:2: ineffectual assignment to e1
|
||||
/usr/local/go/src/container/list/list_test.go:286:6: ineffectual assignment to e2
|
||||
/usr/local/go/src/container/list/list_test.go:286:10: ineffectual assignment to e3
|
||||
/usr/local/go/src/container/list/list_test.go:286:14: ineffectual assignment to e4
|
||||
/usr/local/go/src/crypto/elliptic/p224.go:722:10: ineffectual assignment to bytes
|
||||
/usr/local/go/src/crypto/tls/handshake_messages.go:289:3: ineffectual assignment to z
|
||||
/usr/local/go/src/crypto/x509/verify.go:110:5: ineffectual assignment to certName
|
||||
/usr/local/go/src/database/sql/sql_test.go:1705:4: ineffectual assignment to numOpen
|
||||
/usr/local/go/src/database/sql/sql_test.go:1839:5: ineffectual assignment to err
|
||||
/usr/local/go/src/debug/dwarf/type.go:540:5: ineffectual assignment to haveBitOffset
|
||||
/usr/local/go/src/debug/elf/file.go:1014:3: ineffectual assignment to suffix
|
||||
/usr/local/go/src/debug/gosym/pclntab_test.go:256:2: ineffectual assignment to off
|
||||
/usr/local/go/src/debug/pe/file_test.go:309:2: ineffectual assignment to err
|
||||
/usr/local/go/src/encoding/base32/base32_test.go:120:4: ineffectual assignment to count
|
||||
/usr/local/go/src/encoding/base64/base64_test.go:174:4: ineffectual assignment to count
|
||||
/usr/local/go/src/encoding/gob/decgen.go:187:6: ineffectual assignment to err
|
||||
/usr/local/go/src/encoding/gob/encgen.go:166:6: ineffectual assignment to err
|
||||
/usr/local/go/src/encoding/json/encode.go:1071:2: ineffectual assignment to count
|
||||
/usr/local/go/src/encoding/json/encode.go:1169:6: ineffectual assignment to advance
|
||||
/usr/local/go/src/encoding/xml/xml.go:1030:6: ineffectual assignment to ok
|
||||
/usr/local/go/src/fmt/print.go:936:2: ineffectual assignment to afterIndex
|
||||
/usr/local/go/src/fmt/print.go:1051:15: ineffectual assignment to afterIndex
|
||||
/usr/local/go/src/go/ast/filter.go:84:3: ineffectual assignment to keepField
|
||||
/usr/local/go/src/go/internal/gcimporter/bimport.go:215:2: ineffectual assignment to file
|
||||
/usr/local/go/src/go/printer/nodes.go:439:4: ineffectual assignment to extraTabs
|
||||
/usr/local/go/src/go/printer/printer_test.go:155:8: ineffectual assignment to err
|
||||
/usr/local/go/src/go/types/conversions.go:49:2: ineffectual assignment to final
|
||||
/usr/local/go/src/html/template/css.go:160:2: ineffectual assignment to r
|
||||
/usr/local/go/src/html/template/css.go:160:5: ineffectual assignment to w
|
||||
/usr/local/go/src/html/template/html.go:141:2: ineffectual assignment to r
|
||||
/usr/local/go/src/html/template/html.go:141:5: ineffectual assignment to w
|
||||
/usr/local/go/src/html/template/js.go:249:2: ineffectual assignment to r
|
||||
/usr/local/go/src/html/template/js.go:249:5: ineffectual assignment to w
|
||||
/usr/local/go/src/image/decode_test.go:125:9: ineffectual assignment to err
|
||||
/usr/local/go/src/image/png/reader.go:689:2: ineffectual assignment to n
|
||||
/usr/local/go/src/image/png/writer.go:269:3: ineffectual assignment to best
|
||||
/usr/local/go/src/io/io_test.go:245:2: ineffectual assignment to n
|
||||
/usr/local/go/src/io/ioutil/ioutil.go:149:2: ineffectual assignment to readSize
|
||||
/usr/local/go/src/io/ioutil/ioutil_test.go:24:2: ineffectual assignment to contents
|
||||
/usr/local/go/src/log/syslog/syslog_test.go:236:5: ineffectual assignment to err
|
||||
/usr/local/go/src/log/syslog/syslog_test.go:240:5: ineffectual assignment to err
|
||||
/usr/local/go/src/math/big/ratconv.go:176:4: ineffectual assignment to err
|
||||
/usr/local/go/src/mime/multipart/multipart_test.go:408:2: ineffectual assignment to p
|
||||
/usr/local/go/src/net/dial_test.go:381:6: ineffectual assignment to err
|
||||
/usr/local/go/src/net/dnsname_test.go:36:6: ineffectual assignment to char63
|
||||
/usr/local/go/src/net/dnsname_test.go:37:6: ineffectual assignment to char64
|
||||
/usr/local/go/src/net/fd_plan9.go:64:4: ineffectual assignment to err
|
||||
/usr/local/go/src/net/fd_windows.go:166:3: ineffectual assignment to err
|
||||
/usr/local/go/src/net/http/fs.go:413:5: ineffectual assignment to name
|
||||
/usr/local/go/src/net/http/h2_bundle.go:6249:4: ineffectual assignment to n
|
||||
/usr/local/go/src/net/http/request_test.go:155:13: ineffectual assignment to err
|
||||
/usr/local/go/src/net/http/serve_test.go:4053:13: ineffectual assignment to err
|
||||
/usr/local/go/src/net/http/transport_test.go:729:8: ineffectual assignment to err
|
||||
/usr/local/go/src/net/http/transport_test.go:2345:3: ineffectual assignment to slurp
|
||||
/usr/local/go/src/net/parse.go:27:2: ineffectual assignment to i
|
||||
/usr/local/go/src/net/rpc/server.go:270:3: ineffectual assignment to str
|
||||
/usr/local/go/src/net/udpsock_plan9.go:80:16: ineffectual assignment to i
|
||||
/usr/local/go/src/os/env_test.go:109:2: ineffectual assignment to value
|
||||
/usr/local/go/src/os/os_test.go:1080:5: ineffectual assignment to err
|
||||
/usr/local/go/src/os/path_test.go:122:2: ineffectual assignment to testit
|
||||
/usr/local/go/src/reflect/type.go:2379:3: ineffectual assignment to name
|
||||
/usr/local/go/src/regexp/exec.go:123:2: ineffectual assignment to r
|
||||
/usr/local/go/src/regexp/exec.go:124:2: ineffectual assignment to width
|
||||
/usr/local/go/src/regexp/exec.go:321:2: ineffectual assignment to r
|
||||
/usr/local/go/src/regexp/exec.go:322:2: ineffectual assignment to width
|
||||
/usr/local/go/src/regexp/onepass.go:338:15: ineffectual assignment to matchArg
|
||||
/usr/local/go/src/regexp/syntax/parse.go:577:2: ineffectual assignment to start
|
||||
/usr/local/go/src/runtime/lfstack_test.go:48:2: ineffectual assignment to nodes
|
||||
/usr/local/go/src/runtime/mbitmap.go:1458:3: ineffectual assignment to i
|
||||
/usr/local/go/src/runtime/mfinal_test.go:60:4: ineffectual assignment to v
|
||||
/usr/local/go/src/runtime/mfinal_test.go:98:3: ineffectual assignment to v
|
||||
/usr/local/go/src/runtime/mgcmark.go:414:2: ineffectual assignment to stolen
|
||||
/usr/local/go/src/runtime/mgcsweep.go:188:2: ineffectual assignment to nfree
|
||||
/usr/local/go/src/runtime/os_plan9.go:307:2: ineffectual assignment to n
|
||||
/usr/local/go/src/runtime/pprof/pprof.go:465:5: ineffectual assignment to ok
|
||||
/usr/local/go/src/runtime/pprof/pprof.go:608:5: ineffectual assignment to ok
|
||||
/usr/local/go/src/runtime/pprof/pprof.go:751:5: ineffectual assignment to ok
|
||||
/usr/local/go/src/runtime/proc.go:4227:3: ineffectual assignment to xname
|
||||
/usr/local/go/src/runtime/runtime1.go:360:3: ineffectual assignment to field
|
||||
/usr/local/go/src/runtime/runtime_mmap_test.go:25:2: ineffectual assignment to p
|
||||
/usr/local/go/src/runtime/softfloat64.go:228:3: ineffectual assignment to f
|
||||
/usr/local/go/src/runtime/softfloat64.go:228:6: ineffectual assignment to g
|
||||
/usr/local/go/src/runtime/stack_test.go:106:4: ineffectual assignment to s
|
||||
/usr/local/go/src/strconv/quote.go:23:6: ineffectual assignment to width
|
||||
/usr/local/go/src/sync/atomic/atomic_test.go:1122:2: ineffectual assignment to new
|
||||
/usr/local/go/src/sync/atomic/atomic_test.go:1150:2: ineffectual assignment to new
|
||||
/usr/local/go/src/syscall/dir_plan9.go:88:2: ineffectual assignment to b
|
||||
/usr/local/go/src/syscall/dir_plan9.go:131:13: ineffectual assignment to b
|
||||
/usr/local/go/src/syscall/exec_plan9.go:281:2: ineffectual assignment to r1
|
||||
/usr/local/go/src/syscall/mksyscall_windows.go:310:2: ineffectual assignment to s
|
||||
/usr/local/go/src/syscall/syscall_bsd_test.go:23:2: ineffectual assignment to n
|
||||
/usr/local/go/src/syscall/syscall_unix_test.go:187:17: ineffectual assignment to err
|
||||
/usr/local/go/src/text/template/multi_test.go:249:9: ineffectual assignment to err
|
Loading…
x
Reference in New Issue
Block a user