Skip to content
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

Rule request: Avoid using the Assignment by Addition Operator (+=) to build a string #1935

Open
iRon7 opened this issue Aug 29, 2023 · 1 comment

Comments

@iRon7
Copy link

iRon7 commented Aug 29, 2023

Summary of the new feature

Strings are immutable. Each addition to the string actually creates a new string big enough to hold the contents of both the left and right operands, then copies the elements of both operands into the new string. For small strings, this overhead may not matter. For large strings, this can affect performance and memory consumption.
This will create more awareness of the possible performance impact of using the Assignment by Addition Operator (+= ), see also:

Proposed technical implementation details (optional)

See (PowerShell based) prototype AvoidPlusEqualsToBuildStrings at: https://github.com/iRon7/PSRules

What is the latest version of PSScriptAnalyzer at the point of writing

1.21.0

@bergmeister
Copy link
Collaborator

I've tried this a while ago, see #933
I'd say currently not possible because we cannot infer types and there are limitations of PowerShell internal classes that were forked in early days and even if we were to update them (which is very difficult as well because of the way the PowerShell code has moved on), it would likely not solve the problem either...
I'd say to close as dupe of #806 and #904

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants