{"id":23818,"date":"2019-07-10T09:40:11","date_gmt":"2019-07-10T16:40:11","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/dotnet\/?p=23818"},"modified":"2019-07-10T09:38:21","modified_gmt":"2019-07-10T16:38:21","slug":"write-better-code-faster-with-roslyn-analyzers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/write-better-code-faster-with-roslyn-analyzers\/","title":{"rendered":"Write Better Code Faster with Roslyn Analyzers"},"content":{"rendered":"<p>Roslyn, the .NET compiler platform, helps you catch bugs even before you run your code. One example is Roslyn\u2019s spellcheck analyzer that is built into Visual Studio. Let\u2019s say you are creating a static method and misspelled the word <em>static<\/em> as <em>statc<\/em>. You will be able to see this spelling error before you run your code because Roslyn can produce warnings in your code as you type even before you\u2019ve finished the line. In other words, you don\u2019t have to build your code to find out that you made a mistake.<\/p>\n<p><img decoding=\"async\" class=\"wp-image-23843 size-full aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2019\/07\/roslyn-blog-pic.png\" alt=\"Roslyn Analyzer Spell Check\" width=\"687\" height=\"182\" srcset=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2019\/07\/roslyn-blog-pic.png 687w, https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2019\/07\/roslyn-blog-pic-300x79.png 300w\" sizes=\"(max-width: 687px) 100vw, 687px\" \/><\/p>\n<p>Roslyn analyzers can also surface an automatic code fix through the Visual Studio light bulb icon that allows you to fix your code immediately.<\/p>\n<p><span style=\"font-size: 18pt;\"><strong>But what if you could catch even more bugs?<\/strong><\/span><\/p>\n<p>Let me introduce you to Roslyn analyzer packages. These collections of analyzers provide a more verbose analysis, but don&#8217;t ship with the default Visual Studio tools. To learn more about our favorite Roslyn analyzers visit our <a href=\"https:\/\/github.com\/dotnet\/roslyn-analyzers\">Roslyn analyzers GitHub repository<\/a>. This repository includes the FxCop rules that are still applicable to modern software development, but now target our modern code analysis platform based on Roslyn. Let&#8217;s go ahead and install this package to be more productive and write better code faster!<\/p>\n<p>To Install FxCop analyzers as a NuGet package:<\/p>\n<ol>\n<li>Assuming you\u2019re using Visual Studio 2017 Version 15.8 or newer, choose the most recent version of <a href=\"https:\/\/www.nuget.org\/packages\/Microsoft.CodeAnalysis.FxCopAnalyzers\">Microsoft.CodeAnalysis.FxCopAnalyzers<\/a>.<\/li>\n<li>Install the package in Visual Studio, using the <a href=\"https:\/\/docs.microsoft.com\/nuget\/quickstart\/install-and-use-a-package-in-visual-studio\">Package Manager UI<\/a>.<\/li>\n<\/ol>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-23844 size-full\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2019\/07\/roslyn-nuget-package.png\" alt=\"Roslyn FxCop Nuget Package\" width=\"875\" height=\"251\" srcset=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2019\/07\/roslyn-nuget-package.png 875w, https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2019\/07\/roslyn-nuget-package-300x86.png 300w, https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2019\/07\/roslyn-nuget-package-768x220.png 768w\" sizes=\"(max-width: 875px) 100vw, 875px\" \/><\/p>\n<p>Once you have the package installed you can simply customize the analyzer diagnostics from the Solution Explorer. An analyzer node will appear under the References or Dependencies node in the Solution Explorer. If you expand the analyzers, and then expand one of the analyzer assemblies, you can see all the diagnostics in the assembly.<\/p>\n<p><img decoding=\"async\" class=\"wp-image-23847 size-full aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2100\/07\/roslyn-references.png\" alt=\"Roslyn Analyzer Property References\" width=\"357\" height=\"383\" srcset=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2100\/07\/roslyn-references.png 357w, https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2100\/07\/roslyn-references-280x300.png 280w\" sizes=\"(max-width: 357px) 100vw, 357px\" \/><\/p>\n<p>You can view the properties of a diagnostic, including its description and default severity, in the properties window. To view the properties, right-click on the rule and select\u00a0<strong>Properties<\/strong>, or select the rule and then press\u00a0<strong>Alt<\/strong>+<strong>Enter<\/strong>.<\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-23848 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2100\/07\/roslyn-properties-description.png\" alt=\"Roslyn Analyzers Diagnostic Properties\" width=\"502\" height=\"292\" srcset=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2100\/07\/roslyn-properties-description.png 502w, https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2100\/07\/roslyn-properties-description-300x175.png 300w\" sizes=\"(max-width: 502px) 100vw, 502px\" \/><\/p>\n<p>The icons next to each diagnostic in\u00a0Solution Explorer\u00a0correspond to the icons you see in the rule set when you open it in the editor:<\/p>\n<ul>\n<li>the &#8220;i&#8221; in a circle indicates a severity of <strong>Info<\/strong><\/li>\n<li>the &#8220;!&#8221; in a triangle indicates a\u00a0severity\u00a0of\u00a0<strong>Warning<\/strong><\/li>\n<li>the &#8220;x&#8221; in a circle indicates a\u00a0severity\u00a0of\u00a0<strong>Error<\/strong><\/li>\n<li>the &#8220;i&#8221; in a circle on a light-colored background indicates a\u00a0severity\u00a0of\u00a0<strong>Hidden<\/strong><\/li>\n<li>the &#8220;\u2193&#8221; in a circle indicates a suppressed diagnostic<\/li>\n<\/ul>\n<p><img decoding=\"async\" class=\"size-full wp-image-23849 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2100\/07\/roslyn-severity-options.png\" alt=\"Roslyn Analyzer Severity Options\" width=\"353\" height=\"357\" srcset=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2100\/07\/roslyn-severity-options.png 353w, https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2100\/07\/roslyn-severity-options-297x300.png 297w\" sizes=\"(max-width: 353px) 100vw, 353px\" \/><\/p>\n<p>You can then set the rule set severity from the Solution Explorer. In the Solution Explorer, expand <strong>Dependencies<\/strong> &gt; <strong>Analyzers<\/strong>. Expand the assembly that contains the rule you want to set the severity for. Right-click on the rule and select <strong>Set Rule Set Severity<\/strong> and in the fly-out menu, select one of the severity options.<\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-23850 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2100\/07\/roslyn-set-severity.png\" alt=\"Roslyn Analyzer Rule Set Severity\" width=\"685\" height=\"199\" srcset=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2100\/07\/roslyn-set-severity.png 685w, https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2100\/07\/roslyn-set-severity-300x87.png 300w\" sizes=\"(max-width: 685px) 100vw, 685px\" \/><\/p>\n<p>If you set the rule severity to a warning you will then receive a warning in your code for that specific rule set.<\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-23851 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2100\/07\/roslyn-preview-change.png\" alt=\"Roslyn Analyzer Preview Change\" width=\"777\" height=\"398\" srcset=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2100\/07\/roslyn-preview-change.png 777w, https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2100\/07\/roslyn-preview-change-300x154.png 300w, https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2100\/07\/roslyn-preview-change-768x393.png 768w\" sizes=\"(max-width: 777px) 100vw, 777px\" \/><\/p>\n<p>Now that you understand how analyzers work you can be more productive and write better code faster!<\/p>\n<p><span style=\"font-size: 18pt;\"><strong>FAQ:<\/strong><\/span><\/p>\n<p><strong>Q<\/strong>: This warning appears in Visual Studio: \u201cRun Code Analysis has been deprecated in favor of FxCop analyzers, which run during build. Refer to <a href=\"https:\/\/aka.ms\/fxcopanalyzers\">https:\/\/aka.ms\/fxcopanalyzers<\/a> to migrate to FxCop analyzers\u201d. What does this mean?\n<strong>A<\/strong>: FxCop is the Code Analysis engine that predates Roslyn by almost a decade. Just like we moved our compiler forward (e.g. introduced Roslyn), we are also moving our code analysis technology forward to the Roslyn platform. Since it is powered by the .NET Compiler Platform it can produce warnings in your code as you type. In other words, you don\u2019<span class=\"pl-c1\">t<\/span> have to build your code to find out that you made a mistake.<\/p>\n<p><strong>Q<\/strong>: What&#8217;s the difference between FxCop analyzers and legacy FxCop?\n<strong>A<\/strong>: FxCop analyzers analyze source code in real time and during compilation, whereas legacy FxCop is a static code analysis and analyzes binary files after build has completed. For more information, see\u00a0<a href=\"https:\/\/docs.microsoft.com\/visualstudio\/code-quality\/roslyn-analyzers-overview?view=vs-2019#roslyn-analyzers-vs-static-code-analysis\">Roslyn analyzers vs. static code analysis<\/a>\u00a0and\u00a0<a href=\"https:\/\/docs.microsoft.com\/visualstudio\/code-quality\/fxcop-analyzers-faq?view=vs-2019\">FxCop analyzers FAQ<\/a>.<\/p>\n<p><strong>Q<\/strong>: Can I write custom analyzers?\n<strong>A<\/strong>: Absolutely! Documentation on how to write an analyzer is found <a href=\"https:\/\/docs.microsoft.com\/dotnet\/csharp\/roslyn-sdk\/tutorials\/how-to-write-csharp-analyzer-code-fix\">here.<\/a><\/p>\n<p><strong>Q<\/strong>: Should I use Roslyn analyzers or .editorconfig for code style?\n<strong>A<\/strong>: Roslyn analyzers and .editorconfig files work hand-in-hand. When you define code styles\u00a0<a href=\"https:\/\/docs.microsoft.com\/visualstudio\/ide\/editorconfig-code-style-settings-reference?view=vs-2019\">in an .editorconfig file<\/a>\u00a0or on the\u00a0<a href=\"https:\/\/docs.microsoft.com\/visualstudio\/ide\/code-styles-and-code-cleanup?view=vs-2019\">text editor options<\/a>\u00a0page, you&#8217;re actually configuring the Roslyn analyzers that are built into Visual Studio.<\/p>\n<p><strong>Q<\/strong>: Do analyzers work in continuous integration (CI) builds?\n<strong>A<\/strong>: Yes, analyzers installed as NuGet packages can be enforced in CI builds.<\/p>\n<p><strong>Q:<\/strong> Where can I request more analyzers or report bugs?\n<strong>A:\u00a0<\/strong>You can request more analyzer and report bugs on the <a href=\"https:\/\/github.com\/dotnet\/roslyn\/issues\">Roslyn repository<\/a> on GitHub.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Roslyn, the .NET compiler platform, helps you catch bugs even before you run your code. One example is Roslyn\u2019s spellcheck analyzer that is built into Visual Studio. Let\u2019s say you are creating a static method and misspelled the word static as statc. You will be able to see this spelling error before you run your [&hellip;]<\/p>\n","protected":false},"author":5818,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[685],"tags":[],"class_list":["post-23818","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet"],"acf":[],"blog_post_summary":"<p>Roslyn, the .NET compiler platform, helps you catch bugs even before you run your code. One example is Roslyn\u2019s spellcheck analyzer that is built into Visual Studio. Let\u2019s say you are creating a static method and misspelled the word static as statc. You will be able to see this spelling error before you run your [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/23818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/5818"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=23818"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/23818\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=23818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=23818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=23818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}