Add analyzer+codefix for applying RequiresUnsafe to pointer methods#125196
Merged
Add analyzer+codefix for applying RequiresUnsafe to pointer methods#125196
Conversation
Add a new DiagnosticAnalyzer (UnsafeMethodMissingRequiresUnsafeAnalyzer) that warns when a method, constructor, or local function has the 'unsafe' modifier but is not annotated with [RequiresUnsafe]. Add a matching CodeFixProvider that adds the [RequiresUnsafe] attribute to the flagged declaration. Both are #if DEBUG guarded and enabled via the existing EnableUnsafeAnalyzer MSBuild property. New diagnostic: IL5004 (UnsafeMethodMissingRequiresUnsafe) Co-authored-by: Copilot <[email protected]>
Copilot
AI
changed the title
[WIP] Add exact contents from PR 125195
Add analyzer+codefix for unsafe methods missing [RequiresUnsafe]
Mar 4, 2026
Now based on methods with pointer types, rather than methods with .
…r-content-125195 # Conflicts: # src/libraries/Common/src/Interop/Unix/System.Native/Interop.Futex.cs # src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelFutex.Unix.cs
- Remove lambda/anonymous method break in RequiresUnsafeAnalyzer so unsafe context flows through nested lambdas (matching C# semantics) - Add FieldDeclarationSyntax to IsInRequiresScope for unsafe field initializers - Remove [RequiresUnsafe] from files compiled outside CoreLib (Common/, nativeaot/Runtime.Base/, Resources/) where the attribute is unavailable - Add tests for lambda-in-unsafe-method, anonymous delegate, and field initializer scenarios Co-authored-by: Copilot <[email protected]>
Use the DiagnosticSeverity.Info overload so IL5004 shows as a suggestion rather than a warning/error in builds. Update tests to expect Info severity. Co-authored-by: Copilot <[email protected]>
c08c9a1 to
a6490f4
Compare
…r-content-125195 # Conflicts: # src/coreclr/System.Private.CoreLib/src/System/AppContext.CoreCLR.cs # src/coreclr/System.Private.CoreLib/src/System/StartupHookProvider.CoreCLR.cs
jkotas
reviewed
Mar 6, 2026
Co-authored-by: agocke <[email protected]>
jkotas
reviewed
Mar 6, 2026
src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/Unsafe.cs
Outdated
Show resolved
Hide resolved
jkotas
reviewed
Mar 6, 2026
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeMemory.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: jkotas <[email protected]>
jkotas
reviewed
Mar 14, 2026
src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/StackFrameIterator.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: jkotas <[email protected]>
This was referenced Mar 14, 2026
Open
Member
|
The annotations look fine to me. |
This was referenced Mar 17, 2026
Member
|
@jkotas If you approve this change I'll check in the analyzer/codefixer and use them on the other libraries (after we make RequiresUnsafe match the API review approved this morning) |
jkotas
approved these changes
Mar 17, 2026
Member
|
(I have not reviewed the analyzer and fixer in detail.) |
EgorBo
reviewed
Mar 18, 2026
src/tools/illink/src/ILLink.CodeFix/UnsafeMethodMissingRequiresUnsafeCodeFixProvider.cs
Show resolved
Hide resolved
EgorBo
reviewed
Mar 18, 2026
src/tools/illink/src/ILLink.RoslynAnalyzer/UnsafeMethodMissingRequiresUnsafeAnalyzer.cs
Show resolved
Hide resolved
EgorBo
approved these changes
Mar 18, 2026
Member
|
Merging. Note: analyzer and fixer are currently non-shipping so skipping them is not a problem. |
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.
Description
Cherry-pick of PR #125195 (agocke/runtime). Adds a Roslyn analyzer and code fixer that warns on methods with the
unsafemodifier but missing[RequiresUnsafe], and applies[RequiresUnsafe]to allunsafemethods in System.Private.CoreLib.UnsafeMethodMissingRequiresUnsafeAnalyzer): ReportsIL2900onunsafemethods lacking[RequiresUnsafe]UnsafeMethodMissingRequiresUnsafeCodeFixProvider): Adds[RequiresUnsafe]attribute automatically[RequiresUnsafe]MdImport.cs— kept currentLibraryImport/partialsignature (diverged from fork'sMethodImpl/extern), added[RequiresUnsafe]Sve.PlatformNotSupported.cs— resolved conflicts from SVE method signature changes (Vector<int>/Vector<uint>→Vector<long>/Vector<ulong>), ensured newunsafepointer methods have[RequiresUnsafe][RequiresUnsafe]fromIntPtr.csandUIntPtr.cs— pointers in those files do not implyRequiresUnsafe[RequiresUnsafe]fromAdd<T>(void*, int)andSubtract<T>(void*, int)inUnsafe.cs— these methods do not dereference the pointer[RequiresUnsafe]fromAllocandAllocZeroedinNativeMemory.csandNativeMemory.Unix.cs— returning a pointer is not inherently unsafe, only dereferencing it would beusing System.Diagnostics.CodeAnalysis;additions without any[RequiresUnsafe]usage (leftover from code fixer iterations), and restored UTF-8 BOMs in 20 files that were inadvertently stripped by the fork's editor[RequiresUnsafe]onAdd,Subtract,AddByteOffset, andSubtractByteOffsetmethods returningref TinUnsafe.cswas reverted to keep this PR focused on pointer-based methods; non-pointerRequiresUnsafechanges will be grouped into a separate PR🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.