Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the Find All References in Visual Studio good enough to judge if a piece of code is unused?

I'm refactoring a big C# project and it's only natural to find my snowman's unused dead code.

There's this function called FooBar() which looks useful since it has a bunch of code inside it. As is obvious, I was trying to find the references to this function using the "Find All References (F9)" option and it showed up empty-handed (even with the scope to Entire Solution).

Here is the function:

Class A has:

~
public virtual void FooBar(){

<function is empty>

}
~

I have a Class B with:

public override void FooBar(){

~<doing something important here>~

}

I've ran the "Find All References" on both these functions and nothing showed up. I'm assuming its safe to remove but worried if it is, indeed, used somewhere. Apart from this, I've used the "Find in Files" (with Solution-wide scope) for the method name and it showed up empty too.

My question is: Does "Find All References" obtain all the usages? Can it be relied upon to find and remove dead code if nothing shows up in its results?

EDIT:

This function is not part of an API and would not be called by any external code.

like image 695
cyfrost Avatar asked Sep 03 '18 12:09

cyfrost


People also ask

Where is the unused code in Visual Studio?

To find unused members with a Code Analysis Ruleset, from the Visual Studio menu select File -> New -> File… -> General -> Code Analysis Rule Set. Uncheck all the rules. There are many rules we don't care about right now – and some we probably won't ever care about.

How can you find the unused Nuget packages in a solution?

Instead of opening the Nuget Packagemanager and uninstalling things manually there is faster way: right click on the project in the Solution Explorer and go into R# Refactor menu. There you'll find the Remove unused references menu item.

How do I show reference codes in Visual Studio?

Step 1: From Settings – Type CodeLens and Enable the Editor: Code Lens and JavaScript > References Code Lens to check the references in JavaScript File.


1 Answers

No, there are various reason this will not find code that relies on it.

  1. It is a public API that is consumed by other projects.
  2. The code is called by reflection.
  3. The code is in files that are not analysed, for example Razor files are often skipped.

I'm sure there are more reasons beside these.

like image 73
DavidG Avatar answered Nov 14 '22 22:11

DavidG