Skip to content

Incorrect formatting behaviour when using SyntaxFactory.Block #63013

@bjornhellander

Description

@bjornhellander

Version Used: 4.3.0-2.final (same behaviour with 4.3.0-3, but not with 4.3.0-1)

After updating the c# 11 test project in StyleCop.Analyzers to use a new Roslyn version, three tests (all called TestCodeFixProviderWithAlternateIndentationAsync) failed due to different formatting behaviour when adding braces around code in if statements (using SyntaxFactory.Block).

See DotNetAnalyzers/StyleCopAnalyzers#3535 for updated code.
The call to SyntaxFactory.Block is made here: https://github.com/bjornhellander/StyleCopAnalyzers/blob/feature/sa1206-required/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1503CodeFixProvider.cs#L72
And here we set indentation size to 1 in the failing test cases: https://github.com/bjornhellander/StyleCopAnalyzers/blob/feature/sa1206-required/StyleCop.Analyzers/StyleCop.Analyzers.Test/Verifiers/StyleCopCodeFixVerifier%602.cs#L120

The three tests fail in slightly different ways, but the fixed code seems to have indentation size 4, instead of the specified and expected 1:

using System.Diagnostics;

public class Foo
{
 public void Bar(int i)
 {
  if (i == 0)
-  {
-   Debug.Assert(true);
-  }
- }
+        {
+            Debug.Assert(true);
+        }
+    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-IDEIDE-CodeStyleBuilt-in analyzers, fixes, and refactoringsIDE-FormatterCode formatter and/or smart indent

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions