Skip to content

Commit 1b16a1c

Browse files
Lift computation out
1 parent 1482a74 commit 1b16a1c

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/Features/Core/Portable/ExtractMethod/MethodExtractor.Analyzer.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,10 @@ private void GenerateVariableInfoMap(
535535
candidates.UnionWith(writtenInsideMap);
536536
candidates.UnionWith(variableDeclaredMap);
537537

538+
// Need to analyze from the start of what we're extracting to the end of the scope that this variable could
539+
// have been referenced in.
540+
var analysisRange = TextSpan.FromBounds(SelectionResult.FinalSpan.Start, SelectionResult.GetContainingScope().Span.End);
541+
538542
foreach (var symbol in candidates)
539543
{
540544
// We don't care about the 'this' parameter. It will be available to an extracted method already.
@@ -588,7 +592,7 @@ private void GenerateVariableInfoMap(
588592
continue;
589593
}
590594

591-
var type = GetSymbolType(model, symbol);
595+
var type = GetSymbolType(model, symbol, analysisRange);
592596
if (type == null)
593597
{
594598
continue;
@@ -707,7 +711,10 @@ private bool SelectionContainsOnlyIdentifierWithSameType(ITypeSymbol type)
707711
return type.Equals(SelectionResult.GetContainingScopeType());
708712
}
709713

710-
private ITypeSymbol? GetSymbolType(SemanticModel semanticModel, ISymbol symbol)
714+
private ITypeSymbol? GetSymbolType(
715+
SemanticModel semanticModel,
716+
ISymbol symbol,
717+
TextSpan analysisRange)
711718
{
712719
var type = symbol switch
713720
{
@@ -728,17 +735,13 @@ private bool SelectionContainsOnlyIdentifierWithSameType(ITypeSymbol type)
728735

729736
var selectionOperation = semanticModel.GetOperation(SelectionResult.GetContainingScope());
730737

731-
// Need to analyze from the start of what we're extracting to the end of the scope that this variable could
732-
// have been referenced in.
733-
var span = TextSpan.FromBounds(SelectionResult.FinalSpan.Start, SelectionResult.GetContainingScope().Span.End);
734-
735738
// For Extract-Method we don't care about analyzing the declaration of this variable. For example, even if
736739
// it was initially assigned 'null' for the purposes of determining the type of it for a return value, all
737740
// we care is if it is null at the end of the selection. If it is only assigned non-null values, for
738741
// example, we want to treat it as non-null.
739742
if (selectionOperation is not null &&
740743
NullableHelpers.IsSymbolAssignedPossiblyNullValue(
741-
this.SemanticFacts, semanticModel, selectionOperation, symbol, span, includeDeclaration: false, this.CancellationToken) == false)
744+
this.SemanticFacts, semanticModel, selectionOperation, symbol, analysisRange, includeDeclaration: false, this.CancellationToken) == false)
742745
{
743746
return type.WithNullableAnnotation(NullableAnnotation.NotAnnotated);
744747
}

0 commit comments

Comments
 (0)