C#


CSHARP.FUNCS.IRV.PURE : Ignored Return Value for Pure Function (C#)

要旨

A call to a pure method is performed and the returned value is missing or discarded.

The return value is the result of the computation of a method. In some cases, it is expected that this return value gets used rather than dropped, since it contains important information about the outcome of the method or since otherwise the call to the method or constructor would be useless and hence meaningless. The latter situation occurs for calls to pure methods or constructors, that is, code that does not modify the heap memory of the caller. In many cases, this latter situation is the sign of more serious problems in the algorithmic logic of the code.

プロパティ

クラス名 Ignored Return Value for Pure Function (C#)
日本語クラス名 Ignored Return Value for Pure Function (C#)
クラス分類 信頼性 (reliability)
ニーモニック CSHARP.FUNCS.IRV.PURE
カテゴリー
CWE CWE:252 Unchecked Return Value
対応言語 C# で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Ignored Return Value for Pure Function (C#)"

using System.IO;

namespace DocumentationExamples
{
    public class UnusedReturnValue
    {
        private string name;
        private static int counter;
        public UnusedReturnValue(string name)
        {
            this.name = name;
        }
        public UnusedReturnValue(string name, int offset) : this(name)
        {
            counter += offset;
        }
        public static void Main(string[] args)
        {
            Directory.CreateDirectory("dir");   // Ignored Return Value (C#) warning issued here
            UnusedReturnValue t = new UnusedReturnValue("John");
            t.GetName();                        // Ignored Return Value for Pure Function (C#) warning issued here
            new UnusedReturnValue("Joan");      // Ignored Return Value for Pure Function (C#) warning issued here
            new UnusedReturnValue("Albert", 13);                       // ok: has side effect on static field UnusedReturnValue.counter
        }
        public string GetName()
        {
            return name;
        }
    }
}

In this example, the programmer should for instance modify the program as follows.

using System.IO;
using System;

namespace DocumentationExamples
{
    public class UnusedReturnValue
    {
        private string name;
        private static int counter;
        public UnusedReturnValue(string name)
        {
            this.name = name;
        }
        public UnusedReturnValue(string name, int offset) : this(name)
        {
            counter += offset;
        }
        public static void Main(string[] args)
        {
            DirectoryInfo infoDir = Directory.CreateDirectory("dir");
            if (!infoDir.Exists)
            {
                Console.WriteLine("directory could not be created");
                Environment.Exit(0);
            }

            new UnusedReturnValue("Albert", 13);
        }
        public string GetName()
        {
            return name;
        }
    }
}

解決法

Use the return value of the method or remove the method or constructor call completely, since it has no effect on the heap of the caller, or check if the logic of the code is broken because the return value of a call to a pure method or constructor is not used.

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

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