update scopelint (#680)

This commit is contained in:
Isaev Denis 2019-09-11 17:35:08 +03:00 committed by GitHub
parent 94eaa8f196
commit b08335850e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -26,9 +26,9 @@ func (lint Scopelint) Run(ctx context.Context, lintCtx *linter.Context) ([]resul
for _, f := range lintCtx.ASTCache.GetAllValidFiles() { for _, f := range lintCtx.ASTCache.GetAllValidFiles() {
n := Node{ n := Node{
fset: f.Fset, fset: f.Fset,
dangerObjects: map[*ast.Object]struct{}{}, DangerObjects: map[*ast.Object]int{},
unsafeObjects: map[*ast.Object]struct{}{}, UnsafeObjects: map[*ast.Object]int{},
skipFuncs: map[*ast.FuncLit]struct{}{}, SkipFuncs: map[*ast.FuncLit]int{},
issues: &res, issues: &res,
} }
ast.Walk(&n, f.F) ast.Walk(&n, f.F)
@ -37,14 +37,14 @@ func (lint Scopelint) Run(ctx context.Context, lintCtx *linter.Context) ([]resul
return res, nil return res, nil
} }
// The code below is copy-pasted from https://github.com/kyoh86/scopelint // The code below is copy-pasted from https://github.com/kyoh86/scopelint 92cbe2cc9276abda0e309f52cc9e309d407f174e
// Node represents a Node being linted. // Node represents a Node being linted.
type Node struct { type Node struct {
fset *token.FileSet fset *token.FileSet
dangerObjects map[*ast.Object]struct{} DangerObjects map[*ast.Object]int
unsafeObjects map[*ast.Object]struct{} UnsafeObjects map[*ast.Object]int
skipFuncs map[*ast.FuncLit]struct{} SkipFuncs map[*ast.FuncLit]int
issues *[]result.Issue issues *[]result.Issue
} }
@ -60,7 +60,7 @@ func (f *Node) Visit(node ast.Node) ast.Visitor {
for _, lh := range init.Lhs { for _, lh := range init.Lhs {
switch tlh := lh.(type) { switch tlh := lh.(type) {
case *ast.Ident: case *ast.Ident:
f.unsafeObjects[tlh.Obj] = struct{}{} f.UnsafeObjects[tlh.Obj] = 0
} }
} }
} }
@ -69,25 +69,25 @@ func (f *Node) Visit(node ast.Node) ast.Visitor {
// Memory variables declarated in range statement // Memory variables declarated in range statement
switch k := typedNode.Key.(type) { switch k := typedNode.Key.(type) {
case *ast.Ident: case *ast.Ident:
f.unsafeObjects[k.Obj] = struct{}{} f.UnsafeObjects[k.Obj] = 0
} }
switch v := typedNode.Value.(type) { switch v := typedNode.Value.(type) {
case *ast.Ident: case *ast.Ident:
f.unsafeObjects[v.Obj] = struct{}{} f.UnsafeObjects[v.Obj] = 0
} }
case *ast.UnaryExpr: case *ast.UnaryExpr:
if typedNode.Op == token.AND { if typedNode.Op == token.AND {
switch ident := typedNode.X.(type) { switch ident := typedNode.X.(type) {
case *ast.Ident: case *ast.Ident:
if _, unsafe := f.unsafeObjects[ident.Obj]; unsafe { if _, unsafe := f.UnsafeObjects[ident.Obj]; unsafe {
f.errorf(ident, "Using a reference for the variable on range scope %s", formatCode(ident.Name, nil)) f.errorf(ident, "Using a reference for the variable on range scope %s", formatCode(ident.Name, nil))
} }
} }
} }
case *ast.Ident: case *ast.Ident:
if _, obj := f.dangerObjects[typedNode.Obj]; obj { if _, obj := f.DangerObjects[typedNode.Obj]; obj {
// It is the naked variable in scope of range statement. // It is the naked variable in scope of range statement.
f.errorf(node, "Using the variable on range scope %s in function literal", formatCode(typedNode.Name, nil)) f.errorf(node, "Using the variable on range scope %s in function literal", formatCode(typedNode.Name, nil))
break break
@ -97,26 +97,43 @@ func (f *Node) Visit(node ast.Node) ast.Visitor {
// Ignore func literals that'll be called immediately. // Ignore func literals that'll be called immediately.
switch funcLit := typedNode.Fun.(type) { switch funcLit := typedNode.Fun.(type) {
case *ast.FuncLit: case *ast.FuncLit:
f.skipFuncs[funcLit] = struct{}{} f.SkipFuncs[funcLit] = 0
} }
case *ast.FuncLit: case *ast.FuncLit:
if _, skip := f.skipFuncs[typedNode]; !skip { if _, skip := f.SkipFuncs[typedNode]; !skip {
dangers := map[*ast.Object]struct{}{} dangers := map[*ast.Object]int{}
for d := range f.dangerObjects { for d := range f.DangerObjects {
dangers[d] = struct{}{} dangers[d] = 0
} }
for u := range f.unsafeObjects { for u := range f.UnsafeObjects {
dangers[u] = struct{}{} dangers[u] = 0
f.UnsafeObjects[u]++
} }
return &Node{ return &Node{
fset: f.fset, fset: f.fset,
dangerObjects: dangers, DangerObjects: dangers,
unsafeObjects: f.unsafeObjects, UnsafeObjects: f.UnsafeObjects,
skipFuncs: f.skipFuncs, SkipFuncs: f.SkipFuncs,
issues: f.issues, issues: f.issues,
} }
} }
case *ast.ReturnStmt:
unsafe := map[*ast.Object]int{}
for u := range f.UnsafeObjects {
if f.UnsafeObjects[u] == 0 {
continue
}
unsafe[u] = f.UnsafeObjects[u]
}
return &Node{
fset: f.fset,
DangerObjects: f.DangerObjects,
UnsafeObjects: unsafe,
SkipFuncs: f.SkipFuncs,
issues: f.issues,
}
} }
return f return f
} }