Skip to content

Update SA1137 to check collection expressions - #4137

Merged
AArnott merged 2 commits into
masterfrom
csharp12-sa1137-collection-expressions
Oct 8, 2026
Merged

AArnott merged 2 commits into
masterfrom
csharp12-sa1137-collection-expressions

Conversation

@AArnott

@AArnott AArnott commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Fixes #3904. Part of #4044 (#4007 collection expressions).

This replaces #3910 with @bjornhellander's fix from bjornhellander/NewStyleCopAnalyzers#23 (cherry-picked with -x, authorship kept). His review on #3910 suggested a different approach, and this is that approach.

Behavior change: SA1137 now checks collection expressions ([ ... ]) the same way it checks initializers. The elements must share one indentation, and the closing bracket of a multi-line collection expression must line up with its opening bracket when the opening bracket starts its own line. Before, code like the following wasn't reported:

private int[] testField =
[
    1,
       2,
  3
];

Commits:

  • Björn's cfdf2d45e: registers SyntaxKindEx.CollectionExpression and checks the brackets and elements. Tests cover single-line expressions, an opening bracket on the previous line or the same line, and code fixes. I only added WorkItem(3904).
  • New test (mine): spread elements (.. other) and nested collection expressions passed as an argument, with the code fix.

Tests: all SA1137 tests pass on C# 12–15 locally.

bjornhellander and others added 2 commits October 7, 2026 21:38
From bjornhellander/NewStyleCopAnalyzers#23.

Adapted while cherry-picking: added WorkItem(3904) to the tests. This replaces #3910.

(cherry picked from commit cfdf2d45e392ff1090454f18ca93a34086faacd1)
@codecov

codecov Bot commented Oct 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.39%. Comparing base (53ce0bf) to head (40bf437).

Additional details and impacted files
@@           Coverage Diff            @@
##           master    #4137    +/-   ##
========================================
  Coverage   97.39%   97.39%            
========================================
  Files        1073     1074     +1     
  Lines      122990   123103   +113     
  Branches     4173     4173            
========================================
+ Hits       119781   119894   +113     
  Misses       2108     2108            
  Partials     1101     1101            
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AArnott
AArnott marked this pull request as ready for review October 8, 2026 04:01
@AArnott
AArnott merged commit 0be4ee3 into master Oct 8, 2026
25 checks passed
@AArnott
AArnott deleted the csharp12-sa1137-collection-expressions branch October 8, 2026 14:14
This was referenced Nov 11, 2026
This was referenced Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SA1137 is not raised in C# 12 collection expressions

2 participants