C#


CSHARP.STRUCT.UA : Useless Assignment (C#)

要旨

A field is assigned to itself.

This checker finds assignments to local variables that are useless and could be consequently removed from code, hence obtaining a more efficient program. In some cases, these assignments hide actual bugs in the logic of the code.

プロパティ

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

using System;
using System.Collections.Generic;

namespace DocumentationExamples
{

    public class UselessAssignment
    {
        public static void Main(string[] args)
        { }

        private int f;
        public UselessAssignment(int f)
        {
            f = Process(f);                                                   // Unused Value: Write to Parameter (C#) warning issued here 
                                                                              //   programmer may have intended to write to this.f 
            Dictionary<string, int> factory = new Dictionary<string, int>();  // Unused Value: Variable (C#) warning issued here 
                                                                              //   'factory' immediately overwritten on following line 
            factory = BuildFactory();
            Go(factory);
        }
        private int Process(int x)
        {
            return x * 17 + 13;
        }
        private Dictionary<string, int> BuildFactory()
        {
            Dictionary<string, int> result = new Dictionary<string, intgt;
            {
                { "value", f }
            };
            return result;
        }
        private void Go(Dictionary<string, int> factory)
        {
            foreach (string s in factory.Keys)
                Console.WriteLine(s);
        }
        
        bool b = false;                                                       // Useless Assignment to Default (C#) warning issued here 
        
        public void Foo()
        {
            int n = 0;
            // ...
            n = n;                                                            // Useless Assignment (C#) warning issued here 
            // ...
        }
    }
}

In this example, the programmer should probably write into field this.f and initialize factory to the return value of BuildFactory(), immediately, as follows.


using System;
using System.Collections.Generic;

namespace DocumentationExamples
{

    public class UselessAssignment
    {
        public static void Main(string[] args)
        { }

        private int f;
        public UselessAssignment(int f)
        {
            f = Process(f);
            Dictionary<string, int> factory = BuildFactory();
            Go(factory);
        }
        
         ...
         
        bool b;
        
        public void Foo()
        {
            int n = 0;
            // ...
            // ...
        }
}

解決法

Remove the assignment and check if it actually hid a more serious algorithmic issue.

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

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