Browse Source

[CodeIssues] Don't crash in StatementToInitializerConverter on assignments to members.

(Probably) fixes Xamarin bug #6810.
newNRvisualizers
Simon Lindgren 13 years ago
parent
commit
13f67510b6
  1. 2
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertToInitializer/InitializerPath.cs
  2. 9
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertToInitializer/StatementsToInitializerConverter.cs
  3. 65
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertToInitializer/ConvertToInitializerTests.cs

2
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertToInitializer/InitializerPath.cs

@ -105,8 +105,6 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -105,8 +105,6 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
return true;
}
public int Level { get { return MemberPath.Count + 1; } }
object anchor;
public IVariable VariableRoot {

9
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertToInitializer/StatementsToInitializerConverter.cs

@ -244,11 +244,12 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -244,11 +244,12 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
if (leftPath == null) {
return false;
}
// Move replacement annotations over, in case this is the second assignment
// to the same variable.
// to the same variable/member.
AddOldAnnotationsToInitializer(leftPath, initializer);
if (leftResolveResult is LocalResolveResult) {
if (leftPath.MemberPath.Count == 0) {
ReplacementNodeHelper.AddReplacementAnnotation(initializer, node);
initializers [leftPath] = initializer;
return true;
@ -256,7 +257,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -256,7 +257,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
if (!(leftResolveResult is MemberResolveResult))
return false;
Debug.Assert(leftPath.Level > 1, "No top level assignment should get here.");
Debug.Assert(leftPath.MemberPath.Count > 0, "No top level assignment should get here.");
var parentKey = leftPath.GetParentPath();
var member = leftPath.MemberPath.Last();
@ -389,7 +390,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -389,7 +390,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
public static void AddReplacementAnnotation(AstNode node, AstNode replacedNode)
{
node.AddAnnotation(new ReplacementNodeAnnotation() {
node.AddAnnotation(new ReplacementNodeAnnotation {
ReplacedNode = replacedNode
});
}

65
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertToInitializer/ConvertToInitializerTests.cs

@ -64,7 +64,7 @@ class TestClass @@ -64,7 +64,7 @@ class TestClass
void F ()
{
";
[Test]
public void SimpleObject()
{
@ -197,8 +197,7 @@ class TestClass @@ -197,8 +197,7 @@ class TestClass
}
public System.Collections.Generic.IList<TestClass> Children;
}"
);
}");
}
[Test]
@ -215,10 +214,9 @@ class TestClass @@ -215,10 +214,9 @@ class TestClass
Property = ""Value""
};
}
}"
);
}");
}
[Test]
public void MemberAssignment()
{
@ -231,8 +229,23 @@ class TestClass @@ -231,8 +229,23 @@ class TestClass
Property = ""Value""
};
}
}"
);
}");
}
[Test]
public void MultipleMemberAssignments()
{
Test<ConvertToInitializerAction>(baseText + @"
Nested = new Test$Class ();
Nested = new TestClass ();
Nested.Property = ""Value"";
}
}", baseText + @"
Nested = new TestClass () {
Property = ""Value""
};
}
}");
}
[Test]
@ -251,8 +264,7 @@ class TestClass @@ -251,8 +264,7 @@ class TestClass
}
};
}
}"
);
}");
}
[Test]
@ -277,8 +289,7 @@ class TestClass @@ -277,8 +289,7 @@ class TestClass
Property = ""Value""
};
}
}"
);
}");
}
[Test]
@ -303,8 +314,7 @@ class TestClass @@ -303,8 +314,7 @@ class TestClass
}
};
}
}"
);
}");
}
[Test]
@ -326,8 +336,7 @@ class TestClass @@ -326,8 +336,7 @@ class TestClass
}
};
}
}"
);
}");
}
[Test]
@ -345,8 +354,7 @@ class TestClass @@ -345,8 +354,7 @@ class TestClass
Nested = new TestClass ()
};
}
}"
);
}");
}
[Test]
@ -369,8 +377,7 @@ class TestClass @@ -369,8 +377,7 @@ class TestClass
Nested = new TestClass ()
};
}
}"
);
}");
}
[Test]
@ -391,8 +398,7 @@ class TestClass @@ -391,8 +398,7 @@ class TestClass
System.Console.WriteLine("""");
variable.Nested.Property = ""NestedValue"";
}
}"
);
}");
}
[Test]
@ -411,8 +417,7 @@ class TestClass @@ -411,8 +417,7 @@ class TestClass
};
variable.Nested.Property = variable.ToString();
}
}"
);
}");
}
[Test]
@ -431,8 +436,7 @@ class TestClass @@ -431,8 +436,7 @@ class TestClass
var nested = new TestClass(variable);
variable.Nested = nested;
}
}"
);
}");
}
[Test]
@ -554,8 +558,7 @@ class TestClass @@ -554,8 +558,7 @@ class TestClass
Nested.Nested.Property = Nested.Property;
Nested.Nested.Nested = new TestClass();
}
}"
);
}");
}
[Test]
@ -589,8 +592,7 @@ class TestClass @@ -589,8 +592,7 @@ class TestClass
// This should stay here
item = new TestClass();
}
}"
);
}");
}
[Test]
@ -632,8 +634,7 @@ class TestClass @@ -632,8 +634,7 @@ class TestClass
System.Console.$WriteLine("""");
var variable = new TestClass();
}
}"
);
}");
}
[Test]

Loading…
Cancel
Save