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

Fix RCS1055 #1253

Merged
merged 6 commits into from
Nov 18, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- 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))

## [4.6.2] - 2023-11-10

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override ImmutableArray<string> FixableDiagnosticIds
DiagnosticIdentifiers.RemoveRedundantOverridingMember,
DiagnosticIdentifiers.AddOrRemoveAccessibilityModifiers,
DiagnosticIdentifiers.RemoveRedundantSealedModifier,
DiagnosticIdentifiers.AvoidSemicolonAtEndOfDeclaration,
DiagnosticIdentifiers.UnnecessarySemicolonAtEndOfDeclaration,
DiagnosticIdentifiers.OrderModifiers,
DiagnosticIdentifiers.MakeFieldReadOnly,
DiagnosticIdentifiers.UseConstantInsteadOfField,
Expand Down Expand Up @@ -97,11 +97,11 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
ModifiersCodeFixRegistrator.RemoveModifier(context, diagnostic, memberDeclaration, SyntaxKind.SealedKeyword);
break;
}
case DiagnosticIdentifiers.AvoidSemicolonAtEndOfDeclaration:
case DiagnosticIdentifiers.UnnecessarySemicolonAtEndOfDeclaration:
{
CodeAction codeAction = CodeAction.Create(
"Remove unnecessary semicolon",
ct => AvoidSemicolonAtEndOfDeclarationRefactoring.RefactorAsync(context.Document, memberDeclaration, ct),
ct => RemoveSemicolonAtEndOfDeclarationRefactoring.RefactorAsync(context.Document, memberDeclaration, ct),
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Roslynator.CSharp.Refactorings;

internal static class AvoidSemicolonAtEndOfDeclarationRefactoring
internal static class RemoveSemicolonAtEndOfDeclarationRefactoring
{
public static Task<Document> RefactorAsync(
Document document,
Expand Down
5 changes: 2 additions & 3 deletions src/Analyzers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2811,9 +2811,8 @@ public void Foo()
</Analyzer>
<Analyzer>
<Id>RCS1055</Id>
<Identifier>AvoidSemicolonAtEndOfDeclaration</Identifier>
<Title>Avoid semicolon at the end of declaration.</Title>
<MessageFormat>Remove semicolon from the end of declaration.</MessageFormat>
<Identifier>UnnecessarySemicolonAtEndOfDeclaration</Identifier>
<Title>Unnecessary semicolon at the end of declaration.</Title>
<DefaultSeverity>Hidden</DefaultSeverity>
<IsEnabledByDefault>true</IsEnabledByDefault>
<SupportsFadeOut>true</SupportsFadeOut>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Roslynator.CSharp.Analysis;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class AvoidSemicolonAtEndOfDeclarationAnalyzer : BaseDiagnosticAnalyzer
public sealed class UnnecessarySemicolonAtEndOfDeclarationAnalyzer : BaseDiagnosticAnalyzer
{
private static ImmutableArray<DiagnosticDescriptor> _supportedDiagnostics;

Expand All @@ -18,7 +18,7 @@ public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
get
{
if (_supportedDiagnostics.IsDefault)
Immutable.InterlockedInitialize(ref _supportedDiagnostics, DiagnosticRules.AvoidSemicolonAtEndOfDeclaration);
Immutable.InterlockedInitialize(ref _supportedDiagnostics, DiagnosticRules.UnnecessarySemicolonAtEndOfDeclaration);

return _supportedDiagnostics;
}
Expand All @@ -45,46 +45,55 @@ private static void AnalyzeNamespaceDeclaration(SyntaxNodeAnalysisContext contex
if (semicolon.Parent is not null
&& !semicolon.IsMissing)
{
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticRules.AvoidSemicolonAtEndOfDeclaration, semicolon);
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticRules.UnnecessarySemicolonAtEndOfDeclaration, semicolon);
}
}

private static void AnalyzeClassDeclaration(SyntaxNodeAnalysisContext context)
{
var declaration = (ClassDeclarationSyntax)context.Node;

SyntaxToken semicolon = declaration.SemicolonToken;

if (semicolon.Parent is not null
&& !semicolon.IsMissing)
if (declaration.CloseBraceToken.IsKind(SyntaxKind.CloseBraceToken))
{
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticRules.AvoidSemicolonAtEndOfDeclaration, semicolon);
SyntaxToken semicolon = declaration.SemicolonToken;

if (semicolon.Parent is not null
&& !semicolon.IsMissing)
{
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticRules.UnnecessarySemicolonAtEndOfDeclaration, semicolon);
}
}
}

private static void AnalyzeInterfaceDeclaration(SyntaxNodeAnalysisContext context)
{
var declaration = (InterfaceDeclarationSyntax)context.Node;

SyntaxToken semicolon = declaration.SemicolonToken;

if (semicolon.Parent is not null
&& !semicolon.IsMissing)
if (declaration.CloseBraceToken.IsKind(SyntaxKind.CloseBraceToken))
{
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticRules.AvoidSemicolonAtEndOfDeclaration, semicolon);
SyntaxToken semicolon = declaration.SemicolonToken;

if (semicolon.Parent is not null
&& !semicolon.IsMissing)
{
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticRules.UnnecessarySemicolonAtEndOfDeclaration, semicolon);
}
}
}

private static void AnalyzeStructDeclaration(SyntaxNodeAnalysisContext context)
{
var declaration = (StructDeclarationSyntax)context.Node;

SyntaxToken semicolon = declaration.SemicolonToken;

if (semicolon.Parent is not null
&& !semicolon.IsMissing)
if (declaration.CloseBraceToken.IsKind(SyntaxKind.CloseBraceToken))
{
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticRules.AvoidSemicolonAtEndOfDeclaration, semicolon);
SyntaxToken semicolon = declaration.SemicolonToken;

if (semicolon.Parent is not null
&& !semicolon.IsMissing)
{
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticRules.UnnecessarySemicolonAtEndOfDeclaration, semicolon);
}
}
}

Expand All @@ -99,7 +108,7 @@ private static void AnalyzeRecordDeclaration(SyntaxNodeAnalysisContext context)
if (semicolon.Parent is not null
&& !semicolon.IsMissing)
{
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticRules.AvoidSemicolonAtEndOfDeclaration, semicolon);
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticRules.UnnecessarySemicolonAtEndOfDeclaration, semicolon);
}
}
}
Expand All @@ -113,7 +122,7 @@ private static void AnalyzeEnumDeclaration(SyntaxNodeAnalysisContext context)
if (semicolon.Parent is not null
&& !semicolon.IsMissing)
{
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticRules.AvoidSemicolonAtEndOfDeclaration, semicolon);
DiagnosticHelpers.ReportDiagnostic(context, DiagnosticRules.UnnecessarySemicolonAtEndOfDeclaration, semicolon);
}
}
}
2 changes: 1 addition & 1 deletion src/Analyzers/CSharp/DiagnosticIdentifiers.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static partial class DiagnosticIdentifiers
public const string IncludeParenthesesWhenCreatingNewObject = "RCS1050";
public const string AddOrRemoveParenthesesFromConditionInConditionalOperator = "RCS1051";
public const string DeclareEachAttributeSeparately = "RCS1052";
public const string AvoidSemicolonAtEndOfDeclaration = "RCS1055";
public const string UnnecessarySemicolonAtEndOfDeclaration = "RCS1055";
public const string AvoidUsageOfUsingAliasDirective = "RCS1056";
public const string UseCompoundAssignment = "RCS1058";
public const string AvoidLockingOnPubliclyAccessibleInstance = "RCS1059";
Expand Down
10 changes: 5 additions & 5 deletions src/Analyzers/CSharp/DiagnosticRules.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -512,15 +512,15 @@ public static partial class DiagnosticRules
customTags: Array.Empty<string>());

/// <summary>RCS1055</summary>
public static readonly DiagnosticDescriptor AvoidSemicolonAtEndOfDeclaration = DiagnosticDescriptorFactory.Create(
id: DiagnosticIdentifiers.AvoidSemicolonAtEndOfDeclaration,
title: "Avoid semicolon at the end of declaration.",
messageFormat: "Remove semicolon from the end of declaration.",
public static readonly DiagnosticDescriptor UnnecessarySemicolonAtEndOfDeclaration = DiagnosticDescriptorFactory.Create(
id: DiagnosticIdentifiers.UnnecessarySemicolonAtEndOfDeclaration,
title: "Unnecessary semicolon at the end of declaration.",
messageFormat: "Unnecessary semicolon at the end of declaration.",
category: DiagnosticCategories.Roslynator,
defaultSeverity: DiagnosticSeverity.Hidden,
isEnabledByDefault: true,
description: null,
helpLinkUri: DiagnosticIdentifiers.AvoidSemicolonAtEndOfDeclaration,
helpLinkUri: DiagnosticIdentifiers.UnnecessarySemicolonAtEndOfDeclaration,
customTags: WellKnownDiagnosticTags.Unnecessary);

/// <summary>RCS1056</summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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.Tasks;
using Microsoft.CodeAnalysis;
using Roslynator.CSharp.CodeFixes;
using Roslynator.Testing.CSharp;
using Xunit;

namespace Roslynator.CSharp.Analysis.Tests;

public class RCS1055UnnecessarySemicolonAtEndOfDeclarationAnalyzerTests : AbstractCSharpDiagnosticVerifier<UnnecessarySemicolonAtEndOfDeclarationAnalyzer, MemberDeclarationCodeFixProvider>
{
public override DiagnosticDescriptor Descriptor { get; } = DiagnosticRules.UnnecessarySemicolonAtEndOfDeclaration;

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UnnecessarySemicolonAtEndOfDeclaration)]
public async Task TestNoDiagnostic_Class()
{
await VerifyNoDiagnosticAsync(@"
class C;
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UnnecessarySemicolonAtEndOfDeclaration)]
public async Task TestNoDiagnostic_Struct()
{
await VerifyNoDiagnosticAsync(@"
struct C;
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UnnecessarySemicolonAtEndOfDeclaration)]
public async Task TestNoDiagnostic_Interface()
{
await VerifyNoDiagnosticAsync(@"
interface C;
");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ roslynator_analyzers.enabled_by_default = true|false
# Declare each attribute separately
#dotnet_diagnostic.rcs1052.severity = none

# Avoid semicolon at the end of declaration
# Unnecessary semicolon at the end of declaration
#dotnet_diagnostic.rcs1055.severity = silent

# Avoid usage of using alias directive
Expand Down