Browse Source

RedundantAssignment issue is now more careful when removing

invocations.
pull/32/merge
Mike Krüger 13 years ago
parent
commit
69d4fd1ff1
  1. 28
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/RedundantAssignmentIssue.cs
  2. 16
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/RedundantAssignmentIssueTests.cs

28
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/RedundantAssignmentIssue.cs

@ -140,12 +140,40 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
ProcessNodes (startNode); ProcessNodes (startNode);
} }
class SearchInvocationsVisitor : DepthFirstAstVisitor
{
bool foundInvocations;
public bool ContainsInvocations(AstNode node)
{
foundInvocations = false;
node.AcceptVisitor (this);
return foundInvocations;
}
protected override void VisitChildren (AstNode node)
{
AstNode next;
for (var child = node.FirstChild; child != null && !foundInvocations; child = next) {
next = child.NextSibling;
child.AcceptVisitor (this);
}
}
public override void VisitInvocationExpression(InvocationExpression invocationExpression)
{
foundInvocations = true;
}
}
void AddIssue (AstNode node) void AddIssue (AstNode node)
{ {
var title = ctx.TranslateString ("Remove redundant assignment"); var title = ctx.TranslateString ("Remove redundant assignment");
var variableInitializer = node as VariableInitializer; var variableInitializer = node as VariableInitializer;
if (variableInitializer != null) { if (variableInitializer != null) {
if (new SearchInvocationsVisitor ().ContainsInvocations (variableInitializer.Initializer))
return;
AddIssue (variableInitializer.Initializer, title, AddIssue (variableInitializer.Initializer, title,
script => { script => {
var replacement = (VariableInitializer)node.Clone (); var replacement = (VariableInitializer)node.Clone ();

16
ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/RedundantAssignmentIssueTests.cs

@ -390,6 +390,22 @@ class TestClass
// nothing // nothing
} }
} }
}";
Test<RedundantAssignmentIssue> (input, 0);
}
[Test]
public void TestAssignmentWithFunction ()
{
var input = @"using System;
class TestClass
{
void Test(TestClass a) { }
TestClass Func () { return null; }
void TestMethod ()
{
var a = Func ();
}
}"; }";
Test<RedundantAssignmentIssue> (input, 0); Test<RedundantAssignmentIssue> (input, 0);
} }

Loading…
Cancel
Save