@@ -26,9 +26,9 @@ func (lint Scopelint) Run(ctx context.Context, lintCtx *linter.Context) ([]resul
2626 for _ , f := range lintCtx .ASTCache .GetAllValidFiles () {
2727 n := Node {
2828 fset : f .Fset ,
29- dangerObjects : map [* ast.Object ]struct {} {},
30- unsafeObjects : map [* ast.Object ]struct {} {},
31- skipFuncs : map [* ast.FuncLit ]struct {} {},
29+ DangerObjects : map [* ast.Object ]int {},
30+ UnsafeObjects : map [* ast.Object ]int {},
31+ SkipFuncs : map [* ast.FuncLit ]int {},
3232 issues : & res ,
3333 }
3434 ast .Walk (& n , f .F )
@@ -37,14 +37,14 @@ func (lint Scopelint) Run(ctx context.Context, lintCtx *linter.Context) ([]resul
3737 return res , nil
3838}
3939
40- // The code below is copy-pasted from https://github.com/kyoh86/scopelint
40+ // The code below is copy-pasted from https://github.com/kyoh86/scopelint 92cbe2cc9276abda0e309f52cc9e309d407f174e
4141
4242// Node represents a Node being linted.
4343type Node struct {
4444 fset * token.FileSet
45- dangerObjects map [* ast.Object ]struct {}
46- unsafeObjects map [* ast.Object ]struct {}
47- skipFuncs map [* ast.FuncLit ]struct {}
45+ DangerObjects map [* ast.Object ]int
46+ UnsafeObjects map [* ast.Object ]int
47+ SkipFuncs map [* ast.FuncLit ]int
4848 issues * []result.Issue
4949}
5050
@@ -60,7 +60,7 @@ func (f *Node) Visit(node ast.Node) ast.Visitor {
6060 for _ , lh := range init .Lhs {
6161 switch tlh := lh .(type ) {
6262 case * ast.Ident :
63- f .unsafeObjects [tlh .Obj ] = struct {}{}
63+ f .UnsafeObjects [tlh .Obj ] = 0
6464 }
6565 }
6666 }
@@ -69,25 +69,25 @@ func (f *Node) Visit(node ast.Node) ast.Visitor {
6969 // Memory variables declarated in range statement
7070 switch k := typedNode .Key .(type ) {
7171 case * ast.Ident :
72- f .unsafeObjects [k .Obj ] = struct {}{}
72+ f .UnsafeObjects [k .Obj ] = 0
7373 }
7474 switch v := typedNode .Value .(type ) {
7575 case * ast.Ident :
76- f .unsafeObjects [v .Obj ] = struct {}{}
76+ f .UnsafeObjects [v .Obj ] = 0
7777 }
7878
7979 case * ast.UnaryExpr :
8080 if typedNode .Op == token .AND {
8181 switch ident := typedNode .X .(type ) {
8282 case * ast.Ident :
83- if _ , unsafe := f .unsafeObjects [ident .Obj ]; unsafe {
83+ if _ , unsafe := f .UnsafeObjects [ident .Obj ]; unsafe {
8484 f .errorf (ident , "Using a reference for the variable on range scope %s" , formatCode (ident .Name , nil ))
8585 }
8686 }
8787 }
8888
8989 case * ast.Ident :
90- if _ , obj := f .dangerObjects [typedNode .Obj ]; obj {
90+ if _ , obj := f .DangerObjects [typedNode .Obj ]; obj {
9191 // It is the naked variable in scope of range statement.
9292 f .errorf (node , "Using the variable on range scope %s in function literal" , formatCode (typedNode .Name , nil ))
9393 break
@@ -97,26 +97,43 @@ func (f *Node) Visit(node ast.Node) ast.Visitor {
9797 // Ignore func literals that'll be called immediately.
9898 switch funcLit := typedNode .Fun .(type ) {
9999 case * ast.FuncLit :
100- f .skipFuncs [funcLit ] = struct {}{}
100+ f .SkipFuncs [funcLit ] = 0
101101 }
102102
103103 case * ast.FuncLit :
104- if _ , skip := f .skipFuncs [typedNode ]; ! skip {
105- dangers := map [* ast.Object ]struct {} {}
106- for d := range f .dangerObjects {
107- dangers [d ] = struct {}{}
104+ if _ , skip := f .SkipFuncs [typedNode ]; ! skip {
105+ dangers := map [* ast.Object ]int {}
106+ for d := range f .DangerObjects {
107+ dangers [d ] = 0
108108 }
109- for u := range f .unsafeObjects {
110- dangers [u ] = struct {}{}
109+ for u := range f .UnsafeObjects {
110+ dangers [u ] = 0
111+ f .UnsafeObjects [u ]++
111112 }
112113 return & Node {
113114 fset : f .fset ,
114- dangerObjects : dangers ,
115- unsafeObjects : f .unsafeObjects ,
116- skipFuncs : f .skipFuncs ,
115+ DangerObjects : dangers ,
116+ UnsafeObjects : f .UnsafeObjects ,
117+ SkipFuncs : f .SkipFuncs ,
117118 issues : f .issues ,
118119 }
119120 }
121+
122+ case * ast.ReturnStmt :
123+ unsafe := map [* ast.Object ]int {}
124+ for u := range f .UnsafeObjects {
125+ if f .UnsafeObjects [u ] == 0 {
126+ continue
127+ }
128+ unsafe [u ] = f .UnsafeObjects [u ]
129+ }
130+ return & Node {
131+ fset : f .fset ,
132+ DangerObjects : f .DangerObjects ,
133+ UnsafeObjects : unsafe ,
134+ SkipFuncs : f .SkipFuncs ,
135+ issues : f .issues ,
136+ }
120137 }
121138 return f
122139}
0 commit comments