提交 02093472 编写于 作者: M Manish Vasani

Fix to unused value assignment analyzer for #31583

1. Do not offer remove unused local assignment for locals with error type
2. Make sure that this code fix is ordered after Add usings code fix, in case there are more similar unknown cases, by adding the ExtensionOrder attribute

Fixes #31583
上级 ac724330
......@@ -7,6 +7,7 @@
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.RemoveUnusedParametersAndValues
......@@ -5777,5 +5778,21 @@ bool M2(out char x)
}
}", options: PreferDiscard);
}
[WorkItem(31583, "https://github.com/dotnet/roslyn/issues/31583")]
[Theory, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedValues)]
[InlineData(nameof(PreferDiscard))]
[InlineData(nameof(PreferUnusedLocal))]
public async Task MissingImports(string optionName)
{
await TestMissingInRegularAndScriptAsync(
@"class C
{
void M()
{
List<int> [|x|] = null;
}
}", optionName);
}
}
}
// 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 Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Syntax;
......@@ -11,6 +12,7 @@
namespace Microsoft.CodeAnalysis.CSharp.RemoveUnusedParametersAndValues
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
[ExtensionOrder(After = PredefinedCodeFixProviderNames.AddImport)]
internal class CSharpRemoveUnusedParametersAndValuesDiagnosticAnalyzer : AbstractRemoveUnusedParametersAndValuesDiagnosticAnalyzer
{
protected override Option<CodeStyleOption<UnusedValuePreference>> UnusedValueExpressionStatementOption
......
......@@ -366,7 +366,8 @@ private void AnalyzeOperationBlockEnd(OperationBlockAnalysisContext context)
out ImmutableDictionary<string, string> properties)
{
properties = null;
if (_options.UnusedValueAssignmentSeverity == ReportDiagnostic.Suppress)
if (_options.UnusedValueAssignmentSeverity == ReportDiagnostic.Suppress ||
symbol.GetSymbolType().IsErrorType())
{
return false;
}
......
' 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.CodeStyle
Imports Microsoft.CodeAnalysis.Diagnostics
Imports Microsoft.CodeAnalysis.Operations
......@@ -11,6 +12,7 @@ Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Namespace Microsoft.CodeAnalysis.VisualBasic.RemoveUnusedParametersAndValues
<DiagnosticAnalyzer(LanguageNames.VisualBasic)>
<ExtensionOrder(After:=PredefinedCodeFixProviderNames.AddImport)>
Friend NotInheritable Class VisualBasicRemoveUnusedParametersAndValuesDiagnosticAnalyzer
Inherits AbstractRemoveUnusedParametersAndValuesDiagnosticAnalyzer
Protected Overrides ReadOnly Property UnusedValueExpressionStatementOption As [Option](Of CodeStyleOption(Of UnusedValuePreference))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册