views:

1391

answers:

7

I want to delete foo() if foo() isn't called from anywhere.

+2  A: 

Resharper does this, and not just with methods. It also does it with using statements, variables etcetera.

HTH, Kent

Kent Boogaart
+14  A: 

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)

sixlettervariables
(+1) FxCop is the way to go
Keith
This (CA1811) will not work for all kinds of methods, e.g. static public methods.
JRoppert
Also it is a great tool in general
RedDeckWins
@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.
sixlettervariables
I just tried FxCop. It doesn't detect unused public methods, which is what I was mainly interested in.
Corey Trager
@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.
sixlettervariables
Executable, not library.
Corey Trager
I would like to note that this works for VB.NET as well (and I believe any .NET library or executable)
Nathan Koop
+2  A: 

Yes, the MZ-Tools addin has a review dead code feature.

Scott Dorman
+2  A: 

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.

RedDeckWins
Too tedious to do it one-by-one. I was looking for a tool that would sweep through all the code.
Corey Trager
+2  A: 

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.

Helen Toomik
+5  A: 

NDepend will also report on potentially unused code.

Ian Nelson
+1  A: 

NDepend can report dead code thanks to CQL queries or rules such as:

// <Name>Potentially unused methods</Name>
WARN IF Count > 0 IN SELECT TOP 10 METHODS WHERE 
 MethodCa == 0 AND        // Ca=0 -> No Afferent Coupling -> 
                          // The method is not used in the 
                          // context of this application.

 !IsPublic AND            // Public methods might be used by 
                          // client applications of your assemblies.

 !IsEntryPoint AND        // Main() method is not used by-design.

 !IsExplicitInterfaceImpl // The IL code never explicitely 
 AND                      // calls explicit interface methods 
                          // implementation.

 !IsClassConstructor AND  // The IL code never explicitely 
                          // calls class constructors.

 !IsFinalizer             // The IL code never explicitely 
                          // calls finalizers.

or

// <Name>Potentially unused types</Name>
WARN IF Count > 0 IN SELECT TOP 10 TYPES WHERE 
 TypeCa == 0 AND    // Ca=0 -> No Afferent Coupling -> 
                    // The type is not used in the 
                    // context of this application.

 !IsPublic AND      // Public types might be used 
                    // by client applications of your
                    // assemblies.

 !NameIs "Program"  // Generally, types named Program 
                    // contain a Main() entry-point 
                    // method and this condition avoid 
                    // to consider such type as 
                    // unused code.
Patrick Smacchia - NDepend dev