C# analyzer to eliminate collection expressions.
$ dotnet add package CollectionExpressionKillerEnsures Explicit Coding Style for Code Review
</div>
Collection expression is useful for small scripts, demonstrations or other non-critical use cases but it is unclear what is actually created behind the scenes.
The source code should be explicitly expressing itself and eliminating "Something working" from the repository is important especially on code review on GitHub (no IDE assist).
dotnet add package CollectionExpressionKiller
.editorconfig RecommendationDisable the suggestions relating to collection expressions.
dotnet_diagnostic.IDE0300.severity = silent # Use collection expression for array
dotnet_diagnostic.IDE0301.severity = silent # Use collection expression for empty
dotnet_diagnostic.IDE0305.severity = silent # Use collection expression for fluent
dotnet_diagnostic.IDE0028.severity = silent # Use collection initializers
Ability to disallow all expressions or complicated only.
CEK001: Disallow all collection expressions.CEK002: Disallow expressions with 4 or more elements. (e.g., [1, 2, ..other, 4])CEK003: Disallow expressions whose string representation is longer than 12 characters (including [ and ]).CEK004: Disallow multiline expressions.CEK005: Disallow collection expressions with elements. (i.e., Only [] is allowed)Example usage:
values = [1234, 56789, .. values]; // Error: More than 12 chars
// To allow expression which is 3 or less elements in single line
#pragma warning disable CEK001
#pragma warning disable CEK003
#pragma warning disable CEK005
values = [1234, 56789, ..values]; // OK: Simple enough
values = [1234, 56789, ..values, ..other]; // Error: 4 elements.editorconfig only allowing [] in entire project:
dotnet_diagnostic.CEK001.severity = none # Collection expressions are disallowed
dotnet_diagnostic.CEK002.severity = none # Collection expressions with more than 3 elements are disallowed
dotnet_diagnostic.CEK003.severity = none # Long collection expression text is disallowed
dotnet_diagnostic.CEK004.severity = none # Multiline collection expressions are disallowed
dotnet_diagnostic.CEK005.severity = error # Collection expressions with elements are disallowedDisable for entire project by category with .editorconfig:
dotnet_analyzer_diagnostic.category-CollectionExprKiller.severity = noneDisable for entire project with .editorconfig:
dotnet_diagnostic.CEK001.severity = none
dotnet_diagnostic.CEK002.severity = none
dotnet_diagnostic.CEK003.severity = none
dotnet_diagnostic.CEK004.severity = none
dotnet_diagnostic.CEK005.severity = noneDisable for entire assembly:
using System.Diagnostics.CodeAnalysis;
[assembly: SuppressMessage(
"CollectionExprKiller",
"CEK001:Collection expressions are not allowed",
Justification = "Approved for this assembly")]
[assembly: SuppressMessage(
"CollectionExprKiller",
"CEK002:Collection expressions must have fewer than 4 elements",
Justification = "Approved for this assembly")]
[assembly: SuppressMessage(
"CollectionExprKiller",
"CEK003:Collection expression text length must be 12 or fewer characters",
Justification = "Approved for this assembly")]
[assembly: SuppressMessage(
"CollectionExprKiller",
"CEK004:Collection expressions must be on a single line",
Justification = "Approved for this assembly")]
[assembly: SuppressMessage(
"CollectionExprKiller",
"CEK005:Collection expressions must be empty",
Justification = "Approved for this assembly")]Disable for entire .cs file with #pragma:
#pragma warning disable CEK001
#pragma warning disable CEK002
#pragma warning disable CEK003
#pragma warning disable CEK004
#pragma warning disable CEK005
int[] values = [1, 2, 3, 4, ..otherCollection];