未验证 提交 42852730 编写于 作者: J Joey Robichaud 提交者: GitHub

Merge pull request #36818 from mavasani/UnusedParameterFixes

Fix couple of false positives in unused parameter analyzer
......@@ -1305,5 +1305,18 @@ public C(Task<IFoo> [|foo|])
public void Dispose() => foo.Result.Fooed -= fooed;
}", options);
}
[WorkItem(36817, "https://github.com/dotnet/roslyn/issues/36817")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedParameters)]
public async Task ParameterWithoutName_NoDiagnostic()
{
await TestDiagnosticMissingAsync(
@"public class C
{
public void M[|(int )|]
{
}
}");
}
}
}
......@@ -95,6 +95,22 @@ End Class")
$"Class C
[|Sub M(_0 As Integer, _1 As Char, _3 As C)|]
End Sub
End Class")
End Function
<WorkItem(36816, "https://github.com/dotnet/roslyn/issues/36816")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedParameters)>
Public Async Function PartialMethodParameter_NoDiagnostic() As Task
Await TestDiagnosticMissingAsync(
$"Class C
[|Partial Private Sub M(str As String)|]
End Sub
End Class
Partial Class C
Private Sub M(str As String)
Dim x = str.ToString()
End Sub
End Class")
End Function
End Class
......
......@@ -57,6 +57,12 @@ private sealed partial class SymbolStartAnalyzer
var deserializationConstructorCheck = new DeserializationConstructorCheck(context.Compilation);
context.RegisterSymbolStartAction(symbolStartContext =>
{
if (HasSyntaxErrors((INamedTypeSymbol)symbolStartContext.Symbol, symbolStartContext.CancellationToken))
{
// Bail out on syntax errors.
return;
}
// Create a new SymbolStartAnalyzer instance for every named type symbol
// to ensure there is no shared state (such as identified unused parameters within the type),
// as that would lead to duplicate diagnostics being reported from symbol end action callbacks
......@@ -64,6 +70,23 @@ private sealed partial class SymbolStartAnalyzer
var symbolAnalyzer = new SymbolStartAnalyzer(analyzer, eventsArgType, attributeSetForMethodsToIgnore, deserializationConstructorCheck);
symbolAnalyzer.OnSymbolStart(symbolStartContext);
}, SymbolKind.NamedType);
return;
// Local functions
static bool HasSyntaxErrors(INamedTypeSymbol namedTypeSymbol, CancellationToken cancellationToken)
{
foreach (var syntaxRef in namedTypeSymbol.DeclaringSyntaxReferences)
{
var syntax = syntaxRef.GetSyntax(cancellationToken);
if (syntax.GetDiagnostics().Any(d => d.Severity == DiagnosticSeverity.Error))
{
return true;
}
}
return false;
}
}
private void OnSymbolStart(SymbolStartAnalysisContext context)
......@@ -190,6 +213,7 @@ private bool IsUnusedParameterCandidate(IParameterSymbol parameter)
method.IsAbstract ||
method.IsVirtual ||
method.IsOverride ||
method.PartialImplementationPart != null ||
!method.ExplicitOrImplicitInterfaceImplementations().IsEmpty ||
method.IsAccessor() ||
method.IsAnonymousFunction() ||
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册