Visual Studio Find Unused Code



Visual Studio Find Unused Code

One benefit of using ReSharper is that it shows you bits of your own code that you’re not using. ReSharper is actually very smart about it: not only will it point out code that isn’t used anywhere, but it will detect more complicated cases – for example the case where a field is both declared and initialized, but nothing apart from initialization actually happens to it.

Visual Studio Find Unused Code

Let’s take a look at an example. Say we have a class Person defined as follows:

public class Person{ private string _name; public Person(string name) { this._name = name; }}

The complication comes from trying to detect variables that are 'unused' within code, but is used in other domains (e.g. Visualforce, Lightning). For Lightning (Aura and LWC), variables whose names are dimmed are not used. For example: Here, you can see the event parameter is unused. Unused variable detection - Unused variables are grayed-out in your JavaScript/TypeScript files. Move to new file refactoring - Easily pull classes and functions into their own files. Update imports on move/rename - Automatically update import paths in JavaScript/TypeScript.

Now, ReSharper will indicate that the _name field isn’t used. If you hover the mouse pointer over this field, you’ll get the following message:

C++

If you now press Alt+Enter over the _name field, ReSharper will offer an option to remove the name field and any assignment to it:

And here is the result:

  1. Visual Studio 2012/2013/2015 Plug-in Find and eliminate unused images & files in web projects/applications This extension helps you parse a website solution or project to find and eliminate unused images or other orphaned file types that are not being referenced in your sourcecode anymore.
  2. Visual studio code has released a new feature last year in April in 2018 which allow users to change their settings to remove unused imports automatically on save.

Notice that now ReSharper informs us that the parameter can also be removed. We use the corresponding quick-fix:

And we get an empty constructor which, as you may have guessed, is also not needed, since a default constructor is implemented by default. Once again, ReSharper picks up on it and offers us an option to get rid of the constructor:

Visual Studio Find Unused Code C++

JetBrains Rider lets you find unused JavaScript, TypeScript, and CSS code in your client-side applications. When you run an application in the special Code Coverage mode, JetBrains Rider creates a report showing how much code in every file and folder was used. Thanks to source maps, this report shows coverage for your source files but not for the compiled code that was actually run in the browser.

Unreferenced

Run an application in the Code Coverage mode

  1. Create a run/debug configuration of the type JavaScript Debug: Go64.

    Choose Run | Edit Configuration from the main menu, click on the toolbar and select JavaScript Debug from the list. In the Run/Debug Configuration: JavaScript Debug dialog that opens, specify the URL address at which the application is running. This URL can be copied from the address bar of your browser.

    Because the JavaScript Debug configuration works only with Chrome, the Code Coverage mode is also available only in this browser.

  2. Choose the newly created configuration in the Select run/debug configuration list on the toolbar and click next to the list.

    The URL address specified in the run configuration opens in the browser.

  3. To learn what code was executed during the page load, just load the application and then stop it by clicking on the toolbar, next to , or in the Run tool window. If you need a coverage report for some specific features of your application, trigger these features in the browser and only then click to stop the application.

  4. View the report in the Coverage tool window. The Project tool window shows information about the coverage of files and folders. In the editor, the gutter shows green markers next to the lines that were executed and red markers next to those that were not. You can also hover over the line markers and see how many times each line of code was hit.

C# Mark Parameter As Unused

Last modified: 08 March 2021