Fix IDE0049 in System.Management.Automation. Part 5#25900
Conversation
|
Too many changed files. Could you please split the PR on some ones with similar changes (String -> string)? |
System.Management.AutomationSystem.Management.Automation
System.Management.AutomationSystem.Management.Automation
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
Use language keywords in `System.Management.Automation` Exclude `nint` for pointers/handles. https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0049
System.Management.AutomationIDE0049 in System.Management.Automation. Part 5
There was a problem hiding this comment.
Pull request overview
This PR addresses .NET analyzer rule IDE0049 across System.Management.Automation by replacing framework primitive type names (e.g., Int32, UInt64) with C# built-in keywords (e.g., int, ulong) and corresponding keyword constants (e.g., int.MaxValue), contributing to #25922.
Changes:
- Replaced primitive type references (
Int16/Int32/Int64/UInt*,Single,Decimal,Boolean) with C# keyword equivalents (short/int/long/ushort/uint/ulong,float,decimal,bool) across engine, interpreter, host, and utility code. - Updated uses of primitive constants and parsing helpers (e.g.,
Int32.MaxValue→int.MaxValue,Int32.TryParse→int.TryParse). - Updated select XML documentation
crefreferences to use keyword-based signatures.
Reviewed changes
Copilot reviewed 49 out of 49 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| src/System.Management.Automation/utils/tracing/PSEtwLog.cs | Use keyword primitives in ETW logging method signature (long, uint). |
| src/System.Management.Automation/utils/ParameterBinderExceptions.cs | Use long fields/properties for line/offset tracking. |
| src/System.Management.Automation/utils/ObjectStream.cs | Replace Int32.MaxValue usage with int.MaxValue for capacity/read semantics. |
| src/System.Management.Automation/utils/ObjectReader.cs | Replace Int32.MaxValue with int.MaxValue in non-blocking reads. |
| src/System.Management.Automation/utils/ClrFacade.cs | Use long keyword (and typeof(long)) in DMTF datetime conversion. |
| src/System.Management.Automation/security/SecuritySupport.cs | Use long keyword for intermediate DWORD conversion value. |
| src/System.Management.Automation/namespaces/RegistryWrapper.cs | Use uint/ulong keywords for registry numeric conversion temporaries. |
| src/System.Management.Automation/namespaces/FileSystemProvider.cs | Use ulong tuple types for inode tracking. |
| src/System.Management.Automation/engine/serialization.cs | Replace primitive framework types with keywords in serializer/deserializer and type tables. |
| src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs | Use int.TryParse keyword form. |
| src/System.Management.Automation/engine/lang/parserutils.cs | Use int.TryParse keyword form. |
| src/System.Management.Automation/engine/interpreter/Utilities.cs | Replace default(Int16) etc. with keyword defaults (default(short), etc.). |
| src/System.Management.Automation/engine/interpreter/SubInstruction.cs | Replace primitive casts with keyword casts in subtraction instructions. |
| src/System.Management.Automation/engine/interpreter/NumericConvertInstruction.cs | Replace primitive casts/type names with keyword equivalents in numeric conversion logic. |
| src/System.Management.Automation/engine/interpreter/NotEqualInstruction.cs | Replace primitive casts with keyword casts in not-equals instructions. |
| src/System.Management.Automation/engine/interpreter/MulInstruction.cs | Replace primitive casts with keyword casts in multiply instructions. |
| src/System.Management.Automation/engine/interpreter/LocalVariables.cs | Replace Int32.MaxValue sentinel with int.MaxValue. |
| src/System.Management.Automation/engine/interpreter/LessThanInstruction.cs | Replace primitive locals/casts with keyword types in less-than instructions. |
| src/System.Management.Automation/engine/interpreter/Interpreter.cs | Replace Int32.MaxValue sentinel with int.MaxValue. |
| src/System.Management.Automation/engine/interpreter/GreaterThanInstruction.cs | Replace primitive locals/casts with keyword types in greater-than instructions. |
| src/System.Management.Automation/engine/interpreter/EqualInstruction.cs | Replace primitive casts with keyword casts in equals instructions. |
| src/System.Management.Automation/engine/interpreter/DivInstruction.cs | Replace primitive casts with keyword casts in division instructions. |
| src/System.Management.Automation/engine/interpreter/ControlFlowInstructions.cs | Replace Int32.MinValue sentinel with int.MinValue. |
| src/System.Management.Automation/engine/interpreter/BranchLabel.cs | Replace Int32.MinValue sentinels with int.MinValue. |
| src/System.Management.Automation/engine/interpreter/AddInstruction.cs | Replace primitive casts with keyword casts in add instructions. |
| src/System.Management.Automation/engine/hostifaces/pipelinebase.cs | Replace Int32.MaxValue with int.MaxValue for output non-blocking read. |
| src/System.Management.Automation/engine/hostifaces/PSDataCollection.cs | Replace Int32.MaxValue with int.MaxValue in resolved read count logic. |
| src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs | Update WriteProgress signature and doc cref to use long. |
| src/System.Management.Automation/engine/hostifaces/MshHostRawUserInterface.cs | Replace primitive framework types with keywords in hashing helpers. |
| src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs | Update override signature to WriteProgress(long, ...). |
| src/System.Management.Automation/engine/hostifaces/History.cs | Replace Int16.MaxValue with short.MaxValue in ValidateRange attributes. |
| src/System.Management.Automation/engine/cmdlet.cs | Update internal WriteProgress overload to long sourceId. |
| src/System.Management.Automation/engine/UserFeedbackParameters.cs | Use ulong keywords and ulong.MaxValue for paging parameters and docs. |
| src/System.Management.Automation/engine/TypeMetadata.cs | Replace Int32.MinValue check with int.MinValue. |
| src/System.Management.Automation/engine/SessionStateScopeAPIs.cs | Replace Int32.Parse with int.Parse. |
| src/System.Management.Automation/engine/PSVersionInfo.cs | Replace Int32.TryParse/Parse with int.TryParse/Parse. |
| src/System.Management.Automation/engine/MshObjectTypeDescriptor.cs | Update XML docs to refer to bool keyword. |
| src/System.Management.Automation/engine/MshCommandRuntime.cs | Replace Int64 fields/params with long; update int.MaxValue sentinels. |
| src/System.Management.Automation/engine/Modules/RemoteDiscoveryHelper.cs | Replace UInt16 usage with ushort and parsing with ushort.Parse. |
| src/System.Management.Automation/engine/LanguagePrimitives.cs | Replace primitive framework types with keywords in type tables/conversion logic. |
| src/System.Management.Automation/engine/InternalCommands.cs | Replace Int32.MaxValue with int.MaxValue in ValidateRange attributes. |
| src/System.Management.Automation/engine/ICommandRuntime.cs | Update interface method signature to WriteProgress(long, ...). |
| src/System.Management.Automation/engine/ErrorPackage.cs | Replace typeof(Int32) with typeof(int) for ArrayList.ToArray conversion. |
| src/System.Management.Automation/engine/DefaultCommandRuntime.cs | Update WriteProgress(long, ...) signature. |
| src/System.Management.Automation/engine/CommonCommandParameters.cs | Replace Int32.MaxValue with int.MaxValue in ValidateRange. |
| src/System.Management.Automation/engine/CommandCompletion/PseudoParameterBinder.cs | Replace UInt32.MaxValue with uint.MaxValue in flag comparisons. |
| src/System.Management.Automation/CoreCLR/CorePsStub.cs | Replace Int16 with short in stub signature. |
| src/Microsoft.PowerShell.ConsoleHost/host/msh/PendingProgress.cs | Replace Int32.MaxValue with int.MaxValue in progress aging. |
| src/Microsoft.PowerShell.Commands.Utility/commands/utility/UnblockFile.cs | Replace UInt32 with uint for P/Invoke return and usage. |
| object l = frame.Data[frame.StackIndex - 2]; | ||
| object r = frame.Data[frame.StackIndex - 1]; | ||
| frame.Data[frame.StackIndex - 2] = (UInt64)checked((Int16)l - (Int16)r); | ||
| frame.Data[frame.StackIndex - 2] = (ulong)checked((short)l - (short)r); | ||
| frame.StackIndex--; |
There was a problem hiding this comment.
In the checked (overflow) UInt64 subtraction instruction, operands are being cast to short before subtracting and then cast to ulong, which truncates and breaks ulong arithmetic. Use checked((ulong)l - (ulong)r) (with l/r cast to ulong) so underflow/overflow behavior matches UInt64 semantics.
| object l = frame.Data[frame.StackIndex - 2]; | ||
| object r = frame.Data[frame.StackIndex - 1]; | ||
| frame.Data[frame.StackIndex - 2] = (UInt64)checked((Int16)l * (Int16)r); | ||
| frame.Data[frame.StackIndex - 2] = (ulong)checked((short)l * (short)r); | ||
| frame.StackIndex--; |
There was a problem hiding this comment.
In the checked (overflow) UInt64 multiply instruction, operands are being cast to short before multiplying and then cast to ulong, which truncates values and breaks overflow behavior for UInt64. Use checked((ulong)l * (ulong)r) (with operands cast to ulong).
| else | ||
| { | ||
| i64 += (UInt64)(-upper); | ||
| i64 += (ulong)(-upper); |
There was a problem hiding this comment.
GetHashCode() is using upper when lower is negative (i64 += (ulong)(-upper)), which looks like a copy/paste mistake and will produce incorrect hash codes for many rectangles. This should use lower in that branch (i.e., add the absolute value of lower).
| i64 += (ulong)(-upper); | |
| i64 += (ulong)(-lower); |
| object l = frame.Data[frame.StackIndex - 2]; | ||
| object r = frame.Data[frame.StackIndex - 1]; | ||
| frame.Data[frame.StackIndex - 2] = (UInt64)unchecked((Int16)l - (Int16)r); | ||
| frame.Data[frame.StackIndex - 2] = (ulong)unchecked((short)l - (short)r); | ||
| frame.StackIndex--; |
There was a problem hiding this comment.
In the UInt64 subtraction instruction, operands are being cast to short before subtracting and then cast to ulong. This truncates any values outside Int16 range and produces incorrect results for ulong arithmetic. Cast l and r to ulong (or unchecked((ulong)l - (ulong)r)) so the operation is performed in the correct width/type.
| object l = frame.Data[frame.StackIndex - 2]; | ||
| object r = frame.Data[frame.StackIndex - 1]; | ||
| frame.Data[frame.StackIndex - 2] = (UInt64)unchecked((Int16)l + (Int16)r); | ||
| frame.Data[frame.StackIndex - 2] = (ulong)unchecked((short)l + (short)r); |
There was a problem hiding this comment.
In the UInt64 addition instruction, operands are being cast to short before adding and then cast to ulong. This truncates values and yields incorrect results for ulong arithmetic. Cast l and r to ulong so the addition runs in the correct type/width.
| frame.Data[frame.StackIndex - 2] = (ulong)unchecked((short)l + (short)r); | |
| frame.Data[frame.StackIndex - 2] = unchecked((ulong)l + (ulong)r); |
| object l = frame.Data[frame.StackIndex - 2]; | ||
| object r = frame.Data[frame.StackIndex - 1]; | ||
| frame.Data[frame.StackIndex - 2] = (UInt64)checked((Int16)l + (Int16)r); | ||
| frame.Data[frame.StackIndex - 2] = (ulong)checked((short)l + (short)r); |
There was a problem hiding this comment.
In the checked (overflow) UInt64 addition instruction, operands are being cast to short before adding and then cast to ulong, which truncates and breaks ulong arithmetic/overflow behavior. Use checked((ulong)l + (ulong)r) (casting operands to ulong) to preserve UInt64 semantics.
| frame.Data[frame.StackIndex - 2] = (ulong)checked((short)l + (short)r); | |
| frame.Data[frame.StackIndex - 2] = checked((ulong)l + (ulong)r); |
| object l = frame.Data[frame.StackIndex - 2]; | ||
| object r = frame.Data[frame.StackIndex - 1]; | ||
| frame.Data[frame.StackIndex - 2] = (UInt64)unchecked((Int16)l * (Int16)r); | ||
| frame.Data[frame.StackIndex - 2] = (ulong)unchecked((short)l * (short)r); | ||
| frame.StackIndex--; |
There was a problem hiding this comment.
In the UInt64 multiply instruction, operands are being cast to short before multiplying and then cast to ulong. This truncates values and makes ulong multiplication incorrect for most inputs. Cast l and r to ulong so multiplication uses the correct type.
| object l = frame.Data[frame.StackIndex - 2]; | ||
| object r = frame.Data[frame.StackIndex - 1]; | ||
| frame.Data[frame.StackIndex - 2] = (UInt64)((Int16)l / (Int16)r); | ||
| frame.Data[frame.StackIndex - 2] = (ulong)((short)l / (short)r); |
There was a problem hiding this comment.
In the UInt64 division instruction, operands are being cast to short before dividing and then cast to ulong. This truncates values and yields incorrect results for ulong division. Cast l and r to ulong so the division is performed with UInt64 semantics.
| frame.Data[frame.StackIndex - 2] = (ulong)((short)l / (short)r); | |
| frame.Data[frame.StackIndex - 2] = (ulong)((ulong)l / (ulong)r); |
| string strMicrosec = microsec.ToString((IFormatProvider)CultureInfo.InvariantCulture.GetFormat(typeof(long))); | ||
| if (strMicrosec.Length > 6) |
There was a problem hiding this comment.
CultureInfo.InvariantCulture.GetFormat(typeof(long)) returns null (InvariantCulture only provides formats for types like NumberFormatInfo/DateTimeFormatInfo), so this call falls back to current culture formatting. For DMTF serialization, this should use invariant formatting explicitly (e.g., microsec.ToString(CultureInfo.InvariantCulture) or InvariantCulture.NumberFormat).
Use language keywords in
System.Management.AutomationExclude
nintfor pointers/handles.https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0049
Contributes to: #25922.