diff --git a/src/CSharp/CSharp/IndentationAnalysis.cs b/src/CSharp/CSharp/IndentationAnalysis.cs index a2a4a04349..99949c3e55 100644 --- a/src/CSharp/CSharp/IndentationAnalysis.cs +++ b/src/CSharp/CSharp/IndentationAnalysis.cs @@ -43,6 +43,12 @@ public static IndentationAnalysis Create(SyntaxNode node, CancellationToken canc { return new IndentationAnalysis(indentation, trivia1.Span.Length - trivia2.Span.Length); } + else if (indentation.Span.Length > 0) + { + return (trivia1.Span.Length > 0) + ? new IndentationAnalysis(indentation, trivia1.Span.Length) + : new IndentationAnalysis(indentation, indentation.Span.Length); + } else if (trivia1.Span.Length > 0) { return new IndentationAnalysis(trivia1, -1); diff --git a/src/Tests/Formatting.Analyzers.Tests/RCS0053FixFormattingOfListTests.cs b/src/Tests/Formatting.Analyzers.Tests/RCS0053FixFormattingOfListTests.cs index 4000346953..6487297c6f 100644 --- a/src/Tests/Formatting.Analyzers.Tests/RCS0053FixFormattingOfListTests.cs +++ b/src/Tests/Formatting.Analyzers.Tests/RCS0053FixFormattingOfListTests.cs @@ -1252,5 +1252,25 @@ void M() } "); } + + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.FixFormattingOfList)] + public async Task TestNoDiagnostic_LambdaBlockBodyInGlobalStatement() + { + await VerifyNoDiagnosticAsync(@" +using System.Linq; + +foreach (var item in Enumerable.Range(0, 10)) +{ + var s = """"; +} + +var items = Enumerable.Range(0, 10) + .Select(f => + { + return f; + }) + .Select(f => f); +", options: Options.WithCompilationOptions(Options.CompilationOptions.WithOutputKind(OutputKind.ConsoleApplication))); + } } }