Skip to content
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
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
}
}
39 changes: 39 additions & 0 deletions src/Tests/CodeFixes.Tests/CS0164LabelHasNotBeenReferencedTests.cs
Original file line number Diff line number Diff line change
@@ -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<LabeledStatementCodeFixProvider>
{
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));
}
}