Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve analyzer RCS1259 #1268

Merged
merged 6 commits into from Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions ChangeLog.md
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
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
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
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
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
{
}
");
}
}