locked
Locate Unused Code - VS2015/2017 RRS feed

  • Question

  • User-943250815 posted

    Is there a way to locate unused code (classes, functions, subs) on Visual Studio Community 2015/2017, I know I can check where it is used, but I´m looking for the opposite.
    I know this can be achieved with Reshaper, NDepend and some other external tools, but I´m lookig for a native solution or free tool, not by trial time

    Thanks

    Friday, January 11, 2019 9:54 PM

Answers

All replies

  • User753101303 posted

    Hi,

    Community have a "Code Analysis" feature that may detect some situation. You have "CodeLens" in Pro or Enterprise that shows a reference count. A tool would IMO show lmany false positive (for example in MVC technically speaking your actions are not called from within your code).

    Done a test. Try "Analyze", "Code Analysis", enable all rules and do a check. I see it generate for example :

    https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1811-avoid-uncalled-private-code?view=vs-2015

    Saturday, January 12, 2019 5:45 PM
  • User-943250815 posted

    In Community "Code Analysis" if "Run Code Analysis on WebSite or Solution" it wont detect a function that I know is not used anywhere, not  a single message about that.
    So, my point is, is such resource available only for PRO & Enterprise or am I expecting a result that is not available on Community?

    Saturday, January 12, 2019 7:41 PM
  • User753101303 posted

    I tried with Community. It's not part of the default rules. You have to change selected rules (for now I tried "All" for a quick test).

    Saturday, January 12, 2019 8:09 PM
  • User-943250815 posted

    Thanks, know I can get some more results, but not what I was expecting.
    Just as an example, I created a new Web Site, with a single Page with a Button.
    On code behind, button_click and 2 functions, one is in use the other not.
    After Run Analysis, I was expecting a warning about unused function, but not a single mention about it.
    What am I missing?

    Partial Class _Default
      Inherits System.Web.UI.Page
    
      Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim test As Boolean = CheckVal(1)
      End Sub
    
      Private Function CheckVal(ByVal n As Integer) As Boolean
        Return If(n = 1, True, False)
      End Function
    
      Private Function CheckStr(ByVal s As String) As Boolean
        Return If(s = "a", True, False)
      End Function
    End Class

    Sunday, January 13, 2019 1:04 AM
  • User-893317190 posted

    Hi jzero,

    Have you set the rules to all?

    If not , please right click your project , choose properties, choose Code Analysis on the left  and change Run this rule set: to Microsoft All Rules.

    Then Run Code Analysis  on your project .

    Also , please change your Protected method to private method , or it won't show warning.

    You could refer to the link for more details.

    https://stackoverflow.com/questions/30974433/get-list-of-zero-reference-codes-in-visual-studio

    Best regards,

    Ackerly Xu

    Monday, January 14, 2019 4:38 AM
  • User-943250815 posted

    @Ackerly Xu,
    As pointed by @PatriceSC, and now by you, I used "All Rules", and not a single mention of Unused.
    So I don´t know if in a sample like I posted it should indicate there is an Unused function or not.

    Just in case change from Protected to Private does not make any result change

    Monday, January 14, 2019 1:33 PM
  • User-893317190 posted

    Hi jzero,

    I have made a test  using my visual studio enterprise, but I could get the warning.

    Below is the result. It shows  the method has no upstream Public or Protected callers.

    My method 

      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs)
    
        End Sub
    
        Private Function CheckVal(ByVal n As Integer) As Boolean
            Return If(n = 1, True, False)
        End Function

    Maybe its the version of visual studio ?Have you seen the CA1811 warning?

    Best regards,

    Ackerly Xu

    Tuesday, January 15, 2019 1:15 AM
  • User-943250815 posted

    @Ackerly Xu
    Interesting, I confirmed all rules are there, but seems it is not all rules are "operational" on Community version.
    Do you believe there can be a workaround?

    Tuesday, January 15, 2019 11:29 AM
  • User753101303 posted

    I really tried in VS 2017 Community and got the CA1811 warning. Could you clarify which version you are using as you are talking about VS2015/VS2017. What if you try with Community 2017 first ? Also if you are seeing no warning at all maybe you could check in the setup that "Code Analysis" was installed ? Will try again at home with your code sample.

    Tuesday, January 15, 2019 11:39 AM
  • User-943250815 posted

    @PatriceSC
    I´m using VS 2015 Community, checked install, but there is no "Code Analysis" to select.
    I can see warnings but none refer to CA 1801, 1803, 180 or 1811.
    For sample I posted, this is what i get when on Analyze > Run Code Analysis > Run Code Analysis on Web Site (with Microsoft All Rules in place)

    Code Description
    Warning CA2000 In method 'default_aspx.__BuildControl__control3()', object '__ctrl' is not disposed along all exception paths. Call System.IDisposable.Dispose on object '__ctrl' before all references to it are out of scope.
    Warning CA2000 : Microsoft.Reliability : In method 'default_aspx.__BuildControl__control3()', object '__ctrl' is not disposed along all exception paths. Call System.IDisposable.Dispose on object '__ctrl' before all references to it are out of scope.
    Warning CA2000 In method 'default_aspx.__BuildControl__control4()', object '__ctrl' is not disposed along all exception paths. Call System.IDisposable.Dispose on object '__ctrl' before all references to it are out of scope.
    Warning CA2000 : Microsoft.Reliability : In method 'default_aspx.__BuildControl__control4()', object '__ctrl' is not disposed along all exception paths. Call System.IDisposable.Dispose on object '__ctrl' before all references to it are out of scope.
    Warning CA1303 Method 'default_aspx.__BuildControlButton1()' passes a literal string as parameter 'value' of a call to 'Button.Text.Set(String)'. Retrieve the following string(s) from a resource table instead: "Button".
    Warning CA1303 : Microsoft.Globalization : Method 'default_aspx.__BuildControlButton1()' passes a literal string as parameter 'value' of a call to 'Button.Text.Set(String)'. Retrieve the following string(s) from a resource table instead: "Button".

    Tuesday, January 15, 2019 2:26 PM
  • User-893317190 posted

    Hi jzero,

    Maybe you could try the guide below,its a step by step guide, if you still have the problem, it maybe has something to do with your version of visual studio

    http://dotnetcodepress.com/Articles/Visual-Studio/Eliminate-unused-or-Dead-code-using-Visual-Studio-Code-Analysis

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 29, 2019 7:15 AM
  • User753101303 posted

    You are using 2015 which perhaps doesn't include that. You could perhaps install 2017 to give this a try.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 29, 2019 9:51 AM
  • User-943250815 posted

    @Patrice
    Yes, sounds time to move up and start working with 2017 Community.

    @Ackerly Xu
    Thanks for all suggestions, I´m sure they should work, but I use Community Version.
    Looks 2017 Community is a solution in my case. PatriceSc confirmed it works on this version

    Tuesday, January 29, 2019 7:46 PM