V3156. The argument of the method is not expected to be null.
The analyzer has detected a possible issue, where the value 'null' is passed as an argument to a method that is not supposed to get the value 'null' for this argument.
This may result in, for example, throwing an exception or incorrectly executing the method.
When coding, it might be difficult to make sure you have null checks in all sensitive spots. Such a check is especially important when a variable that can take the value 'null' is passed to a method where it is further used as an argument to another method that does not expect the value 'null' for this argument.
Consider the following contrived example:
void Method(string[] args)
{
var format = args.Length != 0 ? args[0] : null;
....
var message = string.Format(format, _value);
// do something
}
If the 'args' array is empty, the 'format' variable will be assigned the value 'null'. Consequently, that same value will be passed to the 'string.Format' method as its first argument, resulting in throwing an exception. This code can be fixed as follows:
void Method(string[] args)
{
var format = args.Length != 0 ? args[0] : null;
....
if (format == null)
{
// process an error
return;
}
var message = string.Format(format, _value);
// do something
}
Let's make the example above a bit more complex:
void Method(string[] args)
{
var format = args.Length != 0 ? args[0] : null;
....
WriteInfo(format);
}
void WriteInfo(string format)
{
Console.Write(format, _value);
}
The 'format' variable still depends on 'args.Length' and could potentially be assigned the value 'null'. In this case, we assume that 'format == null'. Therefore, it is also the value 'null' that will be passed to the 'WriteInfo' method. This value will then be passed to the 'Console.WriteLine' method as its first argument, resulting in an 'ArgumentNullException'.
This snippet is fixed in the same way as the previous one:
void Method(string[] args)
{
var format = args.Length != 0 ? args[0] : null;
....
WriteInfo(format);
}
void WriteInfo(string format)
{
if (format == null)
{
// process an error
return;
}
Console.Write(format, _value);
}
The next example is taken from a real program:
private static string HandleSuffixValue(object val, StringSegment suffixSegment)
{
....
var res = string.Format(suffixSegment.Value, val).TrimEnd(']');
return res == "" ? null : res;
}
The first argument of the 'string.Format' method must not be 'null'. Let's see what 'suffixSegment.Value' returns:
public string Value
{
get
{
if (HasValue)
{
return Buffer.Substring(Offset, Length);
}
else
{
return null;
}
}
}
If 'HasValue' is 'false', then 'Value' will return 'null'. It means the call of the 'string.Format' method could potentially throw an exception in this case. This is how it can be fixed:
private static string HandleSuffixValue(object val, StringSegment suffixSegment)
{
....
if (suffixSegment.Value == null)
{
return null;
}
var res = string.Format(suffixSegment.Value, val).TrimEnd(']');
return res == "" ? null : res;
}
This diagnostic is classified as:
You can look at examples of errors detected by the V3156 diagnostic. |