Skip to content

Commit

Permalink
Improve analyzer RCS1259 (#1268)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Nov 22, 2023
1 parent c50bc3e commit 34e1f25
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 27 deletions.
11 changes: 6 additions & 5 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

This file was deleted.

11 changes: 11 additions & 0 deletions src/Analyzers/CSharp/Analysis/RemoveEmptySyntaxAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1259RemoveEmptySyntaxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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
{
}
");
}
}

0 comments on commit 34e1f25

Please sign in to comment.