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 bug when exception declared in filter is thrown #4285

Merged
merged 6 commits into from
Oct 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public override void Initialize(AnalysisContext analysisContext)
if (ancestor.Kind == OperationKind.CatchClause &&
ancestor is ICatchClauseOperation catchClause)
{
if (catchClause.Locals.Contains(localReference.Local) &&
if ((catchClause.ExceptionDeclarationOrExpression is not IVariableDeclaratorOperation variableDeclaratorOperation || SymbolEqualityComparer.Default.Equals(variableDeclaratorOperation.Symbol, localReference.Local)) &&
Copy link
Contributor

@mavasani mavasani Oct 3, 2020

Choose a reason for hiding this comment

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

Why are we skipping the check if this not a variable declaration?

Suggested change
if ((catchClause.ExceptionDeclarationOrExpression is not IVariableDeclaratorOperation variableDeclaratorOperation || SymbolEqualityComparer.Default.Equals(variableDeclaratorOperation.Symbol, localReference.Local)) &&
if (catchClause.ExceptionDeclarationOrExpression is IVariableDeclaratorOperation variableDeclaratorOperation && SymbolEqualityComparer.Default.Equals(variableDeclaratorOperation.Symbol, localReference.Local) &&

catchClause.Locals.Contains(localReference.Local) &&
!IsReassignedInCatch(catchClause, localReference))
{
context.ReportDiagnostic(throwOperation.CreateDiagnostic(Rule));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,30 @@ End Class
");
}

[Fact]
public async Task CA2200_NoDiagnosticsForThrowAnotherExceptionInWhenClause()
{
await VerifyCS.VerifyAnalyzerAsync(@"
using System;

public abstract class C
{
public void M()
{
try
{
}
catch (Exception ex) when (Map(ex, out Exception mappedEx))
Youssef1313 marked this conversation as resolved.
Show resolved Hide resolved
{
throw mappedEx;
}
}

protected abstract bool Map(Exception ex, out Exception ex2);
}
");
}

[Fact]
public async Task CA2200_DiagnosticForThrowCaughtException()
{
Expand Down