The analyzer detected a possible error that deals with property and event accessors not using their 'value' parameter.
Consider the following example:
private bool _visible;
public bool IsVisible
{
get { return _visible; }
set { _visible = true; }
}
When setting a new value for the "IsVisible" property, the programmer intended to save the result into the "_visible" variable but made a mistake. As a result, changing the property won't affect the object state in any way.
This is the fixed version:
public bool IsVisible
{
get { return _visible; }
set { _visible = value; }
}
Code of the following pattern will also trigger the warning:
public bool Unsafe {
get { return (flags & Flags.Unsafe) != 0; }
set { flags |= Flags.Unsafe; }
}
In this case, the 'set' method is used to change the flag state and there's no error. However, using a property like that may be misleading, as the assignments "myobj.Unsafe = true" and "myobj.Unsafe = false" will have the same result.
To reset the state of the internal variable, it is better to use a function rather than a property:
public bool Unsafe
{
get { return (flags & Flags.Unsafe) != 0; }
}
public void SetUnsafe()
{
flags |= Flags.Unsafe;
}
If you can't do without the property, mark this line with special comment "//-V3077" to tell the analyzer not to output the warning on this property in future:
public bool Unsafe {
get { return (flags & Flags.Unsafe) != 0; }
set { flags |= Flags.Unsafe; } //-V3077
}
For a complete overview of all false-positive suppression mechanisms, see the documentation.
You can look at examples of errors detected by the V3077 diagnostic. |