提交 0d9d6199 编写于 作者: A Andy Gocke 提交者: GitHub

Merge pull request #17150 from dotnet/post-dev15

Merge post-dev15 into master
......@@ -229,6 +229,7 @@
<Compile Include="Diagnostics\NamingStyles\NamingStylesTests_OptionSets.cs" />
<Compile Include="Diagnostics\PreferFrameworkType\PreferFrameworkTypeTests.cs" />
<Compile Include="Diagnostics\PreferFrameworkType\PreferFrameworkTypeTests_FixAllTests.cs" />
<Compile Include="Diagnostics\RemoveUnusedVariable\RemoveUnusedVariableTest.cs" />
<Compile Include="Extensions\ContextQuery\IsPossibleDeconstructionDesignationTests.cs" />
<Compile Include="Extensions\ContextQuery\PossibleTupleContextTests.cs" />
<Compile Include="QualifyMemberAccess\QualifyMemberAccessTests.cs" />
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.CodeFixes.RemoveUnusedVariable;
using Microsoft.CodeAnalysis.Diagnostics;
using Roslyn.Test.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics.RemoveUnusedVar
{
public partial class RemoveUnusedVariableTest : AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest
{
internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProviderAndFixer(Workspace workspace)
{
return(null, new RemoveUnusedVariableCodeFixProvider());
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedVariable)]
public async Task RemoveUnusedVariable()
{
await TestAsync(
@"class Class
{
void Method()
{
[|int a = 3;|]
}
}",
@"class Class
{
void Method()
{
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedVariable)]
public async Task RemoveUnusedVariable1()
{
await TestAsync(
@"class Class
{
void Method()
{
[|string a;|]
string b = "";
var c = b;
}
}",
@"class Class
{
void Method()
{
string b = "";
var c = b;
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedVariable)]
public async Task RemoveUnusedVariable3()
{
await TestAsync(
@"class Class
{
void Method()
{
[|string a;|]
}
}",
@"class Class
{
void Method()
{
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedVariable)]
public async Task RemoveUnusedVariableMultipleOnLine()
{
await TestAsync(
@"class Class
{
void Method()
{
[|string a|], b;
}
}",
@"class Class
{
void Method()
{
string b;
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedVariable)]
public async Task RemoveUnusedVariableMultipleOnLine1()
{
await TestAsync(
@"class Class
{
void Method()
{
string a, [|b|];
}
}",
@"class Class
{
void Method()
{
string a;
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedVariable)]
public async Task RemoveUnusedVariableFixAll()
{
await TestAsync(
@"class Class
{
void Method()
{
{|FixAllInDocument:string a;|}
string b;
}
}",
@"class Class
{
void Method()
{
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedVariable)]
public async Task RemoveUnusedVariableFixAll1()
{
await TestAsync(
@"class Class
{
void Method()
{
{|FixAllInDocument:string a;|}
string b, c;
}
}",
@"class Class
{
void Method()
{
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedVariable)]
public async Task RemoveUnusedVariableFixAll2()
{
await TestAsync(
@"class Class
{
void Method()
{
string a, {|FixAllInDocument:b|};
}
}",
@"class Class
{
void Method()
{
}
}");
}
}
}
......@@ -80,6 +80,7 @@ public static class Features
public const string CodeActionsRemoveByVal = "CodeActions.RemoveByVal";
public const string CodeActionsRemoveDocCommentNode = "CodeActions.RemoveDocCommentNode";
public const string CodeActionsRemoveUnnecessaryCast = "CodeActions.RemoveUnnecessaryCast";
public const string CodeActionsRemoveUnusedVariable = "CodeActions.RemoveUnusedVariable";
public const string CodeActionsRemoveUnnecessaryImports = "CodeActions.RemoveUnnecessaryImports";
public const string CodeActionsSimplifyTypeNames = "CodeActions.SimplifyTypeNames";
public const string CodeActionsSpellcheck = "CodeActions.Spellcheck";
......
......@@ -182,6 +182,7 @@
<Compile Include="CodeActions\ConvertIfToSwitch\ConvertIfToSwitchTests.vb" />
<Compile Include="CodeActions\ConvertNumericLiteral\ConvertNumericLiteralTests.vb" />
<Compile Include="CodeActions\UseNamedArguments\UseNamedArgumentsTests.vb" />
<Compile Include="Diagnostics\RemoveUnusedVariable\RemoveUnusedVariableTest.vb" />
<Compile Include="Structure\CollectionInitializerStructureProviderTests.vb" />
<Compile Include="ConvertToInterpolatedString\ConvertPlaceholderToInterpolatedStringTests.vb" />
<Compile Include="CodeActions\EncapsulateField\EncapsulateFieldTests.vb" />
......
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports Microsoft.CodeAnalysis.CodeFixes
Imports Microsoft.CodeAnalysis.Diagnostics
Imports Microsoft.CodeAnalysis.VisualBasic.CodeFixes.RemoveUnusedVariable
Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.Diagnostics.RemoveUnusedVariableTest
Partial Public Class RemoveUnusedVariableTest
Inherits AbstractVisualBasicDiagnosticProviderBasedUserDiagnosticTest
Friend Overrides Function CreateDiagnosticProviderAndFixer(workspace As Workspace) As (DiagnosticAnalyzer, CodeFixProvider)
Return (Nothing,
New RemoveUnusedVariableCodeFixProvider())
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedVariable)>
Public Async Function RemoveUnusedVariable() As Task
Dim markup =
<File>
Module M
Sub Main()
Dim [|x as String|]
End Sub
End Module
</File>
Dim expected =
<File>
Module M
Sub Main()
End Sub
End Module
</File>
Await TestAsync(markup, expected)
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedVariable)>
Public Async Function RemoveUnusedVariable1() As Task
Dim markup =
<File>
Module M
Sub Main()
Dim [|x|], c as String
End Sub
End Module
</File>
Dim expected =
<File>
Module M
Sub Main()
Dim c as String
End Sub
End Module
</File>
Await TestAsync(markup, expected)
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedVariable)>
Public Async Function RemoveUnusedVariableFixAll() As Task
Dim markup =
<File>
Module M
Sub Main()
Dim x, c as String
Dim {|FixAllInDocument:a as String|}
End Sub
End Module
</File>
Dim expected =
<File>
Module M
Sub Main()
End Sub
End Module
</File>
Await TestAsync(markup, expected)
End Function
End Class
End Namespace
......@@ -61,6 +61,7 @@
<Link>InternalUtilities\LambdaUtilities.cs</Link>
</Compile>
<Compile Include="AddPackage\CSharpAddSpecificPackageCodeFixProvider.cs" />
<Compile Include="CodeFixes\RemoveUnusedVariable\RemoveUnusedVariableCodeFixProvider.cs" />
<Compile Include="ConvertIfToSwitch\CSharpConvertIfToSwitchCodeRefactoringProvider.cs" />
<Compile Include="ConvertIfToSwitch\CSharpConvertIfToSwitchCodeRefactoringProvider.Pattern.cs" />
<Compile Include="ConvertNumericLiteral\CSharpConvertNumericLiteralCodeRefactoringProvider.cs" />
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Immutable;
using System.Composition;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CodeFixes.RemoveUnusedVariable;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Microsoft.CodeAnalysis.CSharp.CodeFixes.RemoveUnusedVariable
{
[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.RemoveUnusedVariable), Shared]
internal partial class RemoveUnusedVariableCodeFixProvider : AbstractRemoveUnusedVariableCodeFixProvider<LocalDeclarationStatementSyntax, VariableDeclaratorSyntax, VariableDeclarationSyntax>
{
private const string CS0168 = nameof(CS0168);
private const string CS0219 = nameof(CS0219);
public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CS0168, CS0219);
}
}
......@@ -36,6 +36,7 @@ internal static class PredefinedCodeFixProviderNames
public const string QualifyMemberAccess = nameof(QualifyMemberAccess);
public const string RemoveUnnecessaryCast = nameof(RemoveUnnecessaryCast);
public const string RemoveUnnecessaryImports = nameof(RemoveUnnecessaryImports);
public const string RemoveUnusedVariable = nameof(RemoveUnusedVariable);
public const string RenameTracking = nameof(RenameTracking);
public const string SimplifyNames = nameof(SimplifyNames);
public const string SpellCheck = nameof(SpellCheck);
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CodeFixes.RemoveUnusedVariable
{
internal abstract class AbstractRemoveUnusedVariableCodeFixProvider<TLocalDeclarationStatement, TVariableDeclarator, TVariableDeclaration> : SyntaxEditorBasedCodeFixProvider
where TLocalDeclarationStatement: SyntaxNode
where TVariableDeclarator: SyntaxNode
where TVariableDeclaration: SyntaxNode
{
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)
{
return;
}
}
context.RegisterCodeFix(
new MyCodeAction(c => FixAsync(context.Document, context.Diagnostics.First(), c)),
context.Diagnostics);
}
protected override Task FixAllAsync(Document document, ImmutableArray<Diagnostic> diagnostics, SyntaxEditor editor, CancellationToken cancellationToken)
{
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)
{
editor.RemoveNode(token.GetAncestor<TLocalDeclarationStatement>());
}
else if (variableDeclarators.Count() > 1)
{
editor.RemoveNode(variableDeclarator);
}
}
return SpecializedTasks.EmptyTask;
}
private class MyCodeAction : CodeAction.DocumentChangeAction
{
public MyCodeAction(Func<CancellationToken, Task<Document>> createChangedDocument) :
base(FeaturesResources.Remove_unused_variable, createChangedDocument, FeaturesResources.Remove_unused_variable)
{
}
}
}
}
......@@ -105,6 +105,7 @@
<Compile Include="AddPackage\InstallPackageDirectlyCodeActionOperation.cs" />
<Compile Include="AddPackage\InstallWithPackageManagerCodeAction.cs" />
<Compile Include="AddPackage\InstallPackageParentCodeAction.cs" />
<Compile Include="CodeFixes\RemoveUnusedVariable\AbstractRemoveUnusedVariableCodeFixProvider.cs" />
<Compile Include="ConvertIfToSwitch\AbstractConvertIfToSwitchCodeRefactoringProvider.cs" />
<Compile Include="ConvertNumericLiteral\AbstractConvertNumericLiteralCodeRefactoringProvider.cs" />
<Compile Include="CodeRefactorings\UseNamedArguments\AbstractUseNamedArgumentsCodeRefactoringProvider.cs" />
......
......@@ -2310,6 +2310,15 @@ internal class FeaturesResources {
}
}
/// <summary>
/// Looks up a localized string similar to Remove unused variable.
/// </summary>
internal static string Remove_unused_variable {
get {
return ResourceManager.GetString("Remove_unused_variable", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Removing &apos;{0}&apos; that accessed captured variables &apos;{1}&apos; and &apos;{2}&apos; declared in different scopes will prevent the debug session from continuing..
/// </summary>
......
......@@ -1154,6 +1154,9 @@ This version used in: {2}</value>
<data name="Add_argument_name_0" xml:space="preserve">
<value>Add argument name '{0}'</value>
</data>
<data name="Remove_unused_variable" xml:space="preserve">
<value>Remove unused variable</value>
</data>
<data name="Convert_to_binary" xml:space="preserve">
<value>Convert to binary</value>
</data>
......@@ -1175,4 +1178,4 @@ This version used in: {2}</value>
<data name="Remove_separators" xml:space="preserve">
<value>Remove separators</value>
</data>
</root>
\ No newline at end of file
</root>
......@@ -89,6 +89,7 @@
<Compile Include="CodeFixes\GenerateParameterizedMember\GenerateParameterizedMemberCodeFixProvider.vb" />
<Compile Include="CodeFixes\GenerateType\GenerateTypeCodeFixProvider.vb" />
<Compile Include="CodeFixes\GenerateVariable\GenerateVariableCodeFixProvider.vb" />
<Compile Include="CodeFixes\RemoveUnusedVariable\RemoveUnusedVariableCodeFixProvider.vb" />
<Compile Include="ConvertIfToSwitch\VisualBasicConvertIfToSwitchCodeRefactoringProvider.Pattern.vb" />
<Compile Include="ConvertIfToSwitch\VisualBasicConvertIfToSwitchCodeRefactoringProvider.vb" />
<Compile Include="ConvertNumericLiteral\VisualBasicConvertNumericLiteralCodeRefactoringProvider.vb" />
......
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System.Collections.Immutable
Imports System.Composition
Imports Microsoft.CodeAnalysis.CodeFixes
Imports Microsoft.CodeAnalysis.CodeFixes.RemoveUnusedVariable
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.RemoveUnusedVariable
<ExportCodeFixProviderAttribute(LanguageNames.VisualBasic, Name:=PredefinedCodeFixProviderNames.RemoveUnnecessaryCast), [Shared]>
Friend Class RemoveUnusedVariableCodeFixProvider
Inherits AbstractRemoveUnusedVariableCodeFixProvider(Of
LocalDeclarationStatementSyntax, ModifiedIdentifierSyntax, VariableDeclaratorSyntax)
Private Const BC42024 As String = "BC42024"
Private ReadOnly Ids As ImmutableArray(Of String) = ImmutableArray.Create(BC42024)
Public Overrides ReadOnly Property FixableDiagnosticIds As ImmutableArray(Of String)
Get
Return Ids
End Get
End Property
End Class
End Namespace
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册