- What language is this for? C#
- Which rule? S2930 “IDisposables” should be disposed
- Why do you believe it’s a false-positive/false-negative? The class implements a
Dispose()method disposing of theIDisposablemember. - Are you using
- SonarQube Community Build, 26.2
- SonarQube for IDE - Visual Studio, 9.6.0.16157, in connected mode
- How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
public void Dispose()
{
((IDisposable)_cancellationTokenSource).Dispose();
}
The cast to IDisposable was provided by the IDE’s code fix on the IDisposable interface (“Implement interface through ‘_cancellationTokenSource’”). Removing the cast causes S2930 to no longer be detected. While it’s redundant in this case, there are other IDisposable implementations where it is not (i.e. any where IDisposable is explicitly implemented).