Add Requires analyzer and code fix support#357
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Codecov Report❌ Patch coverage is 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
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
Requiresguard 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. |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
A nice analyzer and codefix would be to remove the redundant argument name argument: |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Thanks @drewnoakes - I took this on in e1c46ad. I added a VSV0004 analyzer and code fix for the safe redundant cases, so |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Why
This repo did not have any Roslyn analyzer or code-fix infrastructure yet, which meant users of
Microsoft.VisualStudio.Validationgot no IDE help to add the preferredRequiresguards 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:
VSV0001suggestsRequires.NotNull(value);VSV0002suggestsRequires.Range(value >= 0, nameof(value));VSV0003replaces supported manual null-check-and-throw patterns withRequires.NotNull(value);VSV0004removes redundant explicit parameter-name arguments fromRequires.NotNull(value, parameterName);Non-obvious details
Directory.Packages.Analyzers.props, and Renovate is configured not to update that file automaticallyGitVersionBaseDirectorysetting was removed so project-localversion.jsonfiles in the analyzer projects take effectanalyzers/dotnet/cs/Validation