private static async Task <StatementContainer> RefactorAsync( Document document, StatementContainer container, StatementSyntax statement, StatementSyntax newStatement, int index, int count, SemanticModel semanticModel, CancellationToken cancellationToken, bool removeReturnStatement) { ReturnStatementSyntax returnStatement = FindReturnStatementBelow(container.Statements, index); ExpressionSyntax expression = returnStatement.Expression; ExpressionSyntax newExpression = null; ISymbol symbol = semanticModel.GetSymbol(expression, cancellationToken); if (symbol.IsLocal() && index > 0) { LocalDeclarationStatementSyntax localDeclarationStatement = FindLocalDeclarationStatementAbove(container.Statements, index); if (localDeclarationStatement?.ContainsDiagnostics == false && !localDeclarationStatement.SpanOrTrailingTriviaContainsDirectives() && !statement.GetLeadingTrivia().Any(f => f.IsDirective)) { SeparatedSyntaxList <VariableDeclaratorSyntax> declarators = localDeclarationStatement.Declaration.Variables; VariableDeclaratorSyntax declarator = FindVariableDeclarator(semanticModel, symbol, declarators, cancellationToken); if (declarator != null) { ExpressionSyntax value = declarator.Initializer?.Value; if (removeReturnStatement || value != null) { IEnumerable <ReferencedSymbol> referencedSymbols = await SymbolFinder.FindReferencesAsync(symbol, document.Solution(), cancellationToken).ConfigureAwait(false); if (referencedSymbols.First().Locations.Count() == count + 1) { newExpression = value; if (declarators.Count == 1) { container = container.RemoveNode(localDeclarationStatement, RemoveHelper.GetRemoveOptions(localDeclarationStatement)); index--; } else { container = container.ReplaceNode(localDeclarationStatement, localDeclarationStatement.RemoveNode(declarator, RemoveHelper.GetRemoveOptions(declarator))); } returnStatement = FindReturnStatementBelow(container.Statements, index); } } } } } if (removeReturnStatement) { container = container.RemoveNode(returnStatement, RemoveHelper.GetRemoveOptions(returnStatement)); } else if (newExpression != null) { container = container.ReplaceNode(returnStatement, returnStatement.WithExpression(newExpression.WithTriviaFrom(expression))); } return(container.ReplaceNode(container.Statements[index], newStatement)); }