icsharpcode / NRefactory

NRefactory - Refactoring Your C# Code
684 stars 262 forks source link

ReplaceEmptyStringAction contains a critical bug #495

Closed jernejk closed 9 years ago

jernejk commented 9 years ago

Hi, I noticed that refactoring code bellow crashes Visual Studio while using NR6Pack library:

Console.WriteLine("");

Most likely cause of this bug is trying to replace node received from context.GetNode() which is probably returning ArgumentSyntax instead of StringLiteralSyntax. I'm doing a demo for Roslyn and I had this issue and I solved it by getting most inner node with same location:

SyntaxNode node = root.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);

I tried to fix this but I wasn't able to compile Test project and I'm familiar with framework used in here. This is my naive solution because I didn't found "getInnermostNodeForTie" equivalent functionality:

        static PrimitiveExpression GetEmptyString (RefactoringContext context)
        {
            var node = context.GetNode<PrimitiveExpression> ();
            if (node == null || !(node.Value is string) || node.Value.ToString () != "")
                return null;

            if (node.FirstChild.StartLocation.Column == node.StartLocation.Column)
            {
                var newNode = node.GetNextNode() as PrimitiveExpression;

                if (newNode is PrimitiveExpression)
                {
                    node = newNode as PrimitiveExpression;
                }
            }

            return  node;
        } 
Rpinski commented 9 years ago

Hi Jernej,

thanks for sharing this issue. One question: Which NR6Pack version did you test it with?

jernejk commented 9 years ago

It was from Visual Studio extension gallery for VS 2015 version 0.10.0.0. I noticed today version 1.0 was pushed and it works. :)

Thanks!