Skip to content

Commit 34e1f25

Browse files
authored
Improve analyzer RCS1259 (#1268)
1 parent c50bc3e commit 34e1f25

File tree

6 files changed

+41
-27
lines changed

6 files changed

+41
-27
lines changed

ChangeLog.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414

1515
### Fixed
1616

17-
- Fix [RCS1228](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1228) ([PR](https://github.com/dotnet/roslynator/pull/1249))
18-
- Fix [RCS1213](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1213) ([PR](https://github.com/dotnet/roslynator/pull/1254))
19-
- Fix [RCS1055](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1055) ([PR](https://github.com/dotnet/roslynator/pull/1253))
20-
- Fix [RCS1196](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1196) ([PR](https://github.com/dotnet/roslynator/pull/1235))
21-
- Fix [RCS1257](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1257) ([PR](https://github.com/dotnet/roslynator/pull/1264))
17+
- Fix analyzer [RCS1228](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1228) ([PR](https://github.com/dotnet/roslynator/pull/1249))
18+
- Fix analyzer [RCS1213](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1213) ([PR](https://github.com/dotnet/roslynator/pull/1254))
19+
- Fix analyzer [RCS1055](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1055) ([PR](https://github.com/dotnet/roslynator/pull/1253))
20+
- Fix analyzer [RCS1196](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1196) ([PR](https://github.com/dotnet/roslynator/pull/1235))
21+
- Fix analyzer [RCS1257](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1257) ([PR](https://github.com/dotnet/roslynator/pull/1264))
22+
- Fix analyzer [RCS1259](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1259) ([PR](https://github.com/dotnet/roslynator/pull/1268))
2223

2324
## [4.6.2] - 2023-11-10
2425

src/Analyzers.CodeFixes/CSharp/CodeFixes/NamespaceDeclarationCodeFixProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
4141
{
4242
CodeAction codeAction = CodeAction.Create(
4343
"Remove empty namespace declaration",
44-
ct => RemoveEmptyNamespaceDeclarationRefactoring.RefactorAsync(context.Document, namespaceDeclaration, ct),
44+
ct => context.Document.RemoveNodeAsync(namespaceDeclaration, ct),
4545
GetEquivalenceKey(diagnostic));
4646

4747
context.RegisterCodeFix(codeAction, diagnostic);

src/Analyzers.CodeFixes/CSharp/CodeFixes/RemoveEmptySyntaxCodeFixProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
4343
case SyntaxKind.EmptyStatement:
4444
case SyntaxKind.FinallyClause:
4545
case SyntaxKind.NamespaceDeclaration:
46+
case SyntaxKind.FileScopedNamespaceDeclaration:
4647
case SyntaxKind.ObjectCreationExpression:
4748
case SyntaxKind.RegionDirectiveTrivia:
4849
return true;
@@ -100,11 +101,11 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
100101
context.RegisterCodeFix(codeAction, diagnostic);
101102
break;
102103
}
103-
case NamespaceDeclarationSyntax namespaceDeclaration:
104+
case BaseNamespaceDeclarationSyntax namespaceDeclaration:
104105
{
105106
CodeAction codeAction = CodeAction.Create(
106107
"Remove empty namespace declaration",
107-
ct => RemoveEmptyNamespaceDeclarationRefactoring.RefactorAsync(document, namespaceDeclaration, ct),
108+
ct => document.RemoveNodeAsync(namespaceDeclaration, ct),
108109
GetEquivalenceKey(diagnostic));
109110

110111
context.RegisterCodeFix(codeAction, diagnostic);

src/Analyzers.CodeFixes/CSharp/Refactorings/RemoveEmptyNamespaceDeclarationRefactoring.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Analyzers/CSharp/Analysis/RemoveEmptySyntaxAnalyzer.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public override void Initialize(AnalysisContext context)
3434
context.RegisterSyntaxNodeAction(f => AnalyzeFinallyClause(f), SyntaxKind.FinallyClause);
3535
context.RegisterSyntaxNodeAction(f => AnalyzeObjectCreationExpression(f), SyntaxKind.ObjectCreationExpression);
3636
context.RegisterSyntaxNodeAction(f => AnalyzeNamespaceDeclaration(f), SyntaxKind.NamespaceDeclaration);
37+
context.RegisterSyntaxNodeAction(f => AnalyzeFileScopedNamespaceDeclaration(f), SyntaxKind.FileScopedNamespaceDeclaration);
3738
context.RegisterSyntaxNodeAction(f => AnalyzeRegionDirective(f), SyntaxKind.RegionDirectiveTrivia);
3839
context.RegisterSyntaxNodeAction(f => AnalyzeEmptyStatement(f), SyntaxKind.EmptyStatement);
3940
}
@@ -180,6 +181,16 @@ private static void AnalyzeNamespaceDeclaration(SyntaxNodeAnalysisContext contex
180181
ReportDiagnostic(context, declaration, "namespace declaration");
181182
}
182183

184+
private static void AnalyzeFileScopedNamespaceDeclaration(SyntaxNodeAnalysisContext context)
185+
{
186+
var declaration = (FileScopedNamespaceDeclarationSyntax)context.Node;
187+
188+
if (declaration.Members.Any())
189+
return;
190+
191+
ReportDiagnostic(context, declaration, "namespace declaration");
192+
}
193+
183194
private static void AnalyzeRegionDirective(SyntaxNodeAnalysisContext context)
184195
{
185196
var regionDirective = (RegionDirectiveTriviaSyntax)context.Node;

src/Tests/Analyzers.Tests/RCS1259RemoveEmptySyntaxTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,14 @@ class C
423423
");
424424
}
425425

426+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.RemoveEmptySyntax)]
427+
public async Task Test_FileScopedNamespace()
428+
{
429+
await VerifyDiagnosticAndFixAsync(@"
430+
[|namespace N1;|]
431+
", "");
432+
}
433+
426434
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.RemoveEmptySyntax)]
427435
public async Task Test_RegionDirective()
428436
{
@@ -492,4 +500,16 @@ void M(bool p)
492500
}
493501
", options: Options.AddAllowedCompilerDiagnosticId("CS0642"));
494502
}
503+
504+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.RemoveEmptySyntax)]
505+
public async Task TestNoDiagnostic_FileScopedNamespaceDeclaration()
506+
{
507+
await VerifyNoDiagnosticAsync(@"
508+
namespace N1;
509+
510+
class C
511+
{
512+
}
513+
");
514+
}
495515
}

0 commit comments

Comments
 (0)