Add RequiresUnsafe trimming analyzer capabilities#123588
Merged
agocke merged 5 commits intodotnet:mainfrom Feb 15, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a debug-only “RequiresUnsafe” analyzer path (plus code fix + tests) to help annotate the framework, gated behind an MSBuild property and excluded from shipping builds.
Changes:
- Extend the Roslyn analyzer test harness to optionally compile test inputs with
allowUnsafe. - Add a debug-only
RequiresUnsafeAnalyzerand a debug-only code fix provider. - Add diagnostics/resources for the new RequiresUnsafe warnings and introduce a new test suite for them (DEBUG-only).
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/Verifiers/CSharpAnalyzerVerifier`1.cs | Adds an overload to pass allowUnsafe through the verifier into compilation creation. |
| src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/TestCaseCompilation.cs | Adds allowUnsafe support to the Roslyn compilation options and refactors supported analyzer list creation. |
| src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/RequiresUnsafeAnalyzerTests.cs | New DEBUG-only tests validating RequiresUnsafe diagnostics and suppression behavior in unsafe { } blocks. |
| src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/RequiresUnreferencedCodeAnalyzerTests.cs | Removes an unused diagnostic descriptor field. |
| src/tools/illink/src/ILLink.Shared/SharedStrings.resx | Adds title/message resources for the new RequiresUnsafe diagnostics. |
| src/tools/illink/src/ILLink.Shared/DiagnosticId.cs | Introduces DEBUG-only diagnostic IDs for RequiresUnsafe and adjusts AOT subcategory mapping. |
| src/tools/illink/src/ILLink.RoslynAnalyzer/RequiresUnsafeAnalyzer.cs | New DEBUG-only analyzer implementation, enabled via an MSBuild property, with special “unsafe block” suppression. |
| src/tools/illink/src/ILLink.RoslynAnalyzer/RequiresAnalyzerBase.cs | Refactors scope suppression check to be overridable by derived analyzers. |
| src/tools/illink/src/ILLink.RoslynAnalyzer/MSBuildPropertyOptionNames.cs | Adds a DEBUG-only MSBuild property name for enabling the new analyzer. |
| src/tools/illink/src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs | Includes RequiresUnsafeAnalyzer in the “requires analyzers” set in DEBUG builds. |
| src/tools/illink/src/ILLink.CodeFix/Resources.resx | Adds a localized title for the RequiresUnsafe code fix. |
| src/tools/illink/src/ILLink.CodeFix/RequiresUnsafeCodeFixProvider.cs | New DEBUG-only code fix provider: add RequiresUnsafe attribute and optionally wrap a statement in an unsafe block. |
src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/TestCaseCompilation.cs
Show resolved
Hide resolved
src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/Verifiers/CSharpAnalyzerVerifier`1.cs
Show resolved
Hide resolved
src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/RequiresUnsafeAnalyzerTests.cs
Show resolved
Hide resolved
This was referenced Jan 25, 2026
Open
sbomer
reviewed
Jan 26, 2026
Member
sbomer
left a comment
There was a problem hiding this comment.
This needs some tests for the code fixer too I think.
This was referenced Jan 28, 2026
Member
Author
|
Moving to draft as I clean this up |
src/coreclr/System.Private.CoreLib/CompatibilitySuppressions.xml
Outdated
Show resolved
Hide resolved
src/tools/illink/src/ILLink.CodeFix/RequiresUnsafeCodeFixProvider.cs
Outdated
Show resolved
Hide resolved
This is a debug-only mode that we can use for annotating the framework. No intention of shipping this implementation.
f678fa1 to
7f7bdca
Compare
src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/RequiresUnsafeCodeFixTests.cs
Show resolved
Hide resolved
Member
Author
|
@sbomer OK, I think this is ready again. The code fixer code is kind of a mess, but it's not totally wrong, and it's working enough for local prototyping. We don't plan to ship it, so it seems fine. |
src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/TestCaseCompilation.cs
Show resolved
Hide resolved
...raries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/RequiresUnsafeAttribute.cs
Show resolved
Hide resolved
sbomer
approved these changes
Feb 4, 2026
src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/RequiresUnsafeAnalyzerTests.cs
Show resolved
Hide resolved
...raries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/RequiresUnsafeAttribute.cs
Show resolved
Hide resolved
jkotas
reviewed
Feb 6, 2026
src/coreclr/System.Private.CoreLib/CompatibilitySuppressions.xml
Outdated
Show resolved
Hide resolved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a debug-only mode that we can use for annotating the framework. No intention of shipping this implementation.