Skip to content

Enable CA1825: Avoid zero-length array allocations - #13961

Merged
Ilya (iSazonov) merged 7 commits into
PowerShell:masterfrom
xtqqczze:CA1825
Nov 4, 2020
Merged

Enable CA1825: Avoid zero-length array allocations#13961
Ilya (iSazonov) merged 7 commits into
PowerShell:masterfrom
xtqqczze:CA1825

Conversation

@xtqqczze

@xtqqczze xtqqczze commented Nov 1, 2020

Copy link
Copy Markdown
Contributor

if (times == 0 || array.Length == 0)
{
return new T[0]; // don't use Utils.EmptyArray, always return a new array
return Array.Empty<T>();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ilya (@iSazonov) Perhaps we should suppress CA1825 here, if there is still a rationale for always returning a new array instance. Utils.EmptyArray was removed in your PR #9042.

@iSazonov Ilya (iSazonov) Nov 2, 2020

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yes, please revert and suppress. We could update only the comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I will revert and suppress, but I was wondering what reason we do not to use the static empty array?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't remember. Perhaps it is used in comparisons.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Paul Higinbotham (@PaulHigin) Must we always return a new instance, rather then using the static empty array?

Comment thread docs/dev-process/coding-guidelines.md
public object Data { get; set; }

internal static readonly RuntimeDefinedParameter[] EmptyParameterArray = new RuntimeDefinedParameter[0];
internal static readonly RuntimeDefinedParameter[] EmptyParameterArray = Array.Empty<RuntimeDefinedParameter>();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Perhaps we can inline EmptyParameterArray ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not in the PR.

@xtqqczze
xtqqczze marked this pull request as ready for review November 2, 2020 00:52
Comment thread src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs Outdated
@iSazonov Ilya (iSazonov) added the CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log label Nov 2, 2020
Comment thread src/System.Management.Automation/engine/runtime/Operations/ArrayOps.cs Outdated
Comment thread src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs Outdated
Comment thread src/System.Management.Automation/engine/runtime/Operations/ArrayOps.cs Outdated
Comment thread src/System.Management.Automation/engine/runtime/Operations/MiscOps.cs Outdated
@iSazonov
Ilya (iSazonov) merged commit 1d7a93c into PowerShell:master Nov 4, 2020
@iSazonov Ilya (iSazonov) added this to the 7.2.0-preview.1 milestone Nov 4, 2020
@xtqqczze
xtqqczze deleted the CA1825 branch November 5, 2020 16:59
@ghost

Copy link
Copy Markdown

🎉v7.2.0-preview.1 has been released which incorporates this pull request.:tada:

Handy links:

Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj/PowerShell that referenced this pull request Aug 6, 2026
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants