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

Do not report unused value assignment for VB static local assignment and...

Do not report unused value assignment for VB static local assignment and unused parameter for VB Handles method parameters.
上级 98286227
......@@ -63,5 +63,17 @@ $"Class C
End Class", parameters:=Nothing,
Diagnostic(IDEDiagnosticIds.UnusedParameterDiagnosticId))
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedParameters)>
Public Async Function ParameterOfMethodThatHandlesEvent() As Task
Await TestDiagnosticMissingAsync(
$"Public Class C
Public Event E(p As Integer)
Dim WithEvents field As New C
Public Sub M([|p|] As Integer) Handles field.E
End Sub
End Class")
End Function
End Class
End Namespace
......@@ -339,6 +339,22 @@ Class C
[|i|] = 1
Next
End Sub
End Class")
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedValues)>
Public Async Function StaticLocals() As Task
Await TestMissingInRegularAndScriptAsync(
$"Class C
Function Increment() As Boolean
Static count As Integer = 0
If count > 10 Then
Return True
End If
[|count|] = count + 1
Return False
End Function
End Class")
End Function
End Class
......
......@@ -22,6 +22,9 @@ protected override Option<CodeStyleOption<UnusedValuePreference>> UnusedValueAss
protected override bool SupportsDiscard(SyntaxTree tree)
=> ((CSharpParseOptions)tree.Options).LanguageVersion >= LanguageVersion.CSharp7;
protected override bool MethodHasHandlesClause(IMethodSymbol method)
=> false;
// C# does not have an explicit "call" statement syntax for invocations with explicit value discard.
protected override bool IsCallStatement(IExpressionStatementOperation expressionStatement)
=> false;
......
......@@ -366,8 +366,15 @@ private void AnalyzeOperationBlockEnd(OperationBlockAnalysisContext context)
out ImmutableDictionary<string, string> properties)
{
properties = null;
// Bail out in following cases:
// 1. End user has configured the diagnostic to be suppressed.
// 2. Symbol has error type, hence the diagnostic could be noised
// 3. Symbol is static. For example, assignment to static locals in VB
// is not unnecessary as the assigned value can be used on the next invocation.
if (_options.UnusedValueAssignmentSeverity == ReportDiagnostic.Suppress ||
symbol.GetSymbolType().IsErrorType())
symbol.GetSymbolType().IsErrorType() ||
symbol.IsStatic)
{
return false;
}
......
......@@ -179,7 +179,8 @@ private bool IsUnusedParameterCandidate(IParameterSymbol parameter)
method.IsOverride ||
!method.ExplicitOrImplicitInterfaceImplementations().IsEmpty ||
method.IsAccessor() ||
method.IsAnonymousFunction())
method.IsAnonymousFunction() ||
_compilationAnalyzer.MethodHasHandlesClause(method))
{
return false;
}
......
......@@ -88,6 +88,7 @@ protected AbstractRemoveUnusedParametersAndValuesDiagnosticAnalyzer()
protected abstract Location GetDefinitionLocationToFade(IOperation unusedDefinition);
protected abstract bool SupportsDiscard(SyntaxTree tree);
protected abstract bool MethodHasHandlesClause(IMethodSymbol method);
protected abstract Option<CodeStyleOption<UnusedValuePreference>> UnusedValueExpressionStatementOption { get; }
protected abstract Option<CodeStyleOption<UnusedValuePreference>> UnusedValueAssignmentOption { get; }
......
......@@ -29,6 +29,12 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.RemoveUnusedParametersAndValues
Return False
End Function
Protected Overrides Function MethodHasHandlesClause(method As IMethodSymbol) As Boolean
Return method.DeclaringSyntaxReferences().Any(Function(decl)
Return TryCast(decl.GetSyntax(), MethodStatementSyntax)?.HandlesClause IsNot Nothing
End Function)
End Function
Protected Overrides Function IsCallStatement(expressionStatement As IExpressionStatementOperation) As Boolean
Return TryCast(expressionStatement.Syntax, CallStatementSyntax) IsNot Nothing
End Function
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册