Find Unused Code

Find unused code

Yes, ReSharper does this. Right click on your solution and selection "Find Code Issues". One of the results is "Unused Symbols". This will show you classes, methods, etc., that aren't used.

Find all unused code in my project (Rider/VSCode)

To find unused code in Rider:

  1. Select Code | Inspect Code in the application menu.
  2. Choose a scope to inspect (solution, project, or a custom scope).
  3. In the Inspection Results window, group inspections by issue category (and optionally by issue type).
  4. Focus on issues under Redundancies in code and Redundancies in symbol declarations: Inspeciton Results in Rider

Alternatively, as you read or edit your code in the editor, you may encounter specific unused code warnings that Rider shows you. If you want to find all issues similar to one specific issue:

  1. Press Alt+Enter to display Rider's code inspection pop-up.
  2. Press Right arrow to expand options for the current inspection.
  3. Press Right arrow to expand the Find similar issues submenu.
  4. Select a scope to find issues.Sample Image
  5. Work with the Inspection Results window as shown above.

How to find unused/dead code in java projects

I would instrument the running system to keep logs of code usage, and then start inspecting code that is not used for months or years.

For example if you are interested in unused classes, all classes could be instrumented to log when instances are created. And then a small script could compare these logs against the complete list of classes to find unused classes.

Of course, if you go at the method level you should keep performance in mind. For example, the methods could only log their first use. I dont know how this is best done in Java. We have done this in Smalltalk, which is a dynamic language and thus allows for code modification at runtime. We instrument all methods with a logging call and uninstall the logging code after a method has been logged for the first time, thus after some time no more performance penalties occur. Maybe a similar thing can be done in Java with static boolean flags...

How to use IntelliJ IDEA to find all unused code?

Just use Analyze | Inspect Code with appropriate inspection enabled (Unused declaration under Declaration redundancy group).

Using IntelliJ 11 CE you can now "Analyze | Run Inspection by Name ... | Unused declaration"

How to find unused code in Xcode 7?

Xcode has a number of settings you can enable to warn you about things like unused functions, parameters, and values.

Sample Image

You can also easily enable strict warnings by setting your Other Warning Flags to -Wall -Wextra -Weverything.

Another option for detecting unused code is by using Code Coverage.

How to find dead code in a large react project?

First of all, very good question, in large project coders usually try many lines of code test and at the end of result, hard to find the unused code.

There is two possible that must be work for you - i usually do whenever i need to remove and reduce the unused code into my project.

1st way WebStorm IDE:

If you're using the web-storm IDE for JS development or React JS / React Native or Vue js etc it's tell us and indicate us alote of mention with different color or red warning as unused code inside the editor

but it's not works in your particular scenario there is another way to remove the unused code .

2nd Way unrequired Library:

The second way to remove the unused code inside the project is unrequired library you can visit here : unrequired github

another library called depcheck under NPM & github here

Just follow their appropriate method - how to use them you will fix this unused issue easily

Hopefully that helps you

How to find all unused code in Ruby on Rails

You can look at this answer, and perhaps some of the other answers listed: https://stackoverflow.com/a/9788511/485864

I would probably end up logging the methods that you have, and run your code through the paths and anything not listed in the log, may be examined to see if it is indeed not used.

How can you find unused functions in Python code?

In Python you can find unused code by using dynamic or static code analyzers. Two examples for dynamic analyzers are coverage and figleaf. They have the drawback that you have to run all possible branches of your code in order to find unused parts, but they also have the advantage that you get very reliable results.

Alternatively, you can use static code analyzers that just look at your code, but don't actually run it. They run much faster, but due to Python's dynamic nature the results may contain false positives.
Two tools in this category are pyflakes and vulture. Pyflakes finds unused imports and unused local variables. Vulture finds all kinds of unused and unreachable code. (Full disclosure: I'm the maintainer of Vulture.)

The tools are available in the Python Package Index https://pypi.org/.

Get List of Zero Reference Codes in Visual Studio

Probably the best and easiest way to achieve what you are after is to use the build-in code analysis tool with Visual Studio to find and take you directly to dead code and unused members.

To this effect, I created a new code analysis ruleset file (Via File->New->File, making sure General in the left pane was selected and scrolling down to find Code Analysis Rule Set, giving it a filename, then searching for and selecting the below rules). See below for the contents of the ruleset file that you can copy, and paste into a new file with the extension .ruleset to use.

Given a ruleset file, one can right click on a project file in the Solution Explorer panel, and select Properties. In the project properties windows, click on the Code Analysis tab in the left panel, and then click Open to browse to the .ruleset file's location. If you go to the properties of a solution file (as opposed to a project file), you can set the code analysis file for each project in the solution in one place (under Code Analysis Settings, and using the drop-down there to select the ruleset file. NOTE: You must have previously have browsed to the ruleset file for it to show up in the drop-down in this properties window, however).

Then you simply run the code analysis on the projects/solution (Via Analyze->Run Code Analysis On Solution -OR- Alt+F11) and it will come back as warnings, any unreferenced methods or unused members it finds. It will even find methods that are referenced by a method, whom itself has no references elsewhere.

Be careful however, as one of the ways code analysis for dead code can steer you wrong, is if the reference is 'hidden' by only ever calling the method via delegates, and of course, reflection.

The rules to detect dead code, specifically, are:

  • Private methods that are not called from any other code (CA1811)
  • Unused local variables (CA1804)
  • Unused private fields (CA1823)
  • Unused parameters (CA1801)
  • Internal classes that are not instantiated from any other code (CA1812).
  • Dead code in bitwise-OR limited switch (C6259)

Below is the contents of the .ruleset file that can be had by following the steps above, for your conveinence. You can simply copy the below XML, paste it into notepad++, save somewhere with the extension .ruleset, browse for and use as explained above:

<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Dead Code Rules" Description=" " ToolsVersion="12.0">
<Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
<Rule Id="CA1801" Action="Warning" />
<Rule Id="CA1804" Action="Warning" />
<Rule Id="CA1811" Action="Warning" />
<Rule Id="CA1812" Action="Warning" />
<Rule Id="CA1823" Action="Warning" />
</Rules>
<Rules AnalyzerId="Microsoft.Analyzers.NativeCodeAnalysis" RuleNamespace="Microsoft.Rules.Native">
<Rule Id="C6259" Action="Warning" />
</Rules>
</RuleSet>


Related Topics



Leave a reply



Submit