C#


CSHARP.STRUCT.UUOBJ : Unused Object (C#)

要旨

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

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

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); // ok: assigned to local variable
            new UselessConstructionTest("hello");                  // "Unused Object (C#)" warning issued here 
            new UselessConstructionTest();                                                                // ok: has side effect on static field UselessConstruction.unnamed
            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;
    }
  }
}

解決法

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

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

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