https://github.com/krk/sa1413
Use Trailing Commas In MultiLine Initializers Analyzer
https://github.com/krk/sa1413
Last synced: 12 months ago
JSON representation
Use Trailing Commas In MultiLine Initializers Analyzer
- Host: GitHub
- URL: https://github.com/krk/sa1413
- Owner: krk
- License: apache-2.0
- Created: 2018-02-02T18:15:09.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-04-24T12:26:46.000Z (about 8 years ago)
- Last Synced: 2025-08-01T08:49:14.526Z (12 months ago)
- Language: C#
- Size: 41 KB
- Stars: 0
- Watchers: 0
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
SA1413 https://github.com/DotNetAnalyzers/StyleCopAnalyzers rule as a stand-alone Roslyn Analyzer.
[](https://ci.appveyor.com/project/krk/sa1413)
[](https://www.nuget.org/packages/RoslynAnalyzers.SA1413/)
# SA1413
TypeName
SA1413UseTrailingCommasInMultiLineInitializers
CheckId
SA1413
Category
Maintainability Rules
:memo: This rule is new for StyleCop Analyzers, and was not present in StyleCop Classic.
## Cause
The last statement in a multi-line C# initializer or list is missing a trailing comma.
### Rationale
This rule is specifically designed to work well with the most widely used source control systems as an aid to long-term
code review. By placing a comma on the last line of a multi-line sequence, developers who append an item to the list or
reorder the list at some point in the future will not need to modify any more lines than absolutely necessary for the
change. As a result, the size of the subsequent code review is minimized and focused, and tools like **git blame**
continue to show the original author and commit for the item that was previously last in the list.
## Rule description
A violation of this rule occurs when the last statement of a C# initializer or list is missing a trailing comma.
For example, the following code would generate one instance of this violation:
```csharp
var x = new Barnacle
{
Age = 100,
Height = 0.2M,
Weight = 0.88M
};
```
The following code would not produce any violations:
```csharp
var x = new Barnacle
{
Age = 100,
Height = 0.2M,
Weight = 0.88M,
};
```
This diagnostic is also reported for other forms of comma-separated list, such as enum members.
## How to fix violations
To fix a violation of this rule, add a trailing comma to the last statement in the initializer.
## How to suppress violations
```csharp
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1413:UseTrailingCommasInMultiLineInitializers", Justification = "Reviewed.")]
```
```csharp
#pragma warning disable SA1413 // UseTrailingCommasInMultiLineInitializers
#pragma warning restore SA1413 // UseTrailingCommasInMultiLineInitializers
```