How to check the variables that are not used?

6

There is some way to check which variables are not used within the scope > of a class?

As in the example below, the variables test1 and test4 have no utility within the application, how could it find such variables?

using System;
using System.Web.UI;
namespace bDBContext
{
    public partial class _Default : Page
    {
        string teste1 = ""; // não usada
        bool ok = true; // usada
        string teste2 = ""; // usada

        protected void Page_Load(object sender, EventArgs e)
        {
            string teste3 = teste2; // usada

            if (ok)
            {
                string teste4 = ""; // não usada
            }
        }

        public void Salvar()
        {
            string teste5 = ""; // usada
            if (string.IsNullOrEmpty(teste4))
            {

            }
        }
    }
}

Something does not have to use third-party paid tools like Resharper .

    
asked by anonymous 26.10.2016 / 20:27

4 answers

5

Visual Studio Input Versions

There is nothing built into Visual Studio (in the input versions) except warnings warnings during compilation. These warnings report unused variables (note that a private variable that is assigned inside a constructor, but is no longer used outside of it, is not reported as unused, as far as I know).

You can double-click on each warning that will be taken to the line of code where the variable, or unused method (o) has been declared.

The ideal is to use a tool, and the best is Resharper, but as it is paid, perhaps one solution is to use FxCop which is free: link

Visual Studio Premium Versions

For the Premium and Ultimate versions of Visual Studio 2012, 2013 and 2015 the Static Code Analysis functionality was derived and evolved from FxCop, so if you already have a premium version of Visual Studio, you should have access to these features.

In my Visual Studio 2012 Ultimate I access code analysis by pressing ALT + F11 or with the right mouse button as shown in the image below:

Inthecodeanalysisrunscreenyouprobablyneedtoconfigurewhichparsingrulesyouwanttouse(puttingthemallisnice,butitmightbetoomuch).TogetunusedvariablesIchosetheoptionshownintheimagebelow:

    
26.10.2016 / 20:33
5

There is a free extension that does this: CodeMaid

It is not as complete as Resharper , but adds several shortcuts that do not exist natively in Visual Studio.

Works with both express and community versions.

    
27.10.2016 / 12:19
4

Depends on the compiler configuration, but already does this by default.

If it is private field the compiler can issue warning . Especially the CS609 and CS414 warn of unused fields, without and with value, respectively.

Public fields become more complicated because it can be normal in the class that it is not used within the class. About alerting not to use internal fields already was debated about .

Local variables are alerted with CS0219 and < a href="https://msdn.microsoft.com/en-us/library/tf85t6e4.aspx"> CS0168 .

You can also use the Microsoft Code Scanning Tool, in which case it would be the warning CA1823 . But I see no need, it does not do more than the compiler already does for that case.

There are other warnings that can check for similar things.

In public fields it is much more complicated to do the verification, there are cases that do not even make sense. But it is possible. This is one of the reasons I do not waive ReSharper.

    
26.10.2016 / 21:45
3

I would recommend using Code Cracker .

It is an extension for code analysis that acts directly as a Roslyn Analyzer. In addition, it is open-source and its code is available at GitHub

Finally, it is developed by Brazilians, and the people who created the extension are very receptive and easy to talk and help, and very active in the community.

    
27.10.2016 / 14:39