C#


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

Summary

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.

Properties

Class Name Useless Assignment (C#)
Significance reliability
Mnemonic CSHARP.STRUCT.UA
Categories
CWE CWE:665 Improper Initialization
Availability Available for C# only.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Useless Assignment (C#)"

Example

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;
            // ...
            // ...
        }
}

Resolution

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

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.