提交 8244abe1 编写于 作者: C Cyrus Najmabadi

Suppress refactoring if writing outside loop.

上级 4eb9ff1f
......@@ -70,6 +70,21 @@ class C
end class")
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertForToForEach)>
Public Async Function TestMissingIfReferencingNotDeclaringVariable() As Task
Await TestMissingInRegularAndScriptAsync(
"imports System
class C
sub Test(array as string())
dim i as integer
[||]For i = 0 to array.Length - 1 step 2
Console.WriteLine(array(i))
next
end sub
end class")
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertForToForEach)>
Public Async Function TestMissingWithIncorrectCondition1() As Task
Await TestMissingInRegularAndScriptAsync(
......
......@@ -93,6 +93,20 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
// If the for-variable is an identifier, then make sure it's declaring a variable
// at the for-statemnet, and not referencing some previously declared symbol. i.e
var iterationSymbol = semanticModel.GetSymbolInfo(iterationVariable.Parent, cancellationToken).GetAnySymbol();
if (iterationSymbol != null)
{
if (iterationSymbol.Locations.Length != 1 ||
!iterationSymbol.Locations[0].IsInSource ||
iterationVariable != iterationSymbol.Locations[0].FindToken(cancellationToken))
{
// was a reference to some other variable.
return;
}
}
// Make sure we're starting at 0.
var initializerValue = semanticModel.GetConstantValue(initializer, cancellationToken);
if (!(initializerValue.HasValue && initializerValue.Value is 0))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册