Skip to content

Fix VSTHRD107 to detect unawaited tasks in using variable declarations - #1698

Open
Austin Aviv (Austinviv) wants to merge 3 commits into
microsoft:mainfrom
Austinviv:fix/vsthrd107-using-declarations
Open

Austin Aviv (Austinviv) wants to merge 3 commits into
microsoft:mainfrom
Austinviv:fix/vsthrd107-using-declarations

Conversation

@Austinviv

@Austinviv Austin Aviv (Austinviv) commented Oct 9, 2026 •

Copy link
Copy Markdown

Fixes #1697

Description

This PR expands VSTHRD107AwaitTaskWithinUsingExpressionAnalyzer and VSTHRD107AwaitTaskWithinUsingExpressionCodeFix to detect and fix unawaited Task / Task expressions when used within using variable declarations:

  • using (var releaser = semaphore.EnterAsync())
  • using (IDisposable releaser = semaphore.EnterAsync())
  • using var releaser = semaphore.EnterAsync(); (C# 8 using declarations)

Background

Because Task implements IDisposable, storing an unawaited task in a using variable declaration compiles without warning, but disposes the unawaited task instance instead of awaiting the releaser.

Previously, VSTHRD107 only inspected UsingStatementSyntax.Expression. It missed declaration forms, and VSTHRD107AwaitTaskWithinUsingExpressionCodeFix threw an exception when .Expression was null.

Changes

  1. Analyzer:
    • Registered SyntaxKind.LocalDeclarationStatement in addition to SyntaxKind.UsingStatement.
    • Added checking for usingStatement.Declaration and localDeclaration.Declaration variable initializers.
    • Handled C# 8 using var declarations (ignoring wait using).
  2. Code Fix:
    • Replaced direct mutation of usingStatement.Expression with node replacement targeting the containing statement (ReplaceExpressionWithAwait).
    • Cleanly wraps expressions with wait without unnecessary parentheses around invocations or identifier names.
  3. Docs and Tests:
    • Updated documentation in docfx/analyzers/VSTHRD107.md.
    • Added unit tests covering sync and async methods, using (var ...), using var ...;, and explicit IDisposable type annotations.

Copilot AI balanced review requested due to automatic review settings October 9, 2026 03:25
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Some offered fixes produce uncompilable code for unsupported containing functions or tasks without disposable results.

1 open finding
What changed in this PR

Expands VSTHRD107 to detect unawaited tasks in using declarations and updates its code fix.

Changes:

  • Analyzes using-statement and C# 8 using-declaration initializers.
  • Reworks code-fix expression replacement.
  • Adds documentation and test coverage.
File Description
VSTHRD107AwaitTaskWithinUsingExpressionAnalyzerTests.cs Tests new declaration forms.
VSTHRD107AwaitTaskWithinUsingExpressionAnalyzer.cs Analyzes using-variable initializers.
VSTHRD107AwaitTaskWithinUsingExpressionCodeFix.cs Adds await to diagnosed expressions.
VSTHRD107.md Documents supported forms.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@Austinviv

Austin Aviv (Austinviv) commented Oct 10, 2026 •

Copy link
Copy Markdown
Author

Hi, could any maintainer please review and approve this PR when you get a chance? I'd like to check the workflow test results to see how many tests passed and failed. Thank you!

@Austinviv

Copy link
Copy Markdown
Author

Hi maintainers, could you please approve the workflow run for this PR? Thank you!

@drewnoakes

Copy link
Copy Markdown
Member

Austin (@AustinAviv) I approved the workflows to run. I'll defer to Andrew Arnott (@AArnott) for the actual merge.

@Austinviv

Copy link
Copy Markdown
Author

Austin (Austin (@AustinAviv)) I approved the workflows to run. I'll defer to Andrew Arnott (Andrew Arnott (@AArnott)) for the actual merge.

Hi Andrew Arnott (@AArnott) Drew Noakes (@drewnoakes), thanks, Drew, for approving the previous workflow run! Could you please approve the new workflow run as well if approval is required? Andrew, when you get a chance, I'd appreciate your review of this PR. I'm happy to address any feedback or make further changes. Thanks both for your time and support!

@drewnoakes

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@Austinviv

Copy link
Copy Markdown
Author

/azp run

Thanks Drew Noakes (Drew Noakes (@drewnoakes))for approving the workflow run

Let me know if you have any additional queries regarding this PR.

@AArnott Andrew Arnott (AArnott) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for submitting. I look forward to merging after a couple of small fixes.

{
var usingStatement = (UsingStatementSyntax)context.Node;
if (usingStatement.Expression is object)
if (usingStatement.AwaitKeyword.IsKind(SyntaxKind.AwaitKeyword))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"if the await keyword is an await keyword"?? This deserves a code comment explaining why it makes sense.

…D107AwaitTaskWithinUsingExpressionCodeFix.cs

Co-authored-by: Andrew Arnott <andrew@mysideoftheweb.com>
Copilot AI balanced review requested due to automatic review settings October 15, 2026 12:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The code fix can introduce compiler errors for explicitly task-typed declarations.

1 open finding
1 resolved since last review

🧠 Review effort: Balanced

@Austinviv

Copy link
Copy Markdown
Author

Thanks for submitting. I look forward to merging after a couple of small fixes.

Thanks for the review Andrew Arnott (@AArnott) I'll address the feedback by adding an explanatory comment for the AwaitKeyword check and reviewing the code-fix edge cases to ensure the generated fixes are valid. I'll push the updates and verify the tests before requesting another review. Thanks for the guidance!

Copilot AI balanced review requested due to automatic review settings October 15, 2026 13:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The code fix can produce uncompilable code for explicitly task-typed declarations and contexts that prohibit awaiting.

0 open findings

1 resolved since last review

🧠 Review effort: Balanced

This branch has not been deployed

No deployments
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.

VSTHRD107 does not detect unawaited Task in using variable declarations or C# 8 using declarations

4 participants