Skip to content

Commit

Permalink
Fix exception in declare-as-nullable code fix (#44338)
Browse files Browse the repository at this point in the history
IParameterSymbol.DeclaringSyntaxReferences will be empty if the analyzed
method call is targeting a method with no source code available (external assembly).
  • Loading branch information
dymanoid committed May 27, 2020
1 parent dfdc4ef commit 8c62f62
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,22 @@ void M2(string? x) { }
}", parameters: s_nullableFeature);
}

[Fact]
[WorkItem(44338, "https://github.com/dotnet/roslyn/issues/44338")]
public async Task NoFixInvocationOfExternalMethod_NamedArgument()
{
await TestMissingInRegularAndScriptAsync(
@"#nullable enable
class Program
{
void M()
{
var list = new System.Collections.Generic.List<string>();
list.Add(item: [|null|]);
}
}", parameters: s_nullableFeature);
}

[Fact]
public async Task FixInvocation_NamedArgument_OutOfOrder()
{
Expand All @@ -856,6 +872,22 @@ void M2(int i, string? x) { }
}", parameters: s_nullableFeature);
}

[Fact]
[WorkItem(44338, "https://github.com/dotnet/roslyn/issues/44338")]
public async Task NoFixInvocationOfExternalMethod_NamedArgument_OutOfOrder()
{
await TestMissingInRegularAndScriptAsync(
@"#nullable enable
class Program
{
void M()
{
var dict = new System.Collections.Generic.Dictionary<string, int>();
dict.Add(value: 0, key: [|null|]);
}
}", parameters: s_nullableFeature);
}

[Fact]
public async Task FixInvocation_NamedArgument_Partial()
{
Expand Down Expand Up @@ -896,6 +928,22 @@ void M2(string? x) { }
}", parameters: s_nullableFeature);
}

[Fact]
[WorkItem(44338, "https://github.com/dotnet/roslyn/issues/44338")]
public async Task NoFixInvocationOfExternalMethod_PositionArgument()
{
await TestMissingInRegularAndScriptAsync(
@"#nullable enable
class Program
{
void M()
{
var list = new System.Collections.Generic.List<string>();
list.Add([|null|]);
}
}", parameters: s_nullableFeature);
}

[Fact]
public async Task FixInvocation_PositionArgument_SecondPosition()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ declarator.Parent is VariableDeclarationSyntax declaration &&
static TypeSyntax? TryGetParameterTypeSyntax(IParameterSymbol? parameterSymbol)
{
if (parameterSymbol is object &&
parameterSymbol.DeclaringSyntaxReferences[0].GetSyntax() is ParameterSyntax parameterSyntax &&
parameterSymbol.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is ParameterSyntax parameterSyntax &&
parameterSymbol.ContainingSymbol is IMethodSymbol method &&
method.GetAllMethodSymbolsOfPartialParts().Length == 1)
{
Expand Down

0 comments on commit 8c62f62

Please sign in to comment.