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 S3431 FP: Don't raise if assertions are done in catch or finally #9615

Merged
merged 3 commits into from
Aug 9, 2024

Conversation

sebastien-marichal
Copy link
Contributor

@sebastien-marichal sebastien-marichal commented Aug 7, 2024

Fixes #8300

RSPEC PR: SonarSource/rspec#4127

Copy link
Contributor

@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource left a comment

Choose a reason for hiding this comment

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

The RSPEC looks good. Some performance improvements are needed.

@@ -35,6 +36,7 @@ public abstract class ExpectedExceptionAttributeShouldNotBeUsedBase<TSyntaxKind>
context.RegisterNodeAction(Language.GeneratedCodeRecognizer, c =>
{
if (HasMultiLineBody(c.Node)
&& !AssertInCatchFinallyBlock(c.Node)
Copy link
Contributor

Choose a reason for hiding this comment

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

The rule will go through every method in a project and do this check with syntax walkers. Even though methods marked with ExpectedException are quite rare.
I'd optimize this rule in two places (this can be done in a separate PR):

  1. Before calling AssertInCatchFinallyBlock() check if the method declaration has any attributes called ExpectedException. This can be done on the syntax level. We're going to get a couple of FNs/FPs for some exotic scenarios: aliased attribute, partial test methods. These are quite rare, so I think it's ok.
  2. Before you register the analyzer, check if the project references MsTest or NUnit (with version < 3.0.0).

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 will do in another PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link

sonarcloud bot commented Aug 9, 2024

Copy link

sonarcloud bot commented Aug 9, 2024

Copy link
Contributor

@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource left a comment

Choose a reason for hiding this comment

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

LGTM. There's one unresolved comment left. You can either address it here or in a separate PR.

@sebastien-marichal sebastien-marichal merged commit 4bf4902 into master Aug 9, 2024
21 checks passed
@sebastien-marichal sebastien-marichal deleted the sma/s3431-sonarway branch August 9, 2024 10:14
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.

Fix S3431 FP: Don't raise if assertions are done in catch or finally
2 participants