JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C#


CSHARP.STRUCT.UA.DEFAULT : Useless Assignment to Default (C#)

Summary

A field is assigned to its default value inside a constructor or finaliser.

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 to Default (C#)
Significance reliability
Mnemonic CSHARP.STRUCT.UA.DEFAULT
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 to Default (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.

 

To report problems with this documentation, please visit https://support.codesecure.com/.