{"action":"opened","issue":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/1505","labels_url":"https://api.github.com/repos/dotnet/coreclr/issues/1505/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/coreclr/issues/1505/comments","events_url":"https://api.github.com/repos/dotnet/coreclr/issues/1505/events","html_url":"https://github.com/dotnet/coreclr/issues/1505","id":105077252,"number":1505,"title":"RyuJIT should eliminate unnecessary try/finally","user":{"login":"mikedn","id":9973420,"avatar_url":"https://avatars.githubusercontent.com/u/9973420?v=3","gravatar_id":"","url":"https://api.github.com/users/mikedn","html_url":"https://github.com/mikedn","followers_url":"https://api.github.com/users/mikedn/followers","following_url":"https://api.github.com/users/mikedn/following{/other_user}","gists_url":"https://api.github.com/users/mikedn/gists{/gist_id}","starred_url":"https://api.github.com/users/mikedn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mikedn/subscriptions","organizations_url":"https://api.github.com/users/mikedn/orgs","repos_url":"https://api.github.com/users/mikedn/repos","events_url":"https://api.github.com/users/mikedn/events{/privacy}","received_events_url":"https://api.github.com/users/mikedn/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-09-06T07:38:07Z","updated_at":"2015-09-06T07:38:07Z","closed_at":null,"body":"If the `finally` block in a `try`/`finally` pair does nothing then they can be eliminated. Such `finally` blocks are produced when `foreach` is used with BCL collections such as `List<T>` because their enumerators have empty Dispose methods. The JIT compiler inlines the Dispose method and the callsite and null check are eliminated but the `try`/`finally` is left behind.\r\n\r\nAlso, I'm not very familiar with exception handling but it seems that it should be possible to inline the finally handler if it's small. The handlers generated when `foreach` is used with `IEnumerable<T>` consist of a null check and a call - `if (x != null) x.Dispose();`. Incidentally this code isn't even optimized, the generated code contains a redundant load and null check:\r\n\r\n```\r\n       488B4DF0             mov      rcx, gword ptr [rbp-10H]\r\n       4885C9               test     rcx, rcx\r\n       7413                 je       SHORT G_M65129_IG11\r\n       488B4DF0             mov      rcx, gword ptr [rbp-10H]\r\n       49BB38000601FC7F0000 mov      r11, 0x7FFC01060038\r\n       3909                 cmp      dword ptr [rcx], ecx\r\n       41FF13               call     qword ptr [r11]System.IDisposable:Dispose():this\r\n```\r\n\r\nIt's unfortunate that such a common code pattern isn't better handled.\r\n"}}