Skip to content

Add Requires analyzer and code fix support#357

Merged
AArnott merged 12 commits intomainfrom
aarnott/add-requires-analyzer-codefix
Apr 27, 2026
Merged

Add Requires analyzer and code fix support#357
AArnott merged 12 commits intomainfrom
aarnott/add-requires-analyzer-codefix

Conversation

@AArnott
Copy link
Copy Markdown
Member

@AArnott AArnott commented Apr 25, 2026

Why

This repo did not have any Roslyn analyzer or code-fix infrastructure yet, which meant users of Microsoft.VisualStudio.Validation got no IDE help to add the preferred Requires guards or simplify equivalent manual null-check patterns.

What changed

This adds the first analyzer package for the repo and wires it in using a compatibility-aware layout:

  • adds separate shared, C#-specific, code-fix/package, and test projects
  • splits Roslyn dependency management so shipped analyzer dependencies stay compatible with older VS/SDK baselines while tests use newer Roslyn test packages
  • adds four diagnostics and fixes for the current preferred guard forms:
    • VSV0001 suggests Requires.NotNull(value);
    • VSV0002 suggests Requires.Range(value >= 0, nameof(value));
    • VSV0003 replaces supported manual null-check-and-throw patterns with Requires.NotNull(value);
    • VSV0004 removes redundant explicit parameter-name arguments from Requires.NotNull(value, parameterName);
  • adds analyzer/code-fix tests covering diagnostic, no-diagnostic, and code-fix scenarios
  • adds analyzer package documentation, including a package README and DocFX analyzer pages

Non-obvious details

  • analyzer dependencies are intentionally separated into Directory.Packages.Analyzers.props, and Renovate is configured not to update that file automatically
  • the repo-level GitVersionBaseDirectory setting was removed so project-local version.json files in the analyzer projects take effect
  • the package project is the code-fix project, which packs the shared analyzer, C# analyzer, and code-fix assemblies under analyzers/dotnet/cs/

Validation

  • built the solution in Release
  • ran the analyzer test project
  • resolved remaining compiler and StyleCop warnings introduced by this work

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 25, 2026 14:47
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 25, 2026

Codecov Report

❌ Patch coverage is 75.38126% with 113 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.88%. Comparing base (2646d3f) to head (dc1d0a1).
⚠️ Report is 18 commits behind head on main.

Files with missing lines Patch % Lines
...nalyzers.CSharp/CSharpUseRequiresGuardsAnalyzer.cs 66.40% 54 Missing and 30 partials ⚠️
...odeFixes/CSharpUseRequiresGuardsCodeFixProvider.cs 83.97% 15 Missing and 14 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #357      +/-   ##
==========================================
- Coverage   80.50%   77.88%   -2.62%     
==========================================
  Files          11       14       +3     
  Lines         400      859     +459     
  Branches      111      224     +113     
==========================================
+ Hits          322      669     +347     
- Misses         58      126      +68     
- Partials       20       64      +44     
Flag Coverage Δ
Linux 79.18% <75.38%> (-4.43%) ⬇️
Windows 77.88% <75.38%> (-2.62%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds initial Roslyn analyzer + code-fix infrastructure for Microsoft.VisualStudio.Validation to help users adopt the preferred Requires guard patterns, along with packaging, tests, and DocFX documentation.

Changes:

  • Introduces analyzer, C# analyzer implementation, and code-fix/packaging projects for Requires guard recommendations (VSV0001–VSV0003).
  • Adds analyzer/code-fix test project with Roslyn testing helpers and test coverage for diagnostics + fixes.
  • Wires in documentation (DocFX pages + package README) and centralizes analyzer-vs-test Roslyn dependency versions.

Reviewed changes

Copilot reviewed 31 out of 31 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
test/Microsoft.VisualStudio.Validation.Analyzers.Tests/UseRequiresGuardsCodeFixTests.cs Adds code-fix tests for inserting/replacing Requires guards.
test/Microsoft.VisualStudio.Validation.Analyzers.Tests/UseRequiresGuardsAnalyzerTests.cs Adds analyzer tests for diagnostics/no-diagnostics scenarios.
test/Microsoft.VisualStudio.Validation.Analyzers.Tests/Microsoft.VisualStudio.Validation.Analyzers.Tests.csproj New analyzer test project targeting net8.0 (+ net472 on Windows).
test/Microsoft.VisualStudio.Validation.Analyzers.Tests/Helpers/UseRequiresGuardsVerifier.cs Test verifier wrapper for analyzer/code-fix under test.
test/Microsoft.VisualStudio.Validation.Analyzers.Tests/Helpers/ReferencesHelper.cs Provides reference assemblies + nuget.config discovery for Roslyn tests.
test/Microsoft.VisualStudio.Validation.Analyzers.Tests/Helpers/CSharpCodeFixVerifier`2.cs Shared Roslyn test harness utilities.
test/Microsoft.VisualStudio.Validation.Analyzers.Tests/Helpers/CSharpCodeFixVerifier`2+Test.cs Test fixture configuring references (including Requires assembly).
src/Microsoft.VisualStudio.Validation.Analyzers/version.json Enables project-local versioning for analyzer assembly.
src/Microsoft.VisualStudio.Validation.Analyzers/Microsoft.VisualStudio.Validation.Analyzers.csproj New shared analyzer project (non-packable) with Roslyn dependencies.
src/Microsoft.VisualStudio.Validation.Analyzers/KnownTypeNames.cs Centralizes metadata names used by analyzers.
src/Microsoft.VisualStudio.Validation.Analyzers/DiagnosticIds.cs Defines diagnostic IDs VSV0001–VSV0003.
src/Microsoft.VisualStudio.Validation.Analyzers/DiagnosticDescriptors.cs Defines DiagnosticDescriptors for the analyzer package.
src/Microsoft.VisualStudio.Validation.Analyzers.CodeFixes/version.json Enables project-local versioning for code-fix assembly.
src/Microsoft.VisualStudio.Validation.Analyzers.CodeFixes/README.md Package README listing analyzer IDs and descriptions.
src/Microsoft.VisualStudio.Validation.Analyzers.CodeFixes/Microsoft.VisualStudio.Validation.Analyzers.CodeFixes.csproj Packs analyzers + code fixes into Microsoft.VisualStudio.Validation.Analyzers.
src/Microsoft.VisualStudio.Validation.Analyzers.CodeFixes/CSharpUseRequiresGuardsCodeFixProvider.cs Implements code fixes for VSV0001–VSV0003.
src/Microsoft.VisualStudio.Validation.Analyzers.CSharp/version.json Enables project-local versioning for C# analyzer assembly.
src/Microsoft.VisualStudio.Validation.Analyzers.CSharp/Microsoft.VisualStudio.Validation.Analyzers.CSharp.csproj New C#-specific analyzer project.
src/Microsoft.VisualStudio.Validation.Analyzers.CSharp/CSharpUseRequiresGuardsAnalyzer.cs Implements parameter/if-statement analysis for Requires guard suggestions.
docfx/toc.yml Adds “Analyzers” section to DocFX navigation.
docfx/analyzers/toc.yml Adds DocFX TOC for analyzer pages.
docfx/analyzers/index.md Adds analyzer landing page listing VSV0001–VSV0003.
docfx/analyzers/VSV0001.md Documents “Add Requires.NotNull” rule.
docfx/analyzers/VSV0002.md Documents “Add Requires.Range” rule.
docfx/analyzers/VSV0003.md Documents “Use Requires.NotNull” rule.
Microsoft.VisualStudio.Validation.slnx Adds new projects/files to the solution.
Directory.Packages.props Adds Roslyn + test package version properties/entries.
Directory.Packages.Analyzers.props Introduces analyzer-specific Roslyn dependency pinning (VS-compat).
Directory.Build.targets Imports analyzer-specific package versions for analyzer projects.
Directory.Build.props Removes repo-level GitVersionBaseDirectory so analyzer version.json is honored.
.github/renovate.json Disables Renovate updates for Directory.Packages.Analyzers.props.

AArnott and others added 2 commits April 25, 2026 08:54
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 25, 2026 15:17
Copy link
Copy Markdown

Copilot AI left a comment

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 32 out of 32 changed files in this pull request and generated 2 comments.

AArnott and others added 2 commits April 25, 2026 09:34
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 25, 2026 16:00
Copy link
Copy Markdown

Copilot AI left a comment

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 32 out of 32 changed files in this pull request and generated 5 comments.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@AArnott AArnott enabled auto-merge (squash) April 25, 2026 16:21
@drewnoakes
Copy link
Copy Markdown
Member

A nice analyzer and codefix would be to remove the redundant argument name argument:

Requires.NotNull(value, "value");
Requires.NotNull(value, nameof(value));

// ...become...

Requires.NotNull(value);

AArnott and others added 2 commits April 27, 2026 11:20
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 27, 2026 17:25
@AArnott
Copy link
Copy Markdown
Member Author

AArnott commented Apr 27, 2026

Thanks @drewnoakes - I took this on in e1c46ad. I added a VSV0004 analyzer and code fix for the safe redundant cases, so Requires.NotNull(value, "value") and Requires.NotNull(value, nameof(value)) now simplify to Requires.NotNull(value), while cases like escaped identifiers stay untouched where dropping the argument would change the captured caller-expression text.

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

Copilot AI left a comment

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 32 out of 32 changed files in this pull request and generated 4 comments.

Comment thread src/Microsoft.VisualStudio.Validation.Analyzers.CodeFixes/README.md
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 27, 2026 18:04
Copy link
Copy Markdown

Copilot AI left a comment

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 32 out of 32 changed files in this pull request and generated 4 comments.

Comment thread src/Microsoft.VisualStudio.Validation.Analyzers.CodeFixes/README.md
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@AArnott AArnott disabled auto-merge April 27, 2026 18:50
@AArnott AArnott enabled auto-merge (squash) April 27, 2026 18:50
@AArnott AArnott merged commit b88d1c1 into main Apr 27, 2026
9 checks passed
@AArnott AArnott deleted the aarnott/add-requires-analyzer-codefix branch April 27, 2026 18:55
@AArnott AArnott linked an issue Apr 27, 2026 that may be closed by this pull request
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.

Add analyzer and code fix provider for Requires.NotNull/Range

5 participants