diff options
| author | Teddy Wing | 2023-05-15 03:05:27 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2023-05-15 03:05:27 +0200 | 
| commit | 30fed3b90b79c6aa1221f933e82b52dc5b07b5a6 (patch) | |
| tree | 29d17a93be42d4216371dff17ca0220aece7ef15 | |
| parent | 567f4e859ad48c9e2b01559f771cd20103c7903c (diff) | |
| download | gocapturedrefrace-30fed3b90b79c6aa1221f933e82b52dc5b07b5a6.tar.bz2 | |
Ignore variables from the closure's formal arguments
Don't report variables that were passed as arguments to the closure.
| -rw-r--r-- | gocapturedrefrace.go | 12 | 
1 files changed, 9 insertions, 3 deletions
| diff --git a/gocapturedrefrace.go b/gocapturedrefrace.go index 9544d48..454156f 100644 --- a/gocapturedrefrace.go +++ b/gocapturedrefrace.go @@ -51,11 +51,11 @@ func run(pass *analysis.Pass) (interface{}, error) {  }  func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) { -	fmt.Print("Params: ") +	formalParams := []*ast.Object{}  	for _, field := range funcLit.Type.Params.List { -		fmt.Printf("%#v, ", field.Names[0].Name) +		formalParams = append(formalParams, field.Names[0].Obj)  	} -	fmt.Println() +	fmt.Printf("%#v\n", formalParams)  	ast.Inspect(  		funcLit, @@ -72,6 +72,12 @@ func checkClosure(pass *analysis.Pass, funcLit *ast.FuncLit) {  			// TODO: Find out whether ident is a captured reference  			// Maybe check if variable was not assigned or passed as an argument? +			for _, param := range formalParams { +				if ident.Obj == param { +					return true +				} +			} +  			pass.Reportf(  				ident.Pos(),  				"variable found %q", | 
