The following code incorrectly suggests that I use the Foo._scroll field in Bar.Scroll, even though (1) the field is private, and therefore inaccessible, and (2) the Foo.Scroll setter (in production code) performs additional actions both before and after setting _scroll, so I’d need to call Foo.Scroll from Bar.Scroll anyway.
(S2292 and S1185 don’t appear in my production code, so I suppressed them here.)
namespace S4275;
internal class Foo {
private int _scroll;
#pragma warning disable S2292 // For simplicity, the non-trivial portions of this property have been elided.
public virtual int Scroll {
#pragma warning restore S2292
get => _scroll;
set {
// do validation
_scroll = value;
// do other stuff
}
}
}
internal class Bar : Foo {
#pragma warning disable S1185 // For simplicity, the "do more than call the same member" portions of this property have been elided.
public override int Scroll {
#pragma warning restore S1185
get => base.Scroll; // Incorrect S4275: private fields cannot be accessed in derived classes
set { // Incorrect S4275: (1) private fields cannot be accessed in derived classes,
// and (2) failing to call the base class setter would require re-implementing the before- and after- actions.
base.Scroll = value;
// do more other stuff
}
}
}
I’m using SonarQube 8.26.0.14164 on Visual Studio 17.14.14 and Windows 11 24H2.