diff --git a/ChangeLog.md b/ChangeLog.md index b9956b3af5..d72b386a08 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix [RCS1146](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1146.md) ([#1098](https://github.com/JosefPihrt/Roslynator/pull/1098)). - Fix [RCS1154](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1154.md) ([#1105](https://github.com/JosefPihrt/Roslynator/pull/1105)). - Fix [RCS1211](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1211.md) ([#1095](https://github.com/JosefPihrt/Roslynator/pull/1095)). +- Fix [RCS0005](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS0005.md) ([#1114](https://github.com/JosefPihrt/Roslynator/pull/1114)). ## [4.3.0] - 2023-04-24 diff --git a/src/Formatting.Analyzers/CSharp/AddBlankLineBeforeEndRegionDirectiveAnalyzer.cs b/src/Formatting.Analyzers/CSharp/AddBlankLineBeforeEndRegionDirectiveAnalyzer.cs index 2ad5d09e98..544935c096 100644 --- a/src/Formatting.Analyzers/CSharp/AddBlankLineBeforeEndRegionDirectiveAnalyzer.cs +++ b/src/Formatting.Analyzers/CSharp/AddBlankLineBeforeEndRegionDirectiveAnalyzer.cs @@ -36,15 +36,15 @@ private static void AnalyzeEndRegionDirectiveTrivia(SyntaxNodeAnalysisContext co { var endRegionDirective = (EndRegionDirectiveTriviaSyntax)context.Node; - if (IsPrecededWithEmptyLineOrRegionDirective()) - return; - - DiagnosticHelpers.ReportDiagnostic( - context, - DiagnosticRules.AddBlankLineBeforeEndRegionDirective, - Location.Create(endRegionDirective.SyntaxTree, endRegionDirective.Span.WithLength(0))); + if (!IsPrecededWithEmptyLineOrRegionDirective(endRegionDirective)) + { + DiagnosticHelpers.ReportDiagnostic( + context, + DiagnosticRules.AddBlankLineBeforeEndRegionDirective, + Location.Create(endRegionDirective.SyntaxTree, endRegionDirective.Span.WithLength(0))); + } - bool IsPrecededWithEmptyLineOrRegionDirective() + static bool IsPrecededWithEmptyLineOrRegionDirective(EndRegionDirectiveTriviaSyntax endRegionDirective) { SyntaxTrivia parentTrivia = endRegionDirective.ParentTrivia; @@ -78,7 +78,10 @@ bool IsPrecededWithEmptyLineOrRegionDirective() return false; } - return en.Current.IsEndOfLineTrivia(); + return en.Current.IsKind( + SyntaxKind.EndOfLineTrivia, + SyntaxKind.RegionDirectiveTrivia, + SyntaxKind.EndRegionDirectiveTrivia); } } diff --git a/src/Tests/Formatting.Analyzers.Tests/RCS0005AddBlankLineBeforeEndRegionDirectiveTests.cs b/src/Tests/Formatting.Analyzers.Tests/RCS0005AddBlankLineBeforeEndRegionDirectiveTests.cs index 7bdf6da5af..18341f56f5 100644 --- a/src/Tests/Formatting.Analyzers.Tests/RCS0005AddBlankLineBeforeEndRegionDirectiveTests.cs +++ b/src/Tests/Formatting.Analyzers.Tests/RCS0005AddBlankLineBeforeEndRegionDirectiveTests.cs @@ -192,6 +192,39 @@ void M() #endregion } +"); + } + + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AddBlankLineBeforeEndRegionDirective)] + public async Task TestNoDiagnostic_TwoEndRegions() + { + await VerifyNoDiagnosticAsync(@" +#region +class C +{ + #region + void M() + { + } + + #endregion + + #endregion +} +"); + } + + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AddBlankLineBeforeEndRegionDirective)] + public async Task TestNoDiagnostic_EmptyRegions() + { + await VerifyNoDiagnosticAsync(@" + #region + + #region + + #endregion + + #endregion "); } }