Get List of Zero Reference Codes in Visual Studio

Get List of Zero Reference Codes in Visual Studio

Probably the best and easiest way to achieve what you are after is to use the build-in code analysis tool with Visual Studio to find and take you directly to dead code and unused members.

To this effect, I created a new code analysis ruleset file (Via File->New->File, making sure General in the left pane was selected and scrolling down to find Code Analysis Rule Set, giving it a filename, then searching for and selecting the below rules). See below for the contents of the ruleset file that you can copy, and paste into a new file with the extension .ruleset to use.

Given a ruleset file, one can right click on a project file in the Solution Explorer panel, and select Properties. In the project properties windows, click on the Code Analysis tab in the left panel, and then click Open to browse to the .ruleset file's location. If you go to the properties of a solution file (as opposed to a project file), you can set the code analysis file for each project in the solution in one place (under Code Analysis Settings, and using the drop-down there to select the ruleset file. NOTE: You must have previously have browsed to the ruleset file for it to show up in the drop-down in this properties window, however).

Then you simply run the code analysis on the projects/solution (Via Analyze->Run Code Analysis On Solution -OR- Alt+F11) and it will come back as warnings, any unreferenced methods or unused members it finds. It will even find methods that are referenced by a method, whom itself has no references elsewhere.

Be careful however, as one of the ways code analysis for dead code can steer you wrong, is if the reference is 'hidden' by only ever calling the method via delegates, and of course, reflection.

The rules to detect dead code, specifically, are:

  • Private methods that are not called from any other code (CA1811)
  • Unused local variables (CA1804)
  • Unused private fields (CA1823)
  • Unused parameters (CA1801)
  • Internal classes that are not instantiated from any other code (CA1812).
  • Dead code in bitwise-OR limited switch (C6259)

Below is the contents of the .ruleset file that can be had by following the steps above, for your conveinence. You can simply copy the below XML, paste it into notepad++, save somewhere with the extension .ruleset, browse for and use as explained above:

<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Dead Code Rules" Description=" " ToolsVersion="12.0">
<Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
<Rule Id="CA1801" Action="Warning" />
<Rule Id="CA1804" Action="Warning" />
<Rule Id="CA1811" Action="Warning" />
<Rule Id="CA1812" Action="Warning" />
<Rule Id="CA1823" Action="Warning" />
</Rules>
<Rules AnalyzerId="Microsoft.Analyzers.NativeCodeAnalysis" RuleNamespace="Microsoft.Rules.Native">
<Rule Id="C6259" Action="Warning" />
</Rules>
</RuleSet>

Find Dead code

The rule CA1811: Avoid uncalled private code perfectly works fine to detect the uncalled private methods.

To enable the code analysis warnings, you also need to set this value in project properties like this -

Sample Image

As a suggestion it is good idea to have our custom ruleset, to have more control over rule violations for example you can define any violation as error, so that you can get it as compile error and fix it.

List unreferenced files in Visual Studio 2019?

You can use the RuleSet from this answer to make your CodeAnalysis show you unused code:

<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Dead Code Rules" Description=" " ToolsVersion="12.0">
<Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
<Rule Id="CA1801" Action="Warning" />
<Rule Id="CA1804" Action="Warning" />
<Rule Id="CA1811" Action="Warning" />
<Rule Id="CA1812" Action="Warning" />
<Rule Id="CA1823" Action="Warning" />
</Rules>
<Rules AnalyzerId="Microsoft.Analyzers.NativeCodeAnalysis" RuleNamespace="Microsoft.Rules.Native">
<Rule Id="C6259" Action="Warning" />
</Rules>
</RuleSet>

Edit: You can use this step by step guide to build your RuleSet.

Edit 2: Since the step by step guide is outdated, here is a new way of activating the needed rules:

  1. Go to the properties page of your project (right click -> Properties)
  2. Go to "Code Analysis", the last tab
  3. Click "Configure"
  4. Enter "unused" and activate all the rules that you want
  5. On the right, optionally set the Action to "Error" for extra attention
  6. Save (ctrl+s)

Edit 3: We will keep the ruleset but it turns out I had an old project and on new ones, you need to do the following:

  1. If your target framework is below .NET5, install Microsoft.CodeAnalysis.FxCopAnalyzers Microsoft.CodeAnalysis.NetAnalyzers from NuGet (FxCop was deprecated)

  2. If you haven't already, add a file .editorconfig to you solution

  3. Enable the rules you want like this:

    [*.cs]

    dotnet_diagnostic.CA1822.severity = error

in the editorconfig file to activate them.

Visual Studio Code Analyzer: Finding types with zero references

Roslyn diagnostic analyzers don't currently allow you to do solution-wide (i.e. cross-project) analysis, which is why we don't give you a Solution object. This was partially for performance considerations: if you tried to call FindReferencesAsync everywhere your CPU would be pegged pretty heavily. For CodeLens there was large amount of feedback for how much CPU we were using, we didn't want 10 diagnostics all consuming that same amount of CPU. (Imagine your poor laptop battery...)

If you're OK with this being more limited, say finding internal types that are unused in the project they're within, take a look at this analyzer we wrote awhile back.

How to identify unused classes in C# project

There are no tools which can do this completely, because

  • System.Reflection and System.CodeDom exist - Is it possible to dynamically compile and execute C# code fragments?
  • New C# code can be generated at run-time, which uses otherwise-unused classes.
  • No tools can predict what that new C# code is (apart from the humans who wrote the code)
  • Dependency Injection libraries (which use System.Reflection behind the scenes) can call "unused" classes. This happens frequently with MVC Controller classes.
  • Razor Views can use classes. These are not compiled by default. Instead, they will crash at runtime if a class is missing.

Assuming no-one is using System.Reflection, you could do it by hand.

For each class:

  • Select it in Visual Studio, right-click then "Find All References"
  • If none found, comment the class out /* */
  • Rebuild all (including Razor views). If no errors found, then the class is unused.


Related Topics



Leave a reply



Submit