Java


JAVA.STRUCT.UUOBJ : Unused Object (Java)

要旨

Identify useless constructions of objects.

An object is created and not assigned, and its construction has no side-effects other than on the object itself.

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.

プロパティ

クラス名 Unused Object (Java)
日本語クラス名 Unused Object (Java)
クラス分類 信頼性 (reliability)
ニーモニック JAVA.STRUCT.UUOBJ
カテゴリー
CWE CWE:1164 Irrelevant Code
対応言語 Java で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Unused Object (Java)"

public class Creations {
  public static boolean unnamed;

  public static void main(String[] args) {
      Test test = new Test(args.length > 0 ? args[0] : null);   // ok: object assigned to a local variable
      new Test("hello");   // "Unused Object (Java)" warning issued here
      new Test();                                               // ok: constructor has side effect on field 'unnamed'
      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;
    }
  }
}

解決法

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

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。