{"action":"opened","issue":{"url":"https://api.github.com/repos/dotnet/roslyn/issues/2235","labels_url":"https://api.github.com/repos/dotnet/roslyn/issues/2235/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/roslyn/issues/2235/comments","events_url":"https://api.github.com/repos/dotnet/roslyn/issues/2235/events","html_url":"https://github.com/dotnet/roslyn/issues/2235","id":70693220,"number":2235,"title":"Proposal: create analyzer+fixer for nameof() usage","user":{"login":"bartkoelman","id":10324372,"avatar_url":"https://avatars.githubusercontent.com/u/10324372?v=3","gravatar_id":"","url":"https://api.github.com/users/bartkoelman","html_url":"https://github.com/bartkoelman","followers_url":"https://api.github.com/users/bartkoelman/followers","following_url":"https://api.github.com/users/bartkoelman/following{/other_user}","gists_url":"https://api.github.com/users/bartkoelman/gists{/gist_id}","starred_url":"https://api.github.com/users/bartkoelman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bartkoelman/subscriptions","organizations_url":"https://api.github.com/users/bartkoelman/orgs","repos_url":"https://api.github.com/users/bartkoelman/repos","events_url":"https://api.github.com/users/bartkoelman/events{/privacy}","received_events_url":"https://api.github.com/users/bartkoelman/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-04-24T13:38:45Z","updated_at":"2015-04-24T13:38:45Z","closed_at":null,"body":"@pharring suggested to create an analyzer+fixer for #2179.\r\n\r\nTo me this sounds like an interesting challenge. So I have been thinking how that would work.\r\n\r\nOne approach is that the analyzer scans all method bodies, flagging all cases where a string literal equals the name of one of the method's parameters.\r\nAside from maybe expensive to run, it would potentially flag cases it should not, such as:\r\n\r\n    public void SetAddress(string address)\r\n    {\r\n        label1.Text = \"address\"; // WARN: use nameof()\r\n        textBox1.Text = address;\r\n    }\r\n\r\nAnother approach could be to scan all method bodies for ctor calls to ArgumentException, ArgumentNullException and a few more, like I manually did for #2179. This would benefit the Roslyn codebase, but many codebases that I've seen have helpers such as:\r\n\r\n    public static class Guard\r\n    {\r\n        public static void AssertNotNull<T>(string paramName, T paramValue)\r\n        {\r\n            if (paramValue == null)\r\n            {\r\n                throw new ArgumentNullException(paramName);\r\n            }\r\n        }\r\n    }\r\n\r\nand in that case, users will not profit from such an analyzer. Maybe they expect it to flag all calls into Guard.ArgumentNotNull(...), which it would not.\r\n\r\nPersonally I like @jskeet's solution, but it is only helpful when using Resharper's InvokerParameterName annotation:\r\n\r\nhttps://github.com/nodatime/nodatime/search?utf8=%E2%9C%93&q=invokerparametername\r\n\r\nFor those unfamiliar, [InvokerParameterName] is telling ReSharper that the paramName argument is the name of one of the parameters from the calling (invoking) method, so it will provide code completion that lists all the calling method parameters. This will give a warning in ReSharper if the name is not a parameter, for example, a local variable name.\r\n\r\nI'm open to suggestions. Any thoughts what would be feasible for such an analyzer that lives in the Roslyn codebase?"}}