Skip to content

Add ThreadStatic correctness analyzers - #1646

Merged
Andrew Arnott (AArnott) merged 10 commits into
mainfrom
aarnott-add-threadstatic-analyzer
Aug 22, 2026
Merged

Andrew Arnott (AArnott) merged 10 commits into
mainfrom
aarnott-add-threadstatic-analyzer

Conversation

@AArnott

Copy link
Copy Markdown
Member

Summary

  • add VSTHRD116 to report ThreadStaticAttribute on non-static fields and auto-property backing fields
  • add VSTHRD117 to report inline initialization of thread-static fields and auto-property backing fields
  • support C# and Visual Basic from one symbol/operation analyzer, with localized descriptors and published help pages
  • document the overlap with .NET SDK rules CA2259 and CA2019; no code fixes are offered because the safe correction depends on intended state and initialization semantics

Validation

  • focused VSTHRD116/VSTHRD117 tests across net8.0, net8.0-windows, and net472
  • full analyzer test project: 1,509 passed, 33 skipped
  • full Release solution build
  • analyzer NuGet pack inspection for both analyzers/cs and analyzers/vb

Closes #594
Closes #595

Report ThreadStatic attributes on instance fields and inline initialization of thread-static state, with C# and VB coverage and analyzer documentation.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings August 21, 2026 16:22

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.

Pull request overview

Adds two new analyzers to improve correctness around ThreadStaticAttribute usage (VSTHRD116/VSTHRD117), covering both C# and Visual Basic, and publishes accompanying documentation and tests as part of the existing analyzer suite.

Changes:

  • Introduces VSTHRD116ThreadStaticAnalyzer to flag ThreadStaticAttribute on non-static fields (including auto-property backing fields via [field: ...]).
  • Adds VSTHRD117 diagnostics to flag inline initialization of thread-static fields/backing fields via operation analysis.
  • Adds localized strings plus DocFX/GitHub Pages documentation and a new test suite validating both rules for C# and VB.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/Microsoft.VisualStudio.Threading.Analyzers.Tests/VSTHRD116ThreadStaticAnalyzerTests.cs Adds coverage for VSTHRD116/VSTHRD117 across C# and VB scenarios.
src/Microsoft.VisualStudio.Threading.Analyzers/VSTHRD116ThreadStaticAnalyzer.cs Implements the new analyzer and both diagnostics (non-static + inline initialization).
src/Microsoft.VisualStudio.Threading.Analyzers/Types.cs Adds Types.ThreadStaticAttribute.FullName for reliable metadata lookup.
src/Microsoft.VisualStudio.Threading.Analyzers/Strings.resx Adds localized titles/messages for VSTHRD116 and VSTHRD117.
docfx/analyzers/VSTHRD116.md Adds DocFX help page for the new VSTHRD116 rule.
docfx/analyzers/VSTHRD117.md Adds DocFX help page for the new VSTHRD117 rule.
docfx/analyzers/toc.yml Registers the new analyzer docs in the DocFX TOC.
docfx/analyzers/index.md Adds the new rules to the analyzer index table.
doc/analyzers/VSTHRD116.md Adds redirect stub to GitHub Pages for VSTHRD116.
doc/analyzers/VSTHRD117.md Adds redirect stub to GitHub Pages for VSTHRD117.

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

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings August 21, 2026 16:36
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

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.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings August 21, 2026 16:42
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

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.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings August 21, 2026 16:46

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.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

Suppressed comments (1)

Previously missed (1) — in code that hasn't changed since the last review.

test/Microsoft.VisualStudio.Threading.Analyzers.Tests/ThreadStaticAnalyzerTests.cs:177

  • Visual Basic tests cover fields, but there are no tests exercising the analyzer’s support for auto-property backing fields (e.g., <Field: ThreadStatic> on an auto-implemented Property, and the Property initializer case). Since the analyzer explicitly handles SymbolKind.Property and OperationKind.PropertyInitializer, adding VB auto-property tests would help prevent regressions and validate the intended cross-language behavior.
    [Fact]
    public async Task NonStaticFields_VisualBasic()
    {
        const string test = """
            Imports System

            Class Test
                <ThreadStatic>
                Private {|VSTHRD116:first|} As Object

                <Global.System.ThreadStaticAttribute>
                Private {|VSTHRD116:second|}, {|VSTHRD116:third|} As Integer

            End Class
            """;

        await VBVerify.VerifyAnalyzerAsync(test);
    }

Comment thread docfx/analyzers/VSTHRD117.md Resolved
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings August 21, 2026 23:14

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.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

Suppressed comments (2)

Previously missed (2) — in code that hasn't changed since the last review.

src/Microsoft.VisualStudio.Threading.Analyzers/ThreadStaticAnalyzer.cs:16

  • Consider renaming this analyzer type to match the established naming convention in this project where DiagnosticAnalyzer implementations are prefixed with their primary diagnostic ID (e.g. VSTHRD115AvoidJoinableTaskContextCtorWithNullArgsAnalyzer, VSTHRD111UseConfigureAwaitAnalyzer). This makes analyzers easier to discover/grep when triaging diagnostics.
public class ThreadStaticAnalyzer : DiagnosticAnalyzer

test/Microsoft.VisualStudio.Threading.Analyzers.Tests/ThreadStaticAnalyzerTests.cs:7

  • To align with the existing test naming pattern in this project (most analyzer test classes start with the diagnostic ID, e.g. VSTHRD115AvoidJoinableTaskContextCtorWithNullArgTests), consider prefixing this test class name with the primary ID it covers.
public class ThreadStaticAnalyzerTests

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings August 21, 2026 23:22

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.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings August 21, 2026 23:45
This was referenced Oct 27, 2026
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.

Analyzer proposal: Do not use ThreadStatic on non-static fields Analyzer proposal: ThreadStatic fields should not be initialized inline

3 participants