From 34e1f25fcd24b138bad34a6631b0c703ad7968f3 Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Wed, 22 Nov 2023 23:44:53 +0100 Subject: [PATCH] Improve analyzer RCS1259 (#1268) --- ChangeLog.md | 11 +++++----- .../NamespaceDeclarationCodeFixProvider.cs | 2 +- .../RemoveEmptySyntaxCodeFixProvider.cs | 5 +++-- ...oveEmptyNamespaceDeclarationRefactoring.cs | 19 ------------------ .../Analysis/RemoveEmptySyntaxAnalyzer.cs | 11 ++++++++++ .../RCS1259RemoveEmptySyntaxTests.cs | 20 +++++++++++++++++++ 6 files changed, 41 insertions(+), 27 deletions(-) delete mode 100644 src/Analyzers.CodeFixes/CSharp/Refactorings/RemoveEmptyNamespaceDeclarationRefactoring.cs diff --git a/ChangeLog.md b/ChangeLog.md index 18658e9547..6716799c77 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -14,11 +14,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- Fix [RCS1228](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1228) ([PR](https://github.com/dotnet/roslynator/pull/1249)) -- Fix [RCS1213](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1213) ([PR](https://github.com/dotnet/roslynator/pull/1254)) -- Fix [RCS1055](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1055) ([PR](https://github.com/dotnet/roslynator/pull/1253)) -- Fix [RCS1196](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1196) ([PR](https://github.com/dotnet/roslynator/pull/1235)) -- Fix [RCS1257](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1257) ([PR](https://github.com/dotnet/roslynator/pull/1264)) +- Fix analyzer [RCS1228](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1228) ([PR](https://github.com/dotnet/roslynator/pull/1249)) +- Fix analyzer [RCS1213](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1213) ([PR](https://github.com/dotnet/roslynator/pull/1254)) +- Fix analyzer [RCS1055](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1055) ([PR](https://github.com/dotnet/roslynator/pull/1253)) +- Fix analyzer [RCS1196](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1196) ([PR](https://github.com/dotnet/roslynator/pull/1235)) +- Fix analyzer [RCS1257](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1257) ([PR](https://github.com/dotnet/roslynator/pull/1264)) +- Fix analyzer [RCS1259](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1259) ([PR](https://github.com/dotnet/roslynator/pull/1268)) ## [4.6.2] - 2023-11-10 diff --git a/src/Analyzers.CodeFixes/CSharp/CodeFixes/NamespaceDeclarationCodeFixProvider.cs b/src/Analyzers.CodeFixes/CSharp/CodeFixes/NamespaceDeclarationCodeFixProvider.cs index 014fce1498..862ba5475d 100644 --- a/src/Analyzers.CodeFixes/CSharp/CodeFixes/NamespaceDeclarationCodeFixProvider.cs +++ b/src/Analyzers.CodeFixes/CSharp/CodeFixes/NamespaceDeclarationCodeFixProvider.cs @@ -41,7 +41,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) { CodeAction codeAction = CodeAction.Create( "Remove empty namespace declaration", - ct => RemoveEmptyNamespaceDeclarationRefactoring.RefactorAsync(context.Document, namespaceDeclaration, ct), + ct => context.Document.RemoveNodeAsync(namespaceDeclaration, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); diff --git a/src/Analyzers.CodeFixes/CSharp/CodeFixes/RemoveEmptySyntaxCodeFixProvider.cs b/src/Analyzers.CodeFixes/CSharp/CodeFixes/RemoveEmptySyntaxCodeFixProvider.cs index 12735eae40..8a749dc954 100644 --- a/src/Analyzers.CodeFixes/CSharp/CodeFixes/RemoveEmptySyntaxCodeFixProvider.cs +++ b/src/Analyzers.CodeFixes/CSharp/CodeFixes/RemoveEmptySyntaxCodeFixProvider.cs @@ -43,6 +43,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) case SyntaxKind.EmptyStatement: case SyntaxKind.FinallyClause: case SyntaxKind.NamespaceDeclaration: + case SyntaxKind.FileScopedNamespaceDeclaration: case SyntaxKind.ObjectCreationExpression: case SyntaxKind.RegionDirectiveTrivia: return true; @@ -100,11 +101,11 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) context.RegisterCodeFix(codeAction, diagnostic); break; } - case NamespaceDeclarationSyntax namespaceDeclaration: + case BaseNamespaceDeclarationSyntax namespaceDeclaration: { CodeAction codeAction = CodeAction.Create( "Remove empty namespace declaration", - ct => RemoveEmptyNamespaceDeclarationRefactoring.RefactorAsync(document, namespaceDeclaration, ct), + ct => document.RemoveNodeAsync(namespaceDeclaration, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); diff --git a/src/Analyzers.CodeFixes/CSharp/Refactorings/RemoveEmptyNamespaceDeclarationRefactoring.cs b/src/Analyzers.CodeFixes/CSharp/Refactorings/RemoveEmptyNamespaceDeclarationRefactoring.cs deleted file mode 100644 index 2e0daaf23b..0000000000 --- a/src/Analyzers.CodeFixes/CSharp/Refactorings/RemoveEmptyNamespaceDeclarationRefactoring.cs +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System.Threading; -using System.Threading.Tasks; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp.Syntax; - -namespace Roslynator.CSharp.Refactorings; - -internal static class RemoveEmptyNamespaceDeclarationRefactoring -{ - public static Task RefactorAsync( - Document document, - NamespaceDeclarationSyntax declaration, - CancellationToken cancellationToken) - { - return document.RemoveNodeAsync(declaration, cancellationToken); - } -} diff --git a/src/Analyzers/CSharp/Analysis/RemoveEmptySyntaxAnalyzer.cs b/src/Analyzers/CSharp/Analysis/RemoveEmptySyntaxAnalyzer.cs index 5178ae7b6b..2b4dc2b304 100644 --- a/src/Analyzers/CSharp/Analysis/RemoveEmptySyntaxAnalyzer.cs +++ b/src/Analyzers/CSharp/Analysis/RemoveEmptySyntaxAnalyzer.cs @@ -34,6 +34,7 @@ public override void Initialize(AnalysisContext context) context.RegisterSyntaxNodeAction(f => AnalyzeFinallyClause(f), SyntaxKind.FinallyClause); context.RegisterSyntaxNodeAction(f => AnalyzeObjectCreationExpression(f), SyntaxKind.ObjectCreationExpression); context.RegisterSyntaxNodeAction(f => AnalyzeNamespaceDeclaration(f), SyntaxKind.NamespaceDeclaration); + context.RegisterSyntaxNodeAction(f => AnalyzeFileScopedNamespaceDeclaration(f), SyntaxKind.FileScopedNamespaceDeclaration); context.RegisterSyntaxNodeAction(f => AnalyzeRegionDirective(f), SyntaxKind.RegionDirectiveTrivia); context.RegisterSyntaxNodeAction(f => AnalyzeEmptyStatement(f), SyntaxKind.EmptyStatement); } @@ -180,6 +181,16 @@ private static void AnalyzeNamespaceDeclaration(SyntaxNodeAnalysisContext contex ReportDiagnostic(context, declaration, "namespace declaration"); } + private static void AnalyzeFileScopedNamespaceDeclaration(SyntaxNodeAnalysisContext context) + { + var declaration = (FileScopedNamespaceDeclarationSyntax)context.Node; + + if (declaration.Members.Any()) + return; + + ReportDiagnostic(context, declaration, "namespace declaration"); + } + private static void AnalyzeRegionDirective(SyntaxNodeAnalysisContext context) { var regionDirective = (RegionDirectiveTriviaSyntax)context.Node; diff --git a/src/Tests/Analyzers.Tests/RCS1259RemoveEmptySyntaxTests.cs b/src/Tests/Analyzers.Tests/RCS1259RemoveEmptySyntaxTests.cs index 0e0717cf21..01a135dd05 100644 --- a/src/Tests/Analyzers.Tests/RCS1259RemoveEmptySyntaxTests.cs +++ b/src/Tests/Analyzers.Tests/RCS1259RemoveEmptySyntaxTests.cs @@ -423,6 +423,14 @@ class C "); } + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.RemoveEmptySyntax)] + public async Task Test_FileScopedNamespace() + { + await VerifyDiagnosticAndFixAsync(@" +[|namespace N1;|] +", ""); + } + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.RemoveEmptySyntax)] public async Task Test_RegionDirective() { @@ -492,4 +500,16 @@ void M(bool p) } ", options: Options.AddAllowedCompilerDiagnosticId("CS0642")); } + + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.RemoveEmptySyntax)] + public async Task TestNoDiagnostic_FileScopedNamespaceDeclaration() + { + await VerifyNoDiagnosticAsync(@" +namespace N1; + +class C +{ +} +"); + } }