Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract a shared helper for walking statements for a few analyzers #69524

Merged
merged 3 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.Analyzers.UseCollectionInitializer;
using Microsoft.CodeAnalysis.LanguageService;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;

namespace Microsoft.CodeAnalysis.UseCollectionInitializer
{
Expand Down Expand Up @@ -99,19 +99,6 @@ public ImmutableArray<Match<TStatementSyntax>> Analyze(

protected override bool TryAddMatches(ArrayBuilder<Match<TStatementSyntax>> matches)
{
// If containing statement is inside a block (e.g. method), than we need to iterate through its child statements.
// If containing statement is in top-level code, than we need to iterate through child statements of containing compilation unit.
var containingBlockOrCompilationUnit = _containingStatement.GetRequiredParent();

// In case of top-level code parent of the statement will be GlobalStatementSyntax,
// so we need to get its parent in order to get CompilationUnitSyntax
if (_syntaxFacts.IsGlobalStatement(containingBlockOrCompilationUnit))
{
containingBlockOrCompilationUnit = containingBlockOrCompilationUnit.Parent!;
}

var foundStatement = false;

var seenInvocation = false;
var seenIndexAssignment = false;

Expand All @@ -135,35 +122,19 @@ protected override bool TryAddMatches(ArrayBuilder<Match<TStatementSyntax>> matc
if (seenIndexAssignment && _analyzeForCollectionExpression)
return false;

foreach (var child in containingBlockOrCompilationUnit.ChildNodesAndTokens())
foreach (var subsequentStatement in UseCollectionInitializerHelpers.GetSubsequentStatements(_syntaxFacts, _containingStatement))
{
if (child.IsToken)
continue;

var childNode = child.AsNode();
var extractedChild = _syntaxFacts.IsGlobalStatement(childNode) ? _syntaxFacts.GetStatementOfGlobalStatement(childNode) : childNode;

if (!foundStatement)
{
if (extractedChild == _containingStatement)
{
foundStatement = true;
}

continue;
}

if (extractedChild is TExpressionStatementSyntax expressionStatement &&
if (subsequentStatement is TExpressionStatementSyntax expressionStatement &&
TryProcessExpressionStatement(expressionStatement))
{
continue;
}
else if (extractedChild is TForeachStatementSyntax foreachStatement &&
else if (subsequentStatement is TForeachStatementSyntax foreachStatement &&
TryProcessForeachStatement(foreachStatement))
{
continue;
}
else if (extractedChild is TIfStatementSyntax ifStatement &&
else if (subsequentStatement is TIfStatementSyntax ifStatement &&
TryProcessIfStatement(ifStatement))
{
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text;
using Microsoft.CodeAnalysis.LanguageService;
using Microsoft.CodeAnalysis.Shared.Extensions;

namespace Microsoft.CodeAnalysis.Analyzers.UseCollectionInitializer;

Expand All @@ -15,4 +15,46 @@ internal static class UseCollectionInitializerHelpers

public static readonly ImmutableDictionary<string, string?> UseCollectionExpressionProperties =
ImmutableDictionary<string, string?>.Empty.Add(UseCollectionExpressionName, UseCollectionExpressionName);

public static IEnumerable<TStatementSyntax> GetSubsequentStatements<TStatementSyntax>(
ISyntaxFacts syntaxFacts,
TStatementSyntax initialStatement) where TStatementSyntax : SyntaxNode
{
// If containing statement is inside a block (e.g. method), than we need to iterate through its child
// statements. If containing statement is in top-level code, than we need to iterate through child statements of
// containing compilation unit.
var containingBlockOrCompilationUnit = initialStatement.GetRequiredParent();

// In case of top-level code parent of the statement will be GlobalStatementSyntax, so we need to get its parent
// in order to get CompilationUnitSyntax
if (syntaxFacts.IsGlobalStatement(containingBlockOrCompilationUnit))
containingBlockOrCompilationUnit = containingBlockOrCompilationUnit.Parent!;

var foundStatement = false;
foreach (var child in containingBlockOrCompilationUnit.ChildNodesAndTokens())
{
if (child.IsToken)
continue;

var childNode = child.AsNode()!;
var extractedChild = syntaxFacts.IsGlobalStatement(childNode)
? syntaxFacts.GetStatementOfGlobalStatement(childNode)
: childNode;

if (!foundStatement)
{
if (extractedChild == initialStatement)
{
foundStatement = true;
}

continue;
}

if (extractedChild is not TStatementSyntax childStatement)
break;

yield return childStatement;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using System.Collections.Immutable;
using System.Threading;
using Microsoft.CodeAnalysis.Analyzers.UseCollectionInitializer;
using Microsoft.CodeAnalysis.LanguageService;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
Expand Down Expand Up @@ -59,19 +60,6 @@ protected override bool ShouldAnalyze()

protected override bool TryAddMatches(ArrayBuilder<Match<TExpressionSyntax, TStatementSyntax, TMemberAccessExpressionSyntax, TAssignmentStatementSyntax>> matches)
{
// If containing statement is inside a block (e.g. method), than we need to iterate through its child statements.
// If containing statement is in top-level code, than we need to iterate through child statements of containing compilation unit.
var containingBlockOrCompilationUnit = _containingStatement.Parent;

// In case of top-level code parent of the statement will be GlobalStatementSyntax,
// so we need to get its parent in order to get CompilationUnitSyntax
if (_syntaxFacts.IsGlobalStatement(containingBlockOrCompilationUnit))
{
containingBlockOrCompilationUnit = containingBlockOrCompilationUnit.Parent;
}

var foundStatement = false;

using var _1 = PooledHashSet<string>.GetInstance(out var seenNames);

var initializer = _syntaxFacts.GetInitializerOfBaseObjectCreationExpression(_objectCreationExpression);
Expand All @@ -87,26 +75,9 @@ protected override bool TryAddMatches(ArrayBuilder<Match<TExpressionSyntax, TSta
}
}

foreach (var child in containingBlockOrCompilationUnit.ChildNodesAndTokens())
foreach (var subsequentStatement in UseCollectionInitializerHelpers.GetSubsequentStatements(_syntaxFacts, _containingStatement))
{
if (child.IsToken)
continue;

var childNode = child.AsNode();
var extractedChild = _syntaxFacts.IsGlobalStatement(childNode) ? _syntaxFacts.GetStatementOfGlobalStatement(childNode) : childNode;

if (!foundStatement)
{
if (extractedChild == _containingStatement)
{
foundStatement = true;
continue;
}

continue;
}

if (extractedChild is not TAssignmentStatementSyntax statement)
if (subsequentStatement is not TAssignmentStatementSyntax statement)
break;

if (!_syntaxFacts.IsSimpleAssignmentStatement(statement))
Expand Down