A property accessor reads or writes another field than the one the property is named after
What it finds
public int Width { get { return _height; } } in a class that has a _width field reads the wrong field, typically after the property was copied from another one; callers get or change the wrong value with no error.
Reported are get and set accessors whose whole body returns or assigns a field of the type other than the one the property is named after, when the type declares a field of that name (compared without _, m_ or s_ and without case). A field whose name contains the property's, as _lastWidth for Width, is taken as a deliberate stand-in. To keep to copies, the field used must be the field of another property of the type, and the property's own field must not be read elsewhere in the type, for a getter, or assigned elsewhere, for a setter: a field that nothing else reads is the one the getter was meant to return. Static fields and properties are left alone.
From the rule's source, accessor_wrong_field.rs.
class Box{ private int _width; private int _height; private int _lastWidth; public int Width { get { return _width; } set { _width = value; } } public int Height { get { return _width; }tx0 reports: the get of Height returns _width, not _height set => this._width = value;tx0 reports: the set of Height assigns _width, not _height } public int PreviousWidth => _lastWidth; public int LastWidth { get { return _lastWidth; } } public int Depth { get { return _lastWidth; } }} class Margin{ private int _top; private int _left; public int Top => _top; public int Left { get { return _top; } } public string Describe() => _left.ToString();} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/csharp:accessor-wrong-field" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/csharp:accessor-wrong-field] reason="..."tx0 explain tx0/csharp:accessor-wrong-field shows the same in the terminal. Found a false positive? Report it.