Skip to content

Commit d2ae16e

Browse files
authored
Minor refactor in ThreadAbortCodeFixProvider (#6673)
## Summary of changes Don't re-do work in code fix ## Reason for change Was looking at something else, and noticed that this was doing more work than necessary ## Implementation details The diagnostic is already located at the "problematic" catch, so we don't need to try finding it _again_ in the code fix ## Test coverage There are existing unit tests for this, and they all pass, so :shipit:
1 parent 7d24b3d commit d2ae16e

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

tracer/src/Datadog.Trace.Tools.Analyzers/ThreadAbortAnalyzer/ThreadAbortCodeFixProvider.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,22 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
4545
var diagnosticSpan = diagnostic.Location.SourceSpan;
4646

4747
// Find the while block catch declaration identified by the diagnostic.
48-
var whileStatement = root.FindToken(diagnosticSpan.Start)
48+
var catchClause = root.FindToken(diagnosticSpan.Start)
4949
.Parent
5050
.AncestorsAndSelf()
51-
.OfType<WhileStatementSyntax>().First();
51+
.OfType<CatchClauseSyntax>().First();
5252

5353
// Register a code action that will invoke the fix.
5454
context.RegisterCodeFix(
5555
CodeAction.Create(
5656
title: "Rethrow exception",
57-
createChangedDocument: c => AddThrowStatement(context.Document, whileStatement, c),
57+
createChangedDocument: c => AddThrowStatement(context.Document, catchClause, c),
5858
equivalenceKey: nameof(ThreadAbortCodeFixProvider)),
5959
diagnostic);
6060
}
6161

62-
private async Task<Document> AddThrowStatement(Document document, WhileStatementSyntax whileStatement, CancellationToken cancellationToken)
62+
private static async Task<Document> AddThrowStatement(Document document, CatchClauseSyntax catchBlock, CancellationToken cancellationToken)
6363
{
64-
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
65-
66-
var catchBlock = ThreadAbortSyntaxHelper.FindProblematicCatchClause(whileStatement, semanticModel);
67-
6864
// This messes with the whitespace, but it's a PITA to get that right
6965
var throwStatement = SyntaxFactory.ThrowStatement();
7066
var statements = catchBlock.Block.Statements.Add(throwStatement);

0 commit comments

Comments
 (0)