-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Analyzer: Adjust code for numeric IntPtr
.
#72348
Comments
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsWith the advert of the numeric IntPtr feature,
These analyzers and fixers should be triggered only if the underlying runtime supports numeric
|
@tannergooding Do the details of this proposal match your expectations for the analyzer you had in mind? |
Yes, this looks to capture the most relevant details. |
It would be nice for the code-fixer to suggest replacing Blank replacing |
We're planning on getting an analyzer out that flags the known behavioral changes for 7.0, code fixers will be a future consideration (post 7.0). |
I thought we had a separate issue tracking the analyzer itself, but we don't. Leaving a comment here to capture what the analyzer work is separate from the fixer as proposed in the description. From there, we can spawn off issue(s) as needed.
|
Looks only the 2nd check box is pending (Code simplification opportunities now available that could be suggested (but these are not changes in behavior to existing usage)) for which an issue was created #74518, so closing this issue |
With the advert of the numeric IntPtr feature,
nint
is an alias forSystem.IntPtr
just likeint
is an alias forSystem.Int32
. We should add analyzers and fixers to make it clear to user code, especially to those who think thatIntPtr
represents an opaque pointer-sized value. Here's what the analyzers should do:IDE0049
should be updated to suggest convertingIntPtr
tonint
andUIntPtr
tonuint
.U?IntPtr.Zero
➡0
or(nu?int)0
if it would introduce ambiguitynew U?IntPtr(x)
➡x
or(nu?int)x
if it would introduce ambiguity, orunchecked((nu?int)x)
if we are in achecked
context.x.ToU?Int32()
➡checked((u?int)x)
or(u?int)x
if we are in achecked
contextx.ToU?Int64()
➡unchecked((u?long)x)
or(u?long)x
if we are in anunchecked
contextx.ToPointer()
➡(void*) x
U?IntPtr
andu?int
oru?long
, tonu?int
, while taking the overflow checking context into account (too many to list).These analyzers and fixers should be triggered only if the underlying runtime supports numeric
IntPtr
. Some of them might be legal even without it but it's not necessary to make it more complicated.The text was updated successfully, but these errors were encountered: