Lucky hour! Grab a free trial license — offer ends in 00:59:58. Claim now
The analyzer has detected a potential error where a variable with a similar name is checked instead of the intended one.
The example:
bool UserProcessing()
{
....
var user1 = FindUser(id1);
if (user1 == null)
return false;
....
var user2 = FindUser(id2);
if (user1 == null)
return false;
....
}
In the example, two variables with similar names, user1 and user2, are initialized. After initialization, the value of each variable should be checked for null. However, due to a typo in the second if condition, the user2 variable will not be checked.
The fixed code:
bool UserProcessing()
{
....
var user1 = FindUser(id1);
if (user1 == null)
return false;
....
var user2 = FindUser(id2);
if (user2 == null)
return false;
....
}
Another example:
private readonly int _value;
....
public CustomType(int value)
{
....
_value = value < 0 ? 0 : value;
if (value == 0) { .... }
....
}
The _value field is assigned the value parameter that is limited to positive values. Instead of validating the field, the parameter is checked for 0, which may still have a negative value.
The fixed code:
private readonly int _value;
....
public CustomType(int value)
{
....
_value = value < 0 ? 0 : value;
if (_value == 0) { .... }
....
}
This diagnostic is classified as:
Was this page helpful?
Your message has been sent. We will email you at
If you do not see the email in your inbox, please check if it is filtered to one of the following folders: