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

Rule S3693: Exception constructors should not throw exceptions #629

Merged
merged 3 commits into from
Aug 2, 2017

Conversation

Evangelink
Copy link
Contributor

Fix #584

Copy link
Contributor

@michalb-sonar michalb-sonar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
Please just add the tests.

@@ -0,0 +1,60 @@
using System;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a couple of tests:

  • Throw without object initialization, e.g. var a = new Exception(); throw a;
  • Rethrow, e.g. throw;.

classDeclaration.Members
.OfType<ConstructorDeclarationSyntax>()
.Select(ctor => ctor.DescendantNodes().OfType<ThrowStatementSyntax>())
.Where(throwStatementsPerCtor => throwStatementsPerCtor.Any())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[optional] Wouldn't creating a list and checking length be faster - as it avoids duplicating calls to OfType()? I'm genuinely curious. Feel free to skip it, in that case I'll add it to my todo list.

.Select(ctor => ctor.DescendantNodes().OfType<ThrowStatementSyntax>())
.Where(throwStatementsPerCtor => throwStatementsPerCtor.Any())
.ToList()
.ForEach(throwStatementsPerCtor =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[optional] That's quite a long statement already, but actually reads quite well. I'd extract the bit in foreach to a separate method.

Copy link
Contributor

@valhristov valhristov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

{
public MyException4()
{
throw; // Noncompliant
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a valid statement? what will be thrown here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is a valid statement in this condition but I guess the idea was to be sure the analyzer doesn't crash.

public sealed class ExceptionConstructorShouldNotThrow : SonarDiagnosticAnalyzer
{
internal const string DiagnosticId = "S3693";
private const string MessageFormat = "Avoid throwing exception in this constructor.";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be this should be Avoid throwing exceptions in this constructor. (e.g. plural exception)

@Evangelink
Copy link
Contributor Author

rebase done

@Evangelink Evangelink merged commit 0201b8b into master Aug 2, 2017
@Evangelink Evangelink deleted the rule-S3693 branch August 2, 2017 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants