Checker UselessConstruction

belongs to group Basic
Identify useless constructions of objects

Frameworks supported by this checker

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

Warnings generated by this checker

  • UselessConstructionWarning: an object is created and not assigned, although its construction has only side-effects on that object [ CWE392 CWE771 ]

Options accepted by this checker

  • none

Annotations understood by this checker

  • none


Description

Java allows a few expressions to be used as commands. Among them, object creation is allowed to be used as a command since it can in principle induce side-effects and hence have a computational sense. However, creation of objects that do not induce side-effects is useless and should be removed from the code; it might actually be the sign of a programming error.

Action: Check if the object creation was meant to have some side-effect and delete it.

.NET allows a few expressions to be used as commands. Among them, object creation is allowed to be used as a command since it can in principle induce side-effects and hence have a computational sense. However, creation of objects that do not induce side-effects is useless and should be removed from the code; it might actually be the sign of a programming error.

Action: Check if the object creation was meant to have some side-effect and delete it.

Examples

Consider the following program:

public class Creations {
  public static boolean unnamed;

  public static void main(String[] args) {
    Test test = new Test(args.length > 0 ? args[0] : null);
    new Test("hello");
    new Test();
    System.out.println(test);
  }

  private static class Test {
    private final String name;

    private Test(String name) {
      this.name = name;
    }

    private Test() {
      this.name = "no name";
      unnamed = true;
    }

    @Override
    public String toString() {
      return name;
    }
  }
}

This checker issues the following warning:

Creations.java:6: [UselessConstruction: UselessConstructionWarning] Useless construction of Creations$Test

since the constructor of Test called at line 6 has no side-effects (beyond on the same object being constructed) and the constructed object is not used later. Note that no warning is issued at line 5, since the new Test object is assigned there to a local variable, nor at line 7, since the constructor used there has side-effects on field unnamed.

In this example, the programmer should remove the object creation at line 6 and check if that useless new statement was actually the sign of a programming error.

Consider the following program:

using System;

namespace DocumentationExamples
{

    public class UselessConstruction
    {
        public static bool unnamed;
        public static void Main(string[] args)
        {
            UselessConstructionTest test = new UselessConstructionTest(args.Length > 0 ? args[0] : null);
            new UselessConstructionTest("hello");
            new UselessConstructionTest();
            Console.WriteLine(test);
        }
        class UselessConstructionTest
        {
            public readonly string name;
            public UselessConstructionTest(string name)
            {
                this.name = name;
            }
            public UselessConstructionTest()
            {
                name = "no name";
                unnamed = true;
            }
            public override string ToString()
            {
                return name;
            }
        }
    } @Override
    public String toString() {
      return name;
    }
  }
}

This checker issues the following warning:

DocumentationExamples.cs:12: [UselessConstruction: UselessConstructionWarning] Useless instantiation of class "UselessConstruction$UselessConstructionTest"

since the constructor of UselessConstructionTest called at line 12 has no side-effects (beyond on the same object being constructed) and the constructed object is not used later. Note that no warning is issued at line 5, since the new UselessConstructionTest object is assigned there to a local variable, nor at line 13, since the constructor used there has side-effects on field unnamed.

In this example, the programmer should remove the object creation at line 12 and check if that useless new statement was actually the sign of a programming error.