Repository navigation
Fix PInvoke017 for span-based string copies in using scopes - #1875
Merged
Jevan Saks (jevansaks) merged 2 commits intoNov 9, 2026
Merged
Conversation
Co-authored-by: Copilot App <[email protected]>
Co-authored-by: Copilot App <[email protected]>
Jevan Saks (jevansaks)
marked this pull request as ready for review
November 6, 2026 19:56
Sergio Pedri (Sergio0694)
approved these changes
Nov 8, 2026
Jevan Saks (jevansaks)
deleted the
jevansaks-dangerous-value-using-guard
branch
November 9, 2026 18:08
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.
The lifetime analyzer reports
PInvoke017for this valid pattern:The owner's
usingscope is already recognized. The false positive comes from stopping atAsSpan()instead of following the expression toToString(), which copies the native contents into a managed string.Follow instance calls that return
Span<T>orReadOnlySpan<T>so chained copies, includingSlice(...).ToString(), are evaluated at their final use. The same behavior applies to a lease'sValue. Saving or returning a native-backed span still reportsPInvoke017, and accessing the resource without a visible lifetime scope still reportsPInvoke016.Adds regression coverage for the reported pattern, owners and leases, mutable and read-only spans, and rejected lifetime escapes on .NET Framework 4.7.2 and .NET 8.