BRO1401: Use a trailing comma in multi-line initializers¶
The last item of a multi-line list should be followed by a comma. Replaces StyleCop SA1413.
Example¶
Before¶
public enum Status
{
Active,
Closed
}
public class Settings
{
public string[] Hosts { get; } = new[]
{
"alpha",
"beta"
};
}
After¶
public enum Status
{
Active,
Closed,
}
public class Settings
{
public string[] Hosts { get; } = new[]
{
"alpha",
"beta",
};
}
Why¶
With a trailing comma, adding an item at the end changes one line instead of two, and moving items around never leaves a missing comma behind.
What is reported¶
These lists, when their braces or brackets are on different lines:
- array, object and collection initializers (
new[] { ... },new Point { ... },new List<int> { ... }) - collection expressions (
[ ... ]), nested ones and a spread as the last element (.. more,) included with { ... }expressions and anonymous objects- enums and switch expressions
// Before
int[] ports =
[
80,
443
];
// After
int[] ports =
[
80,
443,
];
Configuration¶
[*.cs]
# include (default): a trailing comma after the last item of a multi-line list.
# omit: no trailing comma after the last item of any list, single-line ones included.
stylebro_trailing_comma = include
With omit the rule reports the trailing comma instead, in the same kinds of lists, and the fix removes it:
public enum Status
{
Active,
Closed, // last
}
var hosts = new[] { "alpha", "beta", };
becomes
public enum Status
{
Active,
Closed // last
}
var hosts = new[] { "alpha", "beta" };
Unlike include, omit checks single-line lists too (like Roslynator's RCS1260 with omit): a comma there never
saves a diff line. Spaces before the removed comma go with it, and a trailing comment keeps its place. Lists with a
directive between their braces are still skipped. BRO1509 and BRO1121, which put a one-line
enum's values on their own lines, then don't add the trailing comma either. bro migrate doesn't
write it.
Switch expressions and with¶
[*.cs]
# true (default): checked like the other lists.
# false: not checked, in either mode.
stylebro_trailing_comma_switch_expressions = true
stylebro_trailing_comma_with_expressions = true
bro migrate writes false for both when the analyzer it migrates from didn't check them (SA1413 before 1.2,
StyleSharp's SST1413).
How the fix works¶
The comma goes right after the last item's code, so before a trailing comment: "beta" // last becomes
"beta", // last. When code follows the item directly ("beta"};), the fix inserts , so the comma gets the space
after it that the formatter expects; before a collection expression's ] it is the comma alone (443]; becomes
443,];). Nested lists get all their commas in one dotnet format run.
Not reported¶
- Lists with a preprocessor directive between their braces, such as an item inside
#if. Which item is last then depends on the build configuration, and in a multi-targeted project each target framework would need a different edit to the same file. - List patterns and property patterns (
o is [...],o is Point {...}, nested ones too), and{ key, value }pairs in a dictionary initializer.