Checker UnusedClass

belongs to group Basic
Identify unused classes

Frameworks supported by this checker

  • java up to 11
  • android up to API level 28
  • dotnet

Warnings generated by this checker

  • UnusedClassWarning: a class is not used [ CWE398 ]

Options accepted by this checker

  • none

Annotations understood by this checker

  • none


Description

Classes that are defined in a project but never used are possibly legacy code, that could be removed or left in the version control system only. This would clean the project and avoid spurious code. Moreover, in some cases an unused class might be the sign of a bug, if the programmer expected it to be used.

Action: Remove the class from the program and simplify the code. Verify why the class is not used, when you expect it to be.

Classes that are defined in a project but never used are possibly legacy code, that could be removed or left in the version control system only. This would clean the project and avoid spurious code. Moreover, in some cases an unused class might be the sign of a bug, if the programmer expected it to be used.

Action: Remove the class from the program and simplify the code. Verify why the class is not used, when you expect it to be.

Examples

Consider the following program:

public class Runner {

  interface Task {
    void go();
  }

  static abstract class SubTask implements Task {
    private static int goCounter;

    public final void go() {
      goCounter++;
      goImpl();
    }

    protected abstract void goImpl();

    public static int howManyRuns() {
      return goCounter;
    }
  }
  
  public static void main(String[] args) {
    new Task() {

      @Override
      public void go() {
        System.out.println("I'm running");
      }
    };
  }
}

This checker issues the following warning:

Runner.java: [UnusedClass: UnusedClassWarning] Class Runner$SubTask is not used

since the inner abstract class Runner$Subtask is defined but never used in the code and could hence be removed. Note that the inner interface Runner$Task is used instead, since an anonymous subclass is defined and created (used) in method main().

Consider the following program:

using System;

namespace DocumentationExamples
{
    public class UnusedClass
    {
        interface ITask
        {
            void Go();
        }
        abstract class SubTask : ITask
        {
            private static int goCounter;
            void ITask.Go()
            {
                goCounter++;
                GoImpl();
            }
            protected abstract void GoImpl();
            protected static int HowManyRuns()
            {
                return goCounter;
            }
        }
        class Tmp : ITask
        {
            void ITask.Go()
            {
                Console.WriteLine("I'm running");
            }
        };
        public static void Main(string[] args)
        {
            new Tmp();
        }
    }
}

This checker issues the following warning:

DocumentationExamples.cs: [UnusedClass: UnusedClassWarning] Class "UnusedClass$SubTask" is not used

since the inner abstract class UnusedClass$Subtask is defined but never used in the code and could hence be removed. Note that the inner interface UnusedClass$Task is used instead, since an anonymous subclass is defined and created (used) in method Main().