diff --git a/ChangeLog.md b/ChangeLog.md index 8b0456ba06..ed6c4c1a51 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -30,6 +30,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Do not remove braces in the cases where there are overlapping local variables. ([RCS1031](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1013.md), [RCS1211](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1211.md), [RCS1208](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1208.md)) ([#1039](https://github.com/JosefPihrt/Roslynator/pull/1039)). - [CLI] Analyze command does not create the XML output file and returns incorrect exit code when only compiler diagnostics are reported ([#1056](https://github.com/JosefPihrt/Roslynator/pull/1056) by @PeterKaszab). - [CLI] Fix exit code when multiple projects are processed ([#1061](https://github.com/JosefPihrt/Roslynator/pull/1061) by @PeterKaszab). +- Fix code fix for CS0164 ([#1031](https://github.com/JosefPihrt/Roslynator/pull/1031)). + ## [4.2.0] - 2022-11-27 diff --git a/src/CodeFixes/CSharp/CodeFixes/LabeledStatementCodeFixProvider.cs b/src/CodeFixes/CSharp/CodeFixes/LabeledStatementCodeFixProvider.cs index a979429e13..2cd0d9ebf9 100644 --- a/src/CodeFixes/CSharp/CodeFixes/LabeledStatementCodeFixProvider.cs +++ b/src/CodeFixes/CSharp/CodeFixes/LabeledStatementCodeFixProvider.cs @@ -2,8 +2,10 @@ using System.Collections.Immutable; using System.Composition; +using System.Linq; using System.Threading.Tasks; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.CSharp.Syntax; using Roslynator.CodeFixes; @@ -30,7 +32,13 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) if (!TryFindFirstAncestorOrSelf(root, context.Span, out LabeledStatementSyntax labeledStatement)) return; + + var codeAction = CodeAction.Create( + "Remove unused label", + ct => context.Document.ReplaceNodeAsync(labeledStatement, labeledStatement.Statement, ct), + EquivalenceKey.Create(diagnostic)); + + context.RegisterCodeFix(codeAction, diagnostic); - CodeFixRegistrator.RemoveStatement(context, diagnostic, labeledStatement, title: "Remove unused label"); } } diff --git a/src/Tests/CodeFixes.Tests/CS0164LabelHasNotBeenReferencedTests.cs b/src/Tests/CodeFixes.Tests/CS0164LabelHasNotBeenReferencedTests.cs new file mode 100644 index 0000000000..e7c8deea75 --- /dev/null +++ b/src/Tests/CodeFixes.Tests/CS0164LabelHasNotBeenReferencedTests.cs @@ -0,0 +1,39 @@ +// Copyright (c) Josef Pihrt 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 Roslynator.Testing.CSharp; +using Xunit; + +namespace Roslynator.CSharp.CodeFixes.Tests; + +public class CS0164LabelHasNotBeenReferencedTests : AbstractCSharpCompilerDiagnosticFixVerifier +{ + public override string DiagnosticId { get; } = CompilerDiagnosticIdentifiers.CS0164_LabelHasNotBeenReferenced; + + [Fact, Trait(Traits.CodeFix, CompilerDiagnosticIdentifiers.CS0164_LabelHasNotBeenReferenced)] + public async Task Test() + { + await VerifyFixAsync(@" +using System; + +class C +{ + int M() + { + start: + return 1; + } +} +", @" +using System; + +class C +{ + int M() + { + return 1; + } +} +", equivalenceKey: EquivalenceKey.Create(DiagnosticId)); + } +}