提交 eefdbeb3 编写于 作者: C CyrusNajmabadi

Support removing unused exception variables.

上级 2b57b223
......@@ -172,6 +172,37 @@ void Method()
void Method()
{
}
}");
}
[WorkItem(20466, "https://github.com/dotnet/roslyn/issues/20466")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedVariable)]
public async Task RemoveUnusedCatchVariable()
{
await TestInRegularAndScriptAsync(
@"class Class
{
void Method()
{
try
{
}
catch (System.Exception [|e|])
{
}
}
}",
@"class Class
{
void Method()
{
try
{
}
catch (System.Exception)
{
}
}
}");
}
}
......
......@@ -17,5 +17,8 @@ internal partial class CSharpRemoveUnusedVariableCodeFixProvider : AbstractRemov
public sealed override ImmutableArray<string> FixableDiagnosticIds
=> ImmutableArray.Create(CS0168, CS0219);
protected override bool IsCatchDeclarationIdentifier(SyntaxToken token)
=> token.Parent is CatchDeclarationSyntax catchDeclaration && catchDeclaration.Identifier == token;
}
}
}
\ No newline at end of file
......@@ -8,27 +8,33 @@
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.RemoveUnusedVariable
{
internal abstract class AbstractRemoveUnusedVariableCodeFixProvider<TLocalDeclarationStatement, TVariableDeclarator, TVariableDeclaration> : SyntaxEditorBasedCodeFixProvider
where TLocalDeclarationStatement: SyntaxNode
where TVariableDeclarator: SyntaxNode
where TVariableDeclaration: SyntaxNode
where TLocalDeclarationStatement : SyntaxNode
where TVariableDeclarator : SyntaxNode
where TVariableDeclaration : SyntaxNode
{
protected abstract bool IsCatchDeclarationIdentifier(SyntaxToken token);
public async override Task RegisterCodeFixesAsync(CodeFixContext context)
{
foreach (var diagnostic in context.Diagnostics)
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var token = root.FindToken(diagnostic.Location.SourceSpan.Start);
var ancestor = token.GetAncestor<TLocalDeclarationStatement>();
if (ancestor == null)
if (!IsCatchDeclarationIdentifier(token))
{
return;
var ancestor = token.GetAncestor<TLocalDeclarationStatement>();
if (ancestor == null)
{
return;
}
}
}
......@@ -42,18 +48,27 @@ protected override Task FixAllAsync(Document document, ImmutableArray<Diagnostic
var root = editor.OriginalRoot;
foreach (var diagnostic in diagnostics)
{
var token = root.FindToken(diagnostic.Location.SourceSpan.Start);
var variableDeclarator = token.GetAncestor<TVariableDeclarator>();
var variableDeclarators = token.GetAncestor<TVariableDeclaration>().ChildNodes().Where(x => x is TVariableDeclarator);
if (variableDeclarators.Count() == 1)
var token = diagnostic.Location.FindToken(cancellationToken);
if (IsCatchDeclarationIdentifier(token))
{
editor.RemoveNode(token.GetAncestor<TLocalDeclarationStatement>());
editor.ReplaceNode(
token.Parent,
token.Parent.ReplaceToken(token, default(SyntaxToken)).WithAdditionalAnnotations(Formatter.Annotation));
}
else if (variableDeclarators.Count() > 1)
else
{
editor.RemoveNode(variableDeclarator);
var variableDeclarator = token.GetAncestor<TVariableDeclarator>();
var variableDeclarators = token.GetAncestor<TVariableDeclaration>().ChildNodes().Where(x => x is TVariableDeclarator);
if (variableDeclarators.Count() == 1)
{
editor.RemoveNode(token.GetAncestor<TLocalDeclarationStatement>());
}
else if (variableDeclarators.Count() > 1)
{
editor.RemoveNode(variableDeclarator);
}
}
}
......@@ -68,4 +83,4 @@ private class MyCodeAction : CodeAction.DocumentChangeAction
}
}
}
}
}
\ No newline at end of file
......@@ -7,16 +7,20 @@ Imports Microsoft.CodeAnalysis.RemoveUnusedVariable
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Namespace Microsoft.CodeAnalysis.VisualBasic.RemoveUnusedVariable
<ExportCodeFixProviderAttribute(LanguageNames.VisualBasic, Name:=PredefinedCodeFixProviderNames.RemoveUnusedVariable), [Shared]>
<ExportCodeFixProvider(LanguageNames.VisualBasic, Name:=PredefinedCodeFixProviderNames.RemoveUnusedVariable), [Shared]>
<ExtensionOrder(After:=PredefinedCodeFixProviderNames.AddImport)>
Friend Class VisualBasicRemoveUnusedVariableCodeFixProvider
Inherits AbstractRemoveUnusedVariableCodeFixProvider(Of
LocalDeclarationStatementSyntax, ModifiedIdentifierSyntax, VariableDeclaratorSyntax)
Private Const BC42024 As String = "BC42024"
Private Const BC42024 As String = NameOf(BC42024)
Public Overrides ReadOnly Property FixableDiagnosticIds As ImmutableArray(Of String) =
ImmutableArray.Create(BC42024)
Protected Overrides Function IsCatchDeclarationIdentifier(token As SyntaxToken) As Boolean
' VB does not support catch declarations without an identifier in them
Return False
End Function
End Class
End Namespace
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册