-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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/simplify code for numeric IntPtr #74518
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @dotnet/area-meta Issue DetailsRelated to #72348 (comment) With numeric IntPtr feature code simplification opportunities now available that could be suggested by this analyzer
These analyzer should be triggered only if the underlying runtime supports numeric IntPtr feature, i.e. if Suggested severity : "Info" Examples: class Example
{
int intValue;
uint uintValue;
void Test ()
{
// Examples to flag:
IntPtr intPtr1 = IntPtr.Zero;
UIntPtr uintPtr1 = UIntPtr.Zero;
// Violations fixed
intPtr1 = 0;
uintPtr1 = 0;
// flag:
intPtr1 = new IntPtr(intValue);
uintPtr1 = new UIntPtr(uintValue);
// fixed
intPtr1 = intValue;
uintPtr1 = uintValue;
// flag:
intValue = intPtr1.ToInt32();
uintValue = uintPtr1.ToUInt32();
// fixed
intValue = (int)intPtr1;
uintValue = (uint)uintPtr1;
// flag:
long longValue = intPtr1.ToInt64();
ulong uLongValue = uintPtr1.ToUInt64();
// fixed
longValue = intPtr1;
uLongValue = uintPtr1;
// flag:
void* ptr1 = intPtr1.ToPointer();
void* ptr2 = uintPtr1.ToPointer();
// fixed
ptr1 = (void*)intPtr1;
ptr2 = (void*)uintPtr1;
}
} CC @tannergooding @jeffhandley
|
It would be great to have in .Net 8! |
We did an unofficial (lacking quorum) review of this. Some notes:
|
Sounds good, updated the description accordingly
The operations coming from
Right, updated conversions to [u]long, |
Looks good as proposed. The only representative case that's not explicitly mentioned is |
Related to #72348
With numeric IntPtr feature code simplification opportunities now available that could be suggested by this analyzer
U?IntPtr.Zero
➡0
ornu?int.Zero
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()
➡(u?long)x
x.ToPointer()
➡(void*) x
Suggested severity : "Info"
Suggested category : "Style" or "Usage"
This analyzer should be triggered only if the underlying runtime supports numeric IntPtr feature, i.e. if
System.Runtime.CompilerServices.RuntimeFeature.NumericIntPtr
is available in corelib.Other related questions:
U?IntPt
, the type also could be converted tonu?int
which also could be covered by IDE0049 should be updated to suggest converting IntPtr to nint and UIntPtr to nuint, so these scenarios might also could be added to that issue to fix all references with appropriate values.it would be nice for the code-fixer to suggest replacing IntPtr with actual pointer types where the code is actually operating on pointers.
It might be not easy to determine if actual pointer should be suggested instead of nu?intCC @tannergooding @jeffhandley
The text was updated successfully, but these errors were encountered: