Browse Source

VariableDeclaredInWideScopeIssue no longer suggests moving method call

initializers.
pull/32/merge
Mike Krüger 13 years ago
parent
commit
551869bd41
  1. 31
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/VariableDeclaredInWideScopeIssue.cs
  2. 68
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/VariableDeclaredInWideScopeTests.cs

31
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/VariableDeclaredInWideScopeIssue.cs

@ -63,7 +63,33 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
typeof(LambdaExpression), typeof(LambdaExpression),
typeof(LockStatement) typeof(LockStatement)
}; };
class CheckInitializer : DepthFirstAstVisitor
{
public bool IsValid {
get;
private set;
}
public CheckInitializer()
{
IsValid = true;
}
public override void VisitInvocationExpression(InvocationExpression invocationExpression)
{
base.VisitInvocationExpression(invocationExpression);
IsValid = false;
}
}
bool CheckForInvocations(Expression initializer)
{
var visitor = new CheckInitializer();
initializer.AcceptVisitor(visitor);
return visitor.IsValid;
}
public override void VisitVariableDeclarationStatement(VariableDeclarationStatement variableDeclarationStatement) public override void VisitVariableDeclarationStatement(VariableDeclarationStatement variableDeclarationStatement)
{ {
base.VisitVariableDeclarationStatement(variableDeclarationStatement); base.VisitVariableDeclarationStatement(variableDeclarationStatement);
@ -84,6 +110,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
// variable is not used // variable is not used
return; return;
if (!CheckForInvocations(variableInitializer.Initializer))
return;
AstNode deepestCommonAncestor = GetDeepestCommonAncestor(rootNode, identifiers); AstNode deepestCommonAncestor = GetDeepestCommonAncestor(rootNode, identifiers);
var path = GetPath(rootNode, deepestCommonAncestor); var path = GetPath(rootNode, deepestCommonAncestor);

68
ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/VariableDeclaredInWideScopeTests.cs

@ -187,7 +187,7 @@ class A
public void IfElseIf() public void IfElseIf()
{ {
TestStatements(@" TestStatements(@"
int val = GetValue (); int val = 12;
if (condition) { if (condition) {
int val2 = GetValue (); int val2 = GetValue ();
Console.WriteLine(val2); Console.WriteLine(val2);
@ -199,7 +199,7 @@ class A
int val2 = GetValue (); int val2 = GetValue ();
Console.WriteLine(val2); Console.WriteLine(val2);
} else if (!condition) { } else if (!condition) {
int val = GetValue (); int val = 12;
Console.WriteLine(val); Console.WriteLine(val);
} }
"); ");
@ -302,58 +302,18 @@ class A
"); ");
} }
[Test]
public void SuggestsMovingMethodCallInitializer ()
{
TestStatements(@"
int j = GetValue ();
if (true) {
Console.WriteLine(j);
}
", 1, @"
if (true) {
int j = GetValue ();
Console.WriteLine(j);
}
");
}
[Test]
public void DoesNotReorderMethodCalls ()
{
TestStatements(@"
int k = GetValue ();
if (b > 3) {
if (a < 2) {
// This should block any move past this point
SetValue (2);
}
Console.WriteLine(k);
}
", 1, @"
if (b > 3) {
int k = GetValue ();
if (a < 2) {
// This should block any move past this point
SetValue (2);
}
Console.WriteLine(k);
}
");
}
[Test] [Test]
public void CommonParentStatement () public void CommonParentStatement ()
{ {
TestStatements(@" TestStatements(@"
int k = GetValue (); int k = 12;
{ {
SetValue (k); SetValue (k);
SetValue (k); SetValue (k);
} }
", 1, @" ", 1, @"
{ {
int k = GetValue (); int k = 12;
SetValue (k); SetValue (k);
SetValue (k); SetValue (k);
} }
@ -364,7 +324,7 @@ class A
public void CaseTarget () public void CaseTarget ()
{ {
TestStatements(@" TestStatements(@"
int j = GetValue (); int j = 12;
switch (a) { switch (a) {
case 2: case 2:
SetValue (j + 1); SetValue (j + 1);
@ -373,7 +333,7 @@ class A
", 1, @" ", 1, @"
switch (a) { switch (a) {
case 2: case 2:
int j = GetValue (); int j = 12;
SetValue (j + 1); SetValue (j + 1);
break; break;
} }
@ -395,14 +355,14 @@ class A
public void DoesNotExpandForStatement () public void DoesNotExpandForStatement ()
{ {
TestStatements(@" TestStatements(@"
var val = GetValue (); var val = 12;
if (false) { if (false) {
for (var i = GetValue (); ; i++) for (var i = GetValue (); ; i++)
System.Console.WriteLine (val); System.Console.WriteLine (val);
} }
", 1, @" ", 1, @"
if (false) { if (false) {
var val = GetValue (); var val = 12;
for (var i = GetValue (); ; i++) for (var i = GetValue (); ; i++)
System.Console.WriteLine (val); System.Console.WriteLine (val);
} }
@ -453,6 +413,18 @@ class A
Assert.AreEqual(0, issues.Count); Assert.AreEqual(0, issues.Count);
} }
[Test]
public void DoesNotSuggestMovingMethodCallInitializer()
{
// Method calls can have side effects therefore it's generally not possible to move them.
TestStatements(@"
int val = GetValue ();
if (true) {
System.Console.WriteLine(val);
}
");
}
} }

Loading…
Cancel
Save