Browse Source

[CodeIssues] Fix some false suggestions in VariableDeclaredInWideScopeIssue.

newNRvisualizers
Simon Lindgren 13 years ago
parent
commit
247d58818f
  1. 11
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/VariableDeclaredInWideScopeIssue.cs
  2. 40
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/VariableDeclaredInWideScopeTests.cs

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

@ -51,17 +51,22 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -51,17 +51,22 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
this.context = context;
}
static IList<Type> loopStatements = new List<Type>() {
static IList<Type> blockingStatements = new List<Type>() {
typeof(WhileStatement),
typeof(ForeachStatement),
typeof(ForStatement),
typeof(DoWhileStatement)
typeof(DoWhileStatement),
typeof(TryCatchStatement)
};
public override void VisitVariableDeclarationStatement(VariableDeclarationStatement variableDeclarationStatement)
{
base.VisitVariableDeclarationStatement(variableDeclarationStatement);
if (!(variableDeclarationStatement.Parent is BlockStatement))
// We are somewhere weird, like a the ResourceAquisition of a using statement
return;
if (variableDeclarationStatement.Variables.Count > 1)
return;
@ -81,7 +86,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -81,7 +86,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var path = GetPath(rootNode, lowestCommonAncestor);
var firstLoopStatement = (from node in path
where loopStatements.Contains(node.GetType())
where blockingStatements.Contains(node.GetType())
select node).FirstOrDefault();
IList<AstNode> possibleDestinationsPath;
if (firstLoopStatement == null) {

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

@ -243,6 +243,46 @@ class A @@ -243,6 +243,46 @@ class A
val = 3;
}
}
}";
TestRefactoringContext context;
var issues = GetIssues(new VariableDeclaredInWideScopeIssue(), input, out context);
Assert.AreEqual(0, issues.Count);
}
[Test]
public void DoesNotSuggestMovingIntoTryCatch()
{
var input = @"
class A
{
void F()
{
int val = 2;
try {
System.Console.WriteLine(val);
} catch {
throw;
}
}
}";
TestRefactoringContext context;
var issues = GetIssues(new VariableDeclaredInWideScopeIssue(), input, out context);
Assert.AreEqual(0, issues.Count);
}
[Test]
public void DoesNotSuggestMovingIntoBodyOfUsing()
{
var input = @"
using System.IO;
class A
{
void F()
{
using (FileStream fs = new FileStream("""", FileMode.Open, FileAccess.Read, FileShare.Read)) {
fs.Read(null, 0, 0);
}
}
}";
TestRefactoringContext context;
var issues = GetIssues(new VariableDeclaredInWideScopeIssue(), input, out context);

Loading…
Cancel
Save