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 |
The returned value of a non-void method is thrown away but should instead be checked or used.
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.
| Class Name | Ignored Return Value (C#) | ||||||
|---|---|---|---|---|---|---|---|
| Significance | reliability | ||||||
| Mnemonic | CSHARP.FUNCS.IRV | ||||||
| Categories |
|
||||||
| 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="Ignored Return Value (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.
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.