Skip to content

Reduce VSTHRD117 severity for default initializers - #1658

Merged
Andrew Arnott (AArnott) merged 2 commits into
mainfrom
aarnott-threadstatic-default-severity
Aug 26, 2026
Merged

Andrew Arnott (AArnott) merged 2 commits into
mainfrom
aarnott-threadstatic-default-severity

Conversation

@AArnott

Copy link
Copy Markdown
Member

Explicitly initializing a [ThreadStatic] field to its type's default value is redundant, but it does not create inconsistent values across threads. Report these inline initializers as informational instead of warning-level diagnostics.

The analyzer now recognizes semantic default values such as null, default, numeric zero, false, and \0 for fields and auto-property backing fields. Non-default initializers and static-constructor assignments remain warnings. C# and Visual Basic tests cover both severity paths, and the VSTHRD117 documentation explains the distinction.

Validation

  • dotnet test --project .\test\Microsoft.VisualStudio.Threading.Analyzers.Tests\Microsoft.VisualStudio.Threading.Analyzers.Tests.csproj -c Release -- --filter-class ThreadStaticAnalyzerTests --filter-not-trait "FailsInCloudTest=true"

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 72e7451d-7612-42b6-bf1d-961d53b6c2f3
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 72e7451d-7612-42b6-bf1d-961d53b6c2f3
Copilot AI lite review requested due to automatic review settings August 26, 2026 14:38

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

This PR updates the VSTHRD117 analyzer so that redundant inline initializers of [ThreadStatic] fields/backing fields that assign a semantic default value are reported as Info rather than Warning, while keeping non-default inline initializers and type-initializer (static ctor) assignments at Warning severity.

Changes:

  • Adjust VSTHRD117 reporting for field/property inline initializers to emit Info when the initializer is a semantic default (null, default, numeric zero, false, '\0', etc.).
  • Expand C# and Visual Basic tests to validate both Info and Warning severities for inline initializers.
  • Update the VSTHRD117 documentation to describe the new severity distinction and recommended fixes.

Reviewed changes

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

File Description
test/Microsoft.VisualStudio.Threading.Analyzers.Tests/ThreadStaticAnalyzerTests.cs Updates/expands analyzer tests to assert per-case Info vs Warning severity for inline initializers in C# and VB.
src/Microsoft.VisualStudio.Threading.Analyzers/ThreadStaticAnalyzer.cs Implements semantic default-value recognition for inline initializers and reports VSTHRD117 as Info for default initializers.
docfx/analyzers/VSTHRD117.md Documents the new severity behavior and clarifies the “Solution” guidance for default vs non-default initializers.

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

@AArnott
Andrew Arnott (AArnott) merged commit fc3767d into main Aug 26, 2026
10 checks passed
@AArnott
Andrew Arnott (AArnott) deleted the aarnott-threadstatic-default-severity branch August 26, 2026 16:41
This was referenced Oct 26, 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.

3 participants