From d2ae16e4adfcfe6c085e2b3dbf670ae23bb17a55 Mon Sep 17 00:00:00 2001 From: Andrew Lock Date: Mon, 17 Feb 2025 13:32:29 +0000 Subject: [PATCH] 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: --- .../ThreadAbortCodeFixProvider.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/tracer/src/Datadog.Trace.Tools.Analyzers/ThreadAbortAnalyzer/ThreadAbortCodeFixProvider.cs b/tracer/src/Datadog.Trace.Tools.Analyzers/ThreadAbortAnalyzer/ThreadAbortCodeFixProvider.cs index 229664377027..ebea99470ba2 100644 --- a/tracer/src/Datadog.Trace.Tools.Analyzers/ThreadAbortAnalyzer/ThreadAbortCodeFixProvider.cs +++ b/tracer/src/Datadog.Trace.Tools.Analyzers/ThreadAbortAnalyzer/ThreadAbortCodeFixProvider.cs @@ -45,26 +45,22 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) var diagnosticSpan = diagnostic.Location.SourceSpan; // Find the while block catch declaration identified by the diagnostic. - var whileStatement = root.FindToken(diagnosticSpan.Start) + var catchClause = root.FindToken(diagnosticSpan.Start) .Parent .AncestorsAndSelf() - .OfType().First(); + .OfType().First(); // Register a code action that will invoke the fix. context.RegisterCodeFix( CodeAction.Create( title: "Rethrow exception", - createChangedDocument: c => AddThrowStatement(context.Document, whileStatement, c), + createChangedDocument: c => AddThrowStatement(context.Document, catchClause, c), equivalenceKey: nameof(ThreadAbortCodeFixProvider)), diagnostic); } - private async Task AddThrowStatement(Document document, WhileStatementSyntax whileStatement, CancellationToken cancellationToken) + private static async Task AddThrowStatement(Document document, CatchClauseSyntax catchBlock, CancellationToken cancellationToken) { - var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); - - var catchBlock = ThreadAbortSyntaxHelper.FindProblematicCatchClause(whileStatement, semanticModel); - // This messes with the whitespace, but it's a PITA to get that right var throwStatement = SyntaxFactory.ThrowStatement(); var statements = catchBlock.Block.Statements.Add(throwStatement);