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
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 :
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?
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
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
@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
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
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
@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?
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.
@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".
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
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
@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
Participant
1061 Points
665 Posts
Locate Unused Code - VS2015/2017
Jan 11, 2019 09:54 PM|jzero|LINK
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
All-Star
48490 Points
18068 Posts
Re: Locate Unused Code - VS2015/2017
Jan 12, 2019 05:45 PM|PatriceSc|LINK
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
Participant
1061 Points
665 Posts
Re: Locate Unused Code - VS2015/2017
Jan 12, 2019 07:41 PM|jzero|LINK
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?
All-Star
48490 Points
18068 Posts
Re: Locate Unused Code - VS2015/2017
Jan 12, 2019 08:09 PM|PatriceSc|LINK
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).
Participant
1061 Points
665 Posts
Re: Locate Unused Code - VS2015/2017
Jan 13, 2019 01:04 AM|jzero|LINK
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?
Contributor
3500 Points
1300 Posts
Re: Locate Unused Code - VS2015/2017
Jan 14, 2019 04:38 AM|Ackerly Xu|LINK
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
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Participant
1061 Points
665 Posts
Re: Locate Unused Code - VS2015/2017
Jan 14, 2019 01:33 PM|jzero|LINK
@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
Contributor
3500 Points
1300 Posts
Re: Locate Unused Code - VS2015/2017
Jan 15, 2019 01:15 AM|Ackerly Xu|LINK
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
Maybe its the version of visual studio ?Have you seen the CA1811 warning?
Best regards,
Ackerly Xu
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Participant
1061 Points
665 Posts
Re: Locate Unused Code - VS2015/2017
Jan 15, 2019 11:29 AM|jzero|LINK
@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?
All-Star
48490 Points
18068 Posts
Re: Locate Unused Code - VS2015/2017
Jan 15, 2019 11:39 AM|PatriceSc|LINK
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.
Participant
1061 Points
665 Posts
Re: Locate Unused Code - VS2015/2017
Jan 15, 2019 02:26 PM|jzero|LINK
@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)
Contributor
3500 Points
1300 Posts
Re: Locate Unused Code - VS2015/2017
Jan 29, 2019 07:15 AM|Ackerly Xu|LINK
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
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
All-Star
48490 Points
18068 Posts
Re: Locate Unused Code - VS2015/2017
Jan 29, 2019 09:51 AM|PatriceSc|LINK
You are using 2015 which perhaps doesn't include that. You could perhaps install 2017 to give this a try.
Participant
1061 Points
665 Posts
Re: Locate Unused Code - VS2015/2017
Jan 29, 2019 07:46 PM|jzero|LINK
@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