-
Notifications
You must be signed in to change notification settings - Fork 228
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
Conversation
analyzers/src/SonarAnalyzer.CSharp/Rules/ExpectedExceptionAttributeShouldNotBeUsed.cs
Outdated
Show resolved
Hide resolved
...yzers/tests/SonarAnalyzer.Test/TestCases/ExpectedExceptionAttributeShouldNotBeUsed.MsTest.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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.
analyzers/src/SonarAnalyzer.CSharp/Rules/ExpectedExceptionAttributeShouldNotBeUsed.cs
Outdated
Show resolved
Hide resolved
@@ -35,6 +36,7 @@ public abstract class ExpectedExceptionAttributeShouldNotBeUsedBase<TSyntaxKind> | |||
context.RegisterNodeAction(Language.GeneratedCodeRecognizer, c => | |||
{ | |||
if (HasMultiLineBody(c.Node) | |||
&& !AssertInCatchFinallyBlock(c.Node) |
There was a problem hiding this comment.
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):
- Before calling
AssertInCatchFinallyBlock()
check if the method declaration has any attributes calledExpectedException
. 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. - Before you register the analyzer, check if the project references MsTest or NUnit (with version < 3.0.0).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
analyzers/tests/SonarAnalyzer.Test/Rules/ExpectedExceptionAttributeShouldNotBeUsedTest.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.Test/Rules/ExpectedExceptionAttributeShouldNotBeUsedTest.cs
Outdated
Show resolved
Hide resolved
...yzers/tests/SonarAnalyzer.Test/TestCases/ExpectedExceptionAttributeShouldNotBeUsed.MsTest.cs
Outdated
Show resolved
Hide resolved
...yzers/tests/SonarAnalyzer.Test/TestCases/ExpectedExceptionAttributeShouldNotBeUsed.MsTest.cs
Outdated
Show resolved
Hide resolved
...yzers/tests/SonarAnalyzer.Test/TestCases/ExpectedExceptionAttributeShouldNotBeUsed.MsTest.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.VisualBasic/Rules/ExpectedExceptionAttributeShouldNotBeUsed.cs
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.VisualBasic/Rules/ExpectedExceptionAttributeShouldNotBeUsed.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.CSharp/Rules/ExpectedExceptionAttributeShouldNotBeUsed.cs
Outdated
Show resolved
Hide resolved
Quality Gate passed for 'Sonar .NET Java Plugin'Issues Measures |
Quality Gate passed for 'SonarAnalyzer for .NET'Issues Measures |
There was a problem hiding this 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.
Fixes #8300
RSPEC PR: SonarSource/rspec#4127