From 5d36b856f8f1c872f988bed0b29c4113599de316 Mon Sep 17 00:00:00 2001 From: Martin Strecker Date: Thu, 18 May 2017 14:10:40 +0200 Subject: [PATCH] Removed check for NullLiteralExpression --- .../RemoveUnnecessaryCastTests.cs | 23 ++++++++ .../CastExpressionSyntaxExtensions.cs | 55 +++++++++---------- 2 files changed, 49 insertions(+), 29 deletions(-) diff --git a/src/EditorFeatures/CSharpTest/Diagnostics/RemoveUnnecessaryCast/RemoveUnnecessaryCastTests.cs b/src/EditorFeatures/CSharpTest/Diagnostics/RemoveUnnecessaryCast/RemoveUnnecessaryCastTests.cs index bbce6a67a53..06895564512 100644 --- a/src/EditorFeatures/CSharpTest/Diagnostics/RemoveUnnecessaryCast/RemoveUnnecessaryCastTests.cs +++ b/src/EditorFeatures/CSharpTest/Diagnostics/RemoveUnnecessaryCast/RemoveUnnecessaryCastTests.cs @@ -3913,5 +3913,28 @@ static void Main(dynamic p) } }"); } + + [WorkItem(18978, "https://github.com/dotnet/roslyn/issues/18978")] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)] + public async Task DontRemoveCastCallToMethodWithParamsArgs() + { + await TestMissingInRegularAndScriptAsync( +@" +class Program +{ + public static void Main(string[] args) + { + var takesArgs = new[] { ""Hello"", ""World"" }; + TakesParams(takesArgs); + TakesParams([|(object)takesArgs|]); + } + + private static void TakesParams(params object[] foo) + { + Console.WriteLine(foo.Length); + } +}"); + } + } } \ No newline at end of file diff --git a/src/Workspaces/CSharp/Portable/Extensions/CastExpressionSyntaxExtensions.cs b/src/Workspaces/CSharp/Portable/Extensions/CastExpressionSyntaxExtensions.cs index 3ed07d393eb..51952270688 100644 --- a/src/Workspaces/CSharp/Portable/Extensions/CastExpressionSyntaxExtensions.cs +++ b/src/Workspaces/CSharp/Portable/Extensions/CastExpressionSyntaxExtensions.cs @@ -161,43 +161,40 @@ private static bool UserDefinedConversionIsAllowed(ExpressionSyntax expression, // // Foo((object)null); - if (cast.Expression.WalkDownParentheses().IsKind(SyntaxKind.NullLiteralExpression)) + var argument = cast.WalkUpParentheses().Parent as ArgumentSyntax; + if (argument != null) { - var argument = cast.WalkUpParentheses().Parent as ArgumentSyntax; - if (argument != null) + // If there are any arguments to the right, we can assume that this is not a + // *single* argument passed to a params parameter. + var argumentList = argument.Parent as BaseArgumentListSyntax; + if (argumentList != null) { - // If there are any arguments to the right, we can assume that this is not a - // *single* argument passed to a params parameter. - var argumentList = argument.Parent as BaseArgumentListSyntax; - if (argumentList != null) + var argumentIndex = argumentList.Arguments.IndexOf(argument); + if (argumentIndex < argumentList.Arguments.Count - 1) { - var argumentIndex = argumentList.Arguments.IndexOf(argument); - if (argumentIndex < argumentList.Arguments.Count - 1) - { - return false; - } + return false; } + } - var parameter = argument.DetermineParameter(semanticModel, cancellationToken: cancellationToken); - if (parameter != null && parameter.IsParams) - { - Debug.Assert(parameter.Type is IArrayTypeSymbol); + var parameter = argument.DetermineParameter(semanticModel, cancellationToken: cancellationToken); + if (parameter != null && parameter.IsParams) + { + Debug.Assert(parameter.Type is IArrayTypeSymbol); - var parameterType = (IArrayTypeSymbol)parameter.Type; + var parameterType = (IArrayTypeSymbol)parameter.Type; - var conversion = semanticModel.Compilation.ClassifyConversion(castType, parameterType); - if (conversion.Exists && - conversion.IsImplicit) - { - return false; - } + var conversion = semanticModel.Compilation.ClassifyConversion(castType, parameterType); + if (conversion.Exists && + conversion.IsImplicit) + { + return false; + } - var conversionElementType = semanticModel.Compilation.ClassifyConversion(castType, parameterType.ElementType); - if (conversionElementType.Exists && - conversionElementType.IsImplicit) - { - return true; - } + var conversionElementType = semanticModel.Compilation.ClassifyConversion(castType, parameterType.ElementType); + if (conversionElementType.Exists && + conversionElementType.IsImplicit) + { + return true; } } } -- GitLab