I want to delete foo() if foo() isn't called from anywhere.
-
PC-Lint and splint will find unreferenced methods and variables. – user7116 Sep 15 '08 at 18:51
7 Answers
Gendarme will detect private methods with no upstream callers. It is available cross platform, and the latest version handles "AvoidUncalledPrivateCodeRule".
FxCop will detect public/protected methods with no upstream callers. However, FxCop does not detect all methods without upstream callers, as it is meant to check in the case that your code is part of a Library, so public members are left out. You can use NDepend to do a search for public members with no upstream callers, which I detail here in this other StackOverflow answer.
(edit: added information about Gendarme which actually does what the questioner asked)
-
1This (CA1811) will not work for all kinds of methods, e.g. static public methods. – JRoppert Sep 15 '08 at 18:56
-
-
@JRoppert: I think this is because it is unable to tell in the number of passes made by FxCop if it is truly unreferenced. Resharper crashes all the time for me, so using it is out of the question. Good point though. – user7116 Sep 15 '08 at 21:33
-
2I just tried FxCop. It doesn't detect unused public methods, which is what I was mainly interested in. – Corey Trager Sep 16 '08 at 11:52
-
@Corey Trager: is this a class library or executable? It is expected that public methods are unused in class libraries. As for executables, if that is the case then there exists no free solution besides @Dustin's answer. – user7116 Sep 16 '08 at 12:41
-
-
I would like to note that this works for VB.NET as well (and I believe any .NET library or executable) – Nathan Koop Mar 20 '09 at 19:51
NDepend will also report on potentially unused code.

- 57,123
- 20
- 76
- 103
-
I like NDepend, but it's hard to justify the cost of it, although it works nicely with TeamCity and most other build agents. – Dan Atkinson Jun 14 '11 at 12:43
Bear in mind that Resharper (and probably other similar tools as well) will not highlight unused methods if the methods are marked public
. There is no way a static code analysis tool will be able to check whether the methods of your assembly are used by other assemblies outside your solution. So the first step in weeding out unused methods is to reduce their visibility to private
or internal
.

- 2,096
- 1
- 18
- 17
-
2ReSharper's solution-wide analysis will detect unused non-private type or member declarations. – TrueWill Mar 19 '12 at 01:06
The tool NDepend can help find unused code in a .NET code base. Disclaimer: I am one of the developer of this tool.
NDepend proposes to write Code Rule over LINQ Query (CQLinq). Around 200 default code rules are proposed, 3 of them being dedicated to unused/dead code detection:
- Potentially dead Types (hence detect unused class, struct, interface, delegate...)
- Potentially dead Methods
- Potentially dead Fields
NDepend is integrated in Visual Studio, thus these rules can be checked/browsed/edited right inside the IDE. The tool can also be integrated into your CI process and it can build reports that will show rules violated and culprit code elements.
If you click these 3 links toward the source code of these rules, you'll see that the ones concerning types and methods are a bit complex. This is because they detect not only unused types and methods, but also types and methods used only by unused dead types and methods (recursive).
This is static analysis, hence the prefix Potentially in the rule names. If a code element is used only through reflection, these rules might consider it as unused which is not the case.
In addition to using these 3 rules, I'd advise measuring code coverage by tests and striving for having full coverage. Often, you'll see that code that cannot be covered by tests, is actually unused/dead code that can be safely discarded. This is especially useful in complex algorithms where it is not clear if a branch of code is reachable or not.

- 13,237
- 6
- 61
- 92
Resharper does this, and not just with methods. It also does it with using statements, variables etcetera.

- 175,602
- 35
- 392
- 393
Well, if VS doesn't do this natively, a simple method is to right click on the method and select "find all references" . If there is only 1 reference (where it is declared) it most likely isn't used anywhere else.

- 2,111
- 14
- 16
-
3Too tedious to do it one-by-one. I was looking for a tool that would sweep through all the code. – Corey Trager Sep 16 '08 at 11:58