Repository navigation
Open
Labels
api-approvedAPI was approved in API review, it can be implementedarea-Metacode-analyzerMarks an issue that suggests a Roslyn analyzercode-fixerMarks an issue that suggests a Roslyn code fixerhelp wanted[up-for-grabs] Good issue for external contributors
Milestone
Description
Related to #72348
With numeric IntPtr feature code simplification opportunities now available that could be suggested by this analyzer
U?IntPtr.Zero➡0ornu?int.Zeroif it would introduce ambiguity
// Flag:
IntPtr intPtr = IntPtr.Zero; // or nint intPtr1 = IntPtr.Zero;
UIntPtr uintPtr1 = UIntPtr.Zero; // nuint uintPtr1 = UIntPtr.Zero;
// Violations fixed
nint intPtr1 = 0; // or void* intPtr1 = null; if it needs to be pointer
nuint uintPtr1 = 0;new U?IntPtr(x)➡xor(nu?int)xif it would introduce ambiguity, orunchecked((nu?int)x)if we are in acheckedcontext.
// Flag:
IntPtr intPtr1 = new IntPtr(x);
uintPtr1 = new UIntPtr(x);
// Fixed
nint intPtr1 = x;
uintPtr1 = (nuint)x;x.ToU?Int32()➡checked((u?int)x)or(u?int)xif we are in acheckedcontext
// Flag:
int i = intPtr1.ToInt32();
uint ui = uintPtr1.ToUInt32();
// Fixed
int i = (int)intPtr1;
uint ui = (uint)uintPtr1;x.ToU?Int64()➡(u?long)x
// Flag:
long longValue = intPtr1.ToInt64();
ulong uLongValue = uintPtr1.ToUInt64();
// Fixed
long longValue = (long)intPtr1;
ulong uLongValue = (ulong)uintPtr1;x.ToPointer()➡(void*) x
// Flag:
void* ptr1 = intPtr1.ToPointer();
void* ptr2 = uintPtr1.ToPointer();
// Fixed
void* ptr1 = (void*)intPtr1;
void* ptr2 = (void*)uintPtr1; 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:
- In case the statement includes a variable declaration with
U?IntPt, the type also could be converted tonu?intwhich 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. - Also, as @jkotas suggested
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?int
Reacted by Paulus Pärssinen
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedarea-Metacode-analyzerMarks an issue that suggests a Roslyn analyzercode-fixerMarks an issue that suggests a Roslyn code fixerhelp wanted[up-for-grabs] Good issue for external contributors
Activity
iSazonov commentedon Dec 2, 2022
It would be great to have in .Net 8!
GrabYourPitchforks commentedon Dec 6, 2022
We did an unofficial (lacking quorum) review of this. Some notes:
nint.Zeroinstead of(nint)0to avoid ambiguity at call sites where we can't use simply0?checked(...)statement, regardless of what the fixer believes the current context is.void*and[u]long, no need to worry about checked / unchecked context. These conversions are guaranteed lossless so will never fail.buyaa-n commentedon Jan 4, 2023
Sounds good, updated the description accordingly
The operations coming from
roslynprovideCheckedproperty we could use for determining checked/unchecked context , I am not sure that covers the scenario you mentioned, most likely it is. Anyway, instead of trying to check the context and offer an appropriate fixer or always offering explicitchecked(...)we could just offer the 2 options both:checked((u?int)x)and(u?int)x, and let the user choose.Right, updated conversions to [u]long,
bartonjs commentedon Feb 28, 2023
Looks good as proposed. The only representative case that's not explicitly mentioned is
var nint = IntPtr.Zero, which needs special handling because of var.Manish-1kumar commentedon Nov 25, 2026
@buyaa-n @jeffhandley I'd like to implement this analyzer and fixer in
dotnet/sdk(Microsoft.CodeAnalysis.NetAnalyzers) if a PR is welcome.One scope note: Roslyn's IDE0049 already rewrites the type names
IntPtr/UIntPtrtonint/nuinton C# 11 with numeric IntPtr support, so I'd leave type names to IDE0049 and cover the member patterns from the approved list:U?IntPtr.Zero→0(ornu?int.Zerowhere0would be ambiguous, including thevarcase from the review)new U?IntPtr(x)ToU?Int32()/ToU?Int64(), respecting checked/unchecked contextToPointer()It would only fire when numeric IntPtr is available. I'd use the suggested severity Info; for category, the description suggests "Style" or "Usage". Which do you prefer?