Powershell: Fix CA2014: Stackalloc in loops

Created on 27 Aug 2020  路  6Comments  路  Source: PowerShell/PowerShell

Most helpful comment

For 1 case. StackAlloc is used only (1) ~for large output~, (2) if new line is needed, (3) once on last iteration - so we could add comment that it is safe to suppress.

For 2 case. We can move StackAlloc out the cycle.

For 3 case. We can remove the cycle and replace break-s with return-s.

All 6 comments

Only first case has real cycle.

We should work to factor out the loops that are not loops in these cases as well. For example, the do { } while (false); examples sorely need to be turned into methods

Both first and second are real loops, no?

For 1 case. StackAlloc is used only (1) ~for large output~, (2) if new line is needed, (3) once on last iteration - so we could add comment that it is safe to suppress.

For 2 case. We can move StackAlloc out the cycle.

For 3 case. We can remove the cycle and replace break-s with return-s.

It strikes me that in case 1 we just need a buffer -- we could allocate outside of the loop and simply reuse the buffer space within the loop. I imagine that's the intent of the warning anyway.

We could put all after line 2570 after the while cycle - it output a tail once.

Was this page helpful?
0 / 5 - 0 ratings