diff options
| author | Teddy Wing | 2023-05-15 22:13:35 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2023-05-15 22:13:35 +0200 | 
| commit | 25249a93b1937f4b9748420dbaea03161e4c6f04 (patch) | |
| tree | 0ffa785b6b12213962ba836b9dd8e1dccf5fce2f | |
| parent | e4441d0d2e003d5a4adafc9a9aaa037b3be830b8 (diff) | |
| download | gocapturedrefrace-25249a93b1937f4b9748420dbaea03161e4c6f04.tar.bz2 | |
assignmentsInFunc: Try checking `(*AssignStmt).Tok`
This field could maybe tell us whether it was a `:=` assignment
(token.DEFINE) or `=` (token.ASSIGN), but it seems like it's always
`token.DEFINE`.
| -rw-r--r-- | gocapturedrefrace.go | 73 | 
1 files changed, 45 insertions, 28 deletions
| diff --git a/gocapturedrefrace.go b/gocapturedrefrace.go index 9430faf..8c09278 100644 --- a/gocapturedrefrace.go +++ b/gocapturedrefrace.go @@ -63,6 +63,7 @@ func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) {  	assignments := assignmentsInFunc(pass, funcLit)  	fmt.Printf("variable declarations: %#v\n", assignments)  	// TODO: Use ast.GenDecl instead +	// ast.Scope?  	ast.Inspect(  		funcLit, @@ -99,49 +100,65 @@ func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) {  func assignmentsInFunc(  	pass *analysis.Pass,  	funcLit *ast.FuncLit, -) []*ast.Object { -	assignments := []*ast.Object{} +) []string { +	assignments := []string{} +	// ) []*ast.Object { +	// 	assignments := []*ast.Object{}  	ast.Inspect(  		funcLit,  		func(node ast.Node) bool { -			decl, ok := node.(*ast.GenDecl) -			if !ok { -				return true -			} - -			fmt.Printf("decl: %#v\n", decl) - -			if decl.Tok != token.VAR { -				return true -			} - -			for _, spec := range decl.Specs { -				valueSpec, ok := spec.(*ast.ValueSpec) -				if !ok { -					return true -				} - -				fmt.Printf("valueSpec: %#v\n", valueSpec) - -				assignments = append(assignments, valueSpec.Names[0].Obj) -			} - -			// ident, ok := node.(*ast.Ident) +			// decl, ok := node.(*ast.GenDecl)  			// if !ok {  			// 	return true  			// }  			// -			// if ident.Obj == nil || ident.Obj.Decl == nil { +			// fmt.Printf("decl: %#v\n", decl) +			// +			// if decl.Tok != token.VAR {  			// 	return true  			// }  			// -			// _, ok = ident.Obj.Decl.(*ast.AssignStmt) +			// for _, spec := range decl.Specs { +			// 	valueSpec, ok := spec.(*ast.ValueSpec) +			// 	if !ok { +			// 		return true +			// 	} +			// +			// 	fmt.Printf("valueSpec: %#v\n", valueSpec) +			// +			// 	assignments = append(assignments, valueSpec.Names[0].Obj) +			// } + +			// decl, ok := node.(*ast.DeclStmt)  			// if !ok {  			// 	return true  			// }  			// -			// assignments = append(assignments, ident.Name) +			// fmt.Printf("decl: %#v\n", decl) + +			ident, ok := node.(*ast.Ident) +			if !ok { +				return true +			} + +			if ident.Obj == nil || ident.Obj.Decl == nil { +				return true +			} + +			assignment, ok := ident.Obj.Decl.(*ast.AssignStmt) +			if !ok { +				return true +			} + +			// fmt.Printf("assignment: %#v\n", assignment.Tok) +			if assignment.Tok == token.DEFINE { +				fmt.Printf("assignment: %v is DEFINE\n", ident.Name) +			} else if assignment.Tok == token.ASSIGN { +				fmt.Printf("assignment: %v is ASSIGN\n", ident.Name) +			} + +			assignments = append(assignments, ident.Name)  			return true  		}, | 
