Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix S2372: Allow some exceptions being raised in properties as per CA1065 #694

Closed
jcurl opened this issue Aug 15, 2017 · 1 comment
Closed
Assignees
Labels
Type: False Positive Rule IS triggered when it shouldn't be.
Milestone

Comments

@jcurl
Copy link

jcurl commented Aug 15, 2017

Description

SonarLint raises S2372: Remove the exception throwing from this property getter, or refactor the property into a method. MSDN Rule CA1065 allows some instances, in particular ObjectDisposedException.

Property Get Methods

Properties are basically smart fields. Therefore, they should behave like a field as much as possible. Fields do not throw exceptions and neither should properties. If you have a property that throws an exception, consider making it a method.

The following exceptions are allowed to be thrown from a property get method:

  • System.InvalidOperationException and all derivatives (including System.ObjectDisposedException)

Repro steps

The following code will raise a warning S2372, which is wrong according to CA1065

public override bool CanRead
{
    get
    {
        if (m_Handler == null) throw new ObjectDisposedException("AtomicFileStream");
        return m_Handler.FileStream.CanRead;
    }
}

Expected behavior

The rule should not trigger in the example code in the repro step given

Actual behavior

The rule is triggered, making a user think they have to change code when it is fine according to MSDN guidelines.

Known workarounds

None

Related information

  • SonarC# Version 3.4.0.x
  • Visual Studio Version 2015
@Evangelink Evangelink added Area: RSPEC Type: False Positive Rule IS triggered when it shouldn't be. labels Aug 15, 2017
@Evangelink
Copy link
Contributor

Hi @jcurl,
Thanks again :). This suggestion totally make sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: False Positive Rule IS triggered when it shouldn't be.
Projects
None yet
Development

No branches or pull requests

2 participants