{"action":"opened","issue":{"url":"https://api.github.com/repos/go-critic/go-critic/issues/801","repository_url":"https://api.github.com/repos/go-critic/go-critic","labels_url":"https://api.github.com/repos/go-critic/go-critic/issues/801/labels{/name}","comments_url":"https://api.github.com/repos/go-critic/go-critic/issues/801/comments","events_url":"https://api.github.com/repos/go-critic/go-critic/issues/801/events","html_url":"https://github.com/go-critic/go-critic/issues/801","id":408010637,"node_id":"MDU6SXNzdWU0MDgwMTA2Mzc=","number":801,"title":"lint: add modify-local-in-defer check","user":{"login":"sergey-shambir","id":1856842,"node_id":"MDQ6VXNlcjE4NTY4NDI=","avatar_url":"https://avatars2.githubusercontent.com/u/1856842?v=4","gravatar_id":"","url":"https://api.github.com/users/sergey-shambir","html_url":"https://github.com/sergey-shambir","followers_url":"https://api.github.com/users/sergey-shambir/followers","following_url":"https://api.github.com/users/sergey-shambir/following{/other_user}","gists_url":"https://api.github.com/users/sergey-shambir/gists{/gist_id}","starred_url":"https://api.github.com/users/sergey-shambir/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sergey-shambir/subscriptions","organizations_url":"https://api.github.com/users/sergey-shambir/orgs","repos_url":"https://api.github.com/users/sergey-shambir/repos","events_url":"https://api.github.com/users/sergey-shambir/events{/privacy}","received_events_url":"https://api.github.com/users/sergey-shambir/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2019-02-08T05:23:30Z","updated_at":"2019-02-08T05:23:30Z","closed_at":null,"author_association":"NONE","body":"Our team faced with code that modifies variable in `defer` after `return` directive:\r\n* [bad code](https://play.golang.org/p/FXJJOHzLoqc)\r\n* [fixed code](https://play.golang.org/p/da3Ag_Zw1NL)\r\n\r\n```go\r\n// incorrect code - \"one\" never appended since defer modifies local variable\r\nfunc test() ([]string) {\r\n\tresult := make([]string, 0, 3)\r\n\tdefer func() {\r\n\t\tresult = append(result, \"one\")\r\n\t}()\r\n\treturn result\r\n}\r\n\r\n// correct code - \"one\" appended since defer modifies named return value\r\nfunc test() (result []string) {\r\n\tresult = make([]string, 0, 3)\r\n\tdefer func() {\r\n\t\tresult = append(result, \"one\")\r\n\t}()\r\n\treturn result\r\n}\r\n```\r\n\r\nI'm not sure if there are any valid reasons to assign variables used with `return` inside `defer`."}}
{"actor":{"display_login":"sergey-shambir"}}