From 6c3bfeda802f55040624f0de33f644b46a7d9b6f Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Tue, 2 Jan 2018 19:02:51 -0800 Subject: [PATCH] AddParameter should handle and bail out on omitted argument (#23933) --- .../AddParameter/AddParameterTests.vb | 19 +++++++++++++++++++ .../AbstractAddParameterCodeFixProvider.cs | 12 ++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/EditorFeatures/VisualBasicTest/AddParameter/AddParameterTests.vb b/src/EditorFeatures/VisualBasicTest/AddParameter/AddParameterTests.vb index 1d1985ef571..6253ab14048 100644 --- a/src/EditorFeatures/VisualBasicTest/AddParameter/AddParameterTests.vb +++ b/src/EditorFeatures/VisualBasicTest/AddParameter/AddParameterTests.vb @@ -13,6 +13,25 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.AddParameter Return (Nothing, New VisualBasicAddParameterCodeFixProvider()) End Function + + + Public Async Function TestMissingOnOmittedArgument() As Task + Await TestMissingAsync( +"Public Module Module1 + Private Class C + Public Sub New(Arg1 As Integer) + End Sub + + Public Sub New(Arg1 As Integer, Arg2 As Integer) + End Sub + End Class + + Public Sub Main() + Dim x = New [|C|](, 0) + End Sub +End Module") + End Function + Public Async Function TestMissingWithImplicitConstructor() As Task Await TestMissingAsync( diff --git a/src/Features/Core/Portable/AddParameter/AbstractAddParameterCodeFixProvider.cs b/src/Features/Core/Portable/AddParameter/AbstractAddParameterCodeFixProvider.cs index fa25ad5d739..ac408bc0357 100644 --- a/src/Features/Core/Portable/AddParameter/AbstractAddParameterCodeFixProvider.cs +++ b/src/Features/Core/Portable/AddParameter/AbstractAddParameterCodeFixProvider.cs @@ -442,10 +442,14 @@ private int NonParamsParameterCount(IMethodSymbol method) // Now check the type of the argument versus the type of the parameter. If they // don't match, then this is the argument we should make the parameter for. - var expressionOfArgumment = syntaxFacts.GetExpressionOfArgument(argument); - var argumentTypeInfo = semanticModel.GetTypeInfo(expressionOfArgumment); - var isNullLiteral = syntaxFacts.IsNullLiteralExpression(expressionOfArgumment); - var isDefaultLiteral = syntaxFacts.IsDefaultLiteralExpression(expressionOfArgumment); + var expressionOfArgument = syntaxFacts.GetExpressionOfArgument(argument); + if (expressionOfArgument is null) + { + return null; + } + var argumentTypeInfo = semanticModel.GetTypeInfo(expressionOfArgument); + var isNullLiteral = syntaxFacts.IsNullLiteralExpression(expressionOfArgument); + var isDefaultLiteral = syntaxFacts.IsDefaultLiteralExpression(expressionOfArgument); if (argumentTypeInfo.Type == null && argumentTypeInfo.ConvertedType == null) { -- GitLab