Skip to content

SA1117: Add a code fix for inconsistent parameter lines - #4353

Merged
AArnott merged 6 commits into
masterfrom
aarnott-scaling-giggle
Oct 20, 2026
Merged

AArnott merged 6 commits into
masterfrom
aarnott-scaling-giggle

Conversation

@AArnott

@AArnott AArnott commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Fixes #1504

SA1117 reports parameter and argument lists whose items are inconsistently split across lines, but had no code fix. This adds a fix that places each list item on a separate line with indentation based on the opening delimiter, preserving comments and supporting parameter, argument, attribute, and array-rank lists. The action text is localized in the existing Russian and Chinese resources.

The regression test failed before the fix because no applicable code action was offered. The analyzer is unchanged; the implementation runs only when the user invokes the code fix, so analyzer performance is unaffected.

Validation: the C# 6 and C# 15 SA1117 suites pass (76 and 99 tests), and the test project builds with zero warnings.

Allow parameter and argument lists with inconsistent line breaks to be reformatted as one item per line.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@AArnott
AArnott marked this pull request as ready for review October 19, 2026 23:36
Copilot AI balanced review requested due to automatic review settings October 19, 2026 23:36

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The fix can throw for omitted array dimensions and leaves multiline elements incorrectly indented.

2 open issues
1 file not reviewed
  • src/StyleCop.Analyzers/Lightup/.generated/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator/ReadabilityResources.Designer.cs: Generated file
What changed in this PR

Adds an SA1117 code fix that places list items on separate lines, addressing #1504 without changing analyzer behavior.

Changes:

  • Adds the code-fix provider and regression tests.
  • Adds English, Russian, and Chinese action text.
File Description
test/​StyleCop.Analyzers.Test/​ReadabilityRules/​SA1117UnitTests.cs Adds fix tests for supported lists and comments.
src/​StyleCop.Analyzers/​ReadabilityRules/​ReadabilityResources.zh-CN.resx Adds Chinese action text.
src/​StyleCop.Analyzers/​ReadabilityRules/​ReadabilityResources.ru-RU.resx Adds Russian action text.
src/​StyleCop.Analyzers/​ReadabilityRules/​ReadabilityResources.resx Adds English action text.
src/​StyleCop.Analyzers/​Lightup/​.generated/​Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/​Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator/​ReadabilityResources.Designer.cs Exposes the new resource property.
src/​StyleCop.Analyzers.CodeFixes/​ReadabilityRules/​SA1117CodeFixProvider.cs Implements list-item splitting and Fix All support.

Review effort: Balanced

Avoid offering a fix for omitted array-rank sizes and preserve multiline item indentation when splitting lists.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 19, 2026 23:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Nested omitted array ranks can still trigger an exception, and later multiline arguments can become overindented.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Reject empty spans for nested omitted-rank arrays

src/​StyleCop.Analyzers.CodeFixes/​ReadabilityRules/​SA1117CodeFixProvider.cs:45

The omitted-rank guard still misses arrays nested in arguments. For Consume(new int[1][,,\n]);, FindToken returns ], and GetContainingList walks past the rank to the enclosing invocation's argument list. Its argument has a nonempty span, so an action is offered, but invoking it throws at elements.Count < 2. Reject empty diagnostic spans before registering the action, and add a nested-array regression test alongside the standalone omitted-rank test.

2 resolved since last review
1 file not reviewed
  • src/StyleCop.Analyzers/Lightup/.generated/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator/ReadabilityResources.Designer.cs: Generated file

Review effort: Balanced

Adjust each moved multiline item relative to its existing indentation so trailing lambdas remain formatted consistently.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 19, 2026 23:56
Do not offer a fix for zero-width diagnostics that resolve to an enclosing argument list.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@AArnott

AArnott commented Oct 19, 2026

Copy link
Copy Markdown
Contributor Author

The omitted-rank guard still misses arrays nested in arguments.

Added an early guard for zero-width diagnostic spans and a nested Consume(new int[1][,, ...]) regression test. The code fix is not offered for the enclosing invocation argument in this case.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Nested omitted array ranks can still trigger an exception, and multiline indentation handling remains incorrect in two cases.

1 open issue
2 previously missed issues

In code that hasn't changed since last review

Medium severity Reject empty omitted-rank diagnostics in nested arguments

src/​StyleCop.Analyzers.CodeFixes/​ReadabilityRules/​SA1117CodeFixProvider.cs:43

The omitted-rank guard still fails when the array is nested in an argument, such as Bar(new int[1][,,\n]); with Bar(object value). The zero-width diagnostic at ] resolves to the enclosing argument list, which passes the guard because its argument is nonempty. Invoking the action then throws at the single-element check. Reject empty diagnostic spans before token lookup, and add a regression asserting that no action is offered for this nested case.

Medium severity Adjust continuation lines for already-multiline elements

src/​StyleCop.Analyzers.CodeFixes/​ReadabilityRules/​SA1117CodeFixProvider.cs:128

Continuation lines also need adjustment when an element already starts on a new line. With Bar(1, 2, at eight spaces followed by a multiline lambda at eight spaces, the fix moves the lambda's first line to twelve spaces but leaves its braces at eight and its body at twelve. startsOnSameLine is false, so the continuation tokens are skipped. Apply the indentation adjustment whenever the element's first-line indentation changes, and add a regression for this already-multiline case.

1 resolved since last review
1 file not reviewed
  • src/StyleCop.Analyzers/Lightup/.generated/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator/ReadabilityResources.Designer.cs: Generated file

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 20, 2026 00:02
AArnott and others added 2 commits October 19, 2026 18:06
Preserve nested indentation when a multiline item moves either direction, including trivia on both sides of tokens and blank lines.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Shift continuations whenever a list item moves to a different indentation, including items that already start on a separate line.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@AArnott

AArnott commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

Continuation lines also need adjustment when an element already starts on a new line.

The fix now shifts multiline continuations whenever the item's original line indentation differs from the target, whether the item originally began inline or on a separate line. Added regression coverage for the already-multiline argument; the nested omitted-rank case is covered by an empty-diagnostic-span guard and its own test.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Multiline arguments already starting on separate lines can have their headers moved without corresponding continuation-line indentation.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Separate-line argument continuations ignore indentation changes

src/​StyleCop.Analyzers.CodeFixes/​ReadabilityRules/​SA1117CodeFixProvider.cs:133

An argument that already starts on a new line still has its first token moved to targetIndentation, but startsOnSameLine prevents its continuation lines from moving. For example:

        Bar(
        () =>
        {
            Work();
        },
        2, 3);

Fixing SA1117 moves the lambda header from 8 to 12 spaces while leaving its braces/body at 8/12 spaces. Apply the indentation adjustment whenever the element's indentation changes, not only when it follows the delimiter on the same line. Add a regression test with a multiline argument already starting on a separate line.

1 resolved since last review
1 file not reviewed
  • src/StyleCop.Analyzers/Lightup/.generated/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator/ReadabilityResources.Designer.cs: Generated file

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 20, 2026 00:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Indentation remains incorrect for comment-prefixed items and column-zero multiline lambda braces.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

Medium severity Reindent items following preserved comments

src/​StyleCop.Analyzers.CodeFixes/​ReadabilityRules/​SA1117CodeFixProvider.cs:128

When an item follows a preserved // comment, its old indentation after the newline is copied unchanged. For Bar(1, // comment\n 2, 3); with the call indented eight spaces, the comment and other arguments move to twelve spaces, but 2 stays at eight. A multiline lambda in that position also keeps its old header indentation while its braces and body shift. Reindent the retained first-token trivia and add regression tests for a commented argument and a commented multiline lambda whose indentation must change.

Medium severity Indent column-zero multiline lambda braces

src/​StyleCop.Analyzers.CodeFixes/​ReadabilityRules/​SA1117CodeFixProvider.cs:201

For Bar(1, 2,\n() =>\n{\n Work();\n});, the lambda's braces start at column zero and have empty leading trivia. ShiftTrivia therefore returns an empty list, leaving the braces at column zero while the lambda header and body move right. Insert indentation when a continuation token starts a line with empty trivia and the element's original indentation is empty. Add a column-zero regression; the existing multiline tests all have indentation whitespace.

1 file not reviewed
  • src/StyleCop.Analyzers/Lightup/.generated/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator/ReadabilityResources.Designer.cs: Generated file

Review effort: Balanced

@AArnott
AArnott merged commit 719181f into master Oct 20, 2026
30 checks passed
@AArnott
AArnott deleted the aarnott-scaling-giggle branch October 20, 2026 00:20
@codecov

codecov Bot commented Oct 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.83270% with 64 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.62%. Comparing base (abc4947) to head (1abc1d4).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4353      +/-   ##
==========================================
- Coverage   97.66%   97.62%   -0.04%     
==========================================
  Files        1334     1335       +1     
  Lines      149089   149615     +526     
  Branches     5056     5093      +37     
==========================================
+ Hits       145603   146065     +462     
- Misses       2210     2264      +54     
- Partials     1276     1286      +10     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This was referenced Nov 13, 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.

Code fix: SA1117 (ParametersMustBeOnSameLineOrSeparateLines)

2 participants