Skip to content

Fix IDE0049 in System.Management.Automation. Part 5#25900

Open
xtqqczze wants to merge 1 commit into
PowerShell:masterfrom
xtqqczze:IDE0049-sma
Open

Fix IDE0049 in System.Management.Automation. Part 5#25900
xtqqczze wants to merge 1 commit into
PowerShell:masterfrom
xtqqczze:IDE0049-sma

Conversation

@xtqqczze

@xtqqczze xtqqczze commented Aug 24, 2025

Copy link
Copy Markdown
Contributor

Use language keywords in System.Management.Automation

Exclude nint for pointers/handles.

https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0049

Contributes to: #25922.

@iSazonov

Copy link
Copy Markdown
Collaborator

Too many changed files. Could you please split the PR on some ones with similar changes (String -> string)?

@xtqqczze xtqqczze marked this pull request as draft August 26, 2025 23:51
@xtqqczze xtqqczze changed the title Fix IDE0049 in System.Management.Automation WIP: Fix IDE0049 in System.Management.Automation Aug 27, 2025
@microsoft-github-policy-service microsoft-github-policy-service Bot added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Aug 30, 2025
@microsoft-github-policy-service microsoft-github-policy-service Bot added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept labels Sep 8, 2025
@microsoft-github-policy-service microsoft-github-policy-service Bot added Stale Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept Stale labels Sep 27, 2025
@microsoft-github-policy-service microsoft-github-policy-service Bot added Stale and removed Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept Stale labels Oct 16, 2025
@xtqqczze xtqqczze changed the title WIP: Fix IDE0049 in System.Management.Automation Fix IDE0049 in System.Management.Automation Oct 17, 2025
@xtqqczze xtqqczze marked this pull request as ready for review October 17, 2025 10:54
@iSazonov iSazonov added the CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log label Oct 17, 2025
@microsoft-github-policy-service microsoft-github-policy-service Bot added the Review - Needed The PR is being reviewed label Oct 25, 2025
@microsoft-github-policy-service

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

Use language keywords in `System.Management.Automation`

Exclude `nint` for pointers/handles.

https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0049
Copilot AI review requested due to automatic review settings April 29, 2026 13:49
@xtqqczze xtqqczze requested a review from a team as a code owner April 29, 2026 13:49
@xtqqczze xtqqczze changed the title Fix IDE0049 in System.Management.Automation Fix IDE0049 in System.Management.Automation. Part 5 Apr 29, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.MaxValueint.MaxValue, Int32.TryParseint.TryParse).
  • Updated select XML documentation cref references 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.

Comment on lines 229 to 232
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--;

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines 229 to 232
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--;

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copilot uses AI. Check for mistakes.
else
{
i64 += (UInt64)(-upper);
i64 += (ulong)(-upper);

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
i64 += (ulong)(-upper);
i64 += (ulong)(-lower);

Copilot uses AI. Check for mistakes.
Comment on lines 96 to 99
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--;

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
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);

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
frame.Data[frame.StackIndex - 2] = (ulong)unchecked((short)l + (short)r);
frame.Data[frame.StackIndex - 2] = unchecked((ulong)l + (ulong)r);

Copilot uses AI. Check for mistakes.
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);

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
frame.Data[frame.StackIndex - 2] = (ulong)checked((short)l + (short)r);
frame.Data[frame.StackIndex - 2] = checked((ulong)l + (ulong)r);

Copilot uses AI. Check for mistakes.
Comment on lines 96 to 99
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--;

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
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);

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
frame.Data[frame.StackIndex - 2] = (ulong)((short)l / (short)r);
frame.Data[frame.StackIndex - 2] = (ulong)((ulong)l / (ulong)r);

Copilot uses AI. Check for mistakes.
Comment on lines +353 to 354
string strMicrosec = microsec.ToString((IFormatProvider)CultureInfo.InvariantCulture.GetFormat(typeof(long)));
if (strMicrosec.Length > 6)

Copilot AI Apr 29, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log Review - Needed The PR is being reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants