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 |
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 | |||
| カテゴリー |
|
|||
| 対応言語 | 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.
設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。
To report problems with this documentation, please visit https://support.codesecure.com/.