Bug 564397 - provide a dead/unused code detector for non-private code
Summary: provide a dead/unused code detector for non-private code
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.16   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 563661 (view as bug list)
Depends on:
Blocks:
 
Reported: 2020-06-18 02:59 EDT by Rolf Theunissen CLA
Modified: 2022-01-18 07:11 EST (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Rolf Theunissen CLA 2020-06-18 02:59:29 EDT
JDT provides unused code detection for private methods and fields. However, AFAIK there no build in detection for unused code that is protected or private.
Having detection for dead/unused code would be really beneficial, for instance, Eclipse Platform UI has dozens of unused internal classes (e.g. Bug 564144).

UCDetector [1] is an Eclipse plugin that provides such kind of analysis already, though its maintenance seems low. It heavily depends on functionality provided by Eclipse.
Though, some integration could be better, such as able to distinguish between exported and not-exported packages.

[1] http://www.ucdetector.org/
Comment 1 Manoj N Palat CLA 2020-06-18 23:53:03 EDT
We have a dead code detector built into our flow analysis, check the code below:
public class X  {

	protected void foo(int i) {
		if (true) {
			return ;
		}
		if (i > 0) { // Dead code [warning]
			return ;
		}
	}
}

Could you give a specific code snippet example for your requirement?
Comment 2 Rolf Theunissen CLA 2020-06-19 02:43:21 EDT
I know the detection for dead code within a class. But there is no detection of, for example, *public* classes, methods or fields which have no references. Surely it is valid to have such classes/methods/fields for API that is provided. For non-API this is a smell that the code is not used (disclaimers aside for reflection, special cases of text references).

Read the description of the UCDetector project. Also IntelliJ provides similar functionality [1].

Surely some configuration is required here, that can filter out common false positives, and result in confusion if enabled always [2]. But, for example, for most OSGi bundles it should be quite well defined what is API and what not.

[1] https://stackoverflow.com/questions/6587729/how-to-use-intellij-idea-to-find-all-unused-code
[2] https://stackoverflow.com/questions/6166334/disable-not-used-warning-for-public-methods-of-a-class
Comment 3 Rolf Theunissen CLA 2020-06-19 13:25:45 EDT
I just learned that the Code Minings already provide functionality to count the number of references to types/fields/methods, among others.
Everything without a reference is potentially unused code.
Comment 4 Manoj N Palat CLA 2020-06-19 19:12:25 EDT
This comes under the purview of jdt/ui - moving to jdt/ui
Comment 5 Lars Vogel CLA 2020-06-24 08:18:30 EDT
*** Bug 563661 has been marked as a duplicate of this bug. ***
Comment 6 Lars Vogel CLA 2020-06-24 08:19:24 EDT
org.eclipse.core.tools contains code for finding unused code. Also we could contact the developers of UCDetector if they are interested in contributing this to JDT.
Comment 7 Lars Vogel CLA 2022-01-12 03:00:15 EST
The eclipse.platform.runtime repo has the org.eclipse.core.tools project which contains a implementation to find unused code.