You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Avoid implicitly allocating params arrays in loops #33793
Find calls to System.* methods inside loops, where those methods take params arrays and those params arrays are being allocated, and hoist the allocation to before the loop if possible.
We need to check that the array values do not get modified between iterations.
If the analyzer flags a case, the fixer should create the array immediately before the beginning of the loop, using the data type of the method's params array parameter.
The new array should be named after the method parameter's name, unless a variable with that name exist, then append a number.
Restrict the analyzer to only check invocations of methods that live inside System.*.
CS0231: params is always the last parameter in a method signature, so the analyzer should only check if the last parameter is a params array.
Activity
jeffhandley commentedon Mar 21, 2020
Estimates:
carlossanlop commentedon Jan 8, 2021
@terrajobst can you please provide a code example?
carlossanlop commentedon Mar 30, 2021
Examples
Let's use the
Console.WriteLineoverload that takes aparamsarray:Case 1
Flag
Before:
After:
Case 2
Do not flag: An element in the passed params array has variable data
Case 3
Flag
Before: Variables are used inside the loop, but they weren't modified between their creation and their use:
After:
Case 4
Having this class:
Flag
Before:
After: Notice the data type of the array can be preserved as
object[]Case 5
Do not flag: An element in the passed params array has variable data
Case 6
Do not flag: Method does not belong to
System.*Things to consider:
System.*.paramsis always the last parameter in a method signature, so the analyzer should only check if the last parameter is a params array.2 remaining items
NewellClark commentedon Sep 19, 2021
I would like to be assigned to this, please.
Youssef1313 commentedon Jan 7, 2022
I think this is ALREADY implemented (except the "in loop" part and the attribute requirement).
https://github.com/dotnet/roslyn-analyzers/blob/a46689a39bd6bbea9761961b4683bb85c84e3be8/src/PerformanceSensitiveAnalyzers/Microsoft.CodeAnalysis.PerformanceSensitiveAnalyzers.sarif#L12-L24
An implementation of this as a "CAxxxx" can benefit from the existing implementation.
huoyaoyuan commentedon Apr 27, 2024
With
params ReadOnlySpan<T>coming, is this still valuable?