Repository: dotnet/roslyn
File: src/Analyzers/Core/Analyzers/UseCollectionInitializer/AbstractUseCollectionInitializerDiagnosticAnalyzer.cs (C#)

Code before:
        INamedTypeSymbol? expressionType)
    {
        var semanticModel = context.SemanticModel;
        var objectCreationExpression = (TObjectCreationExpressionSyntax)context.Node;
        var language = objectCreationExpression.Language;
        var cancellationToken = context.CancellationToken;

        var preferInitializerOption = context.GetAnalyzerOptions().PreferCollectionInitializer;
        var preferExpressionOption = context.GetAnalyzerOptions().PreferCollectionExpression;

        // not point in analyzing if both options are off.
        if (!preferInitializerOption.Value
            && preferExpressionOption.Value == Shared.CodeStyle.CollectionExpressionPreference.Never
            && !ShouldSkipAnalysis(context.FilterTree, context.Options, context.Compilation.Options,
                    [preferInitializerOption.Notification, preferExpressionOption.Notification],
                    context.CancellationToken))
        {
            return;
        }

        // Object creation can only be converted to collection initializer if it implements the IEnumerable type.
        var objectType = context.SemanticModel.GetTypeInfo(objectCreationExpression, cancellationToken);
        if (objectType.Type == null || !objectType.Type.AllInterfaces.Contains(ienumerableType))
            return;

        // Check if the diagnostic should be suppressed (e.g., inside CollectionBuilder methods)
        if (ShouldSuppressDiagnostic(semanticModel, objectCreationExpression, objectType.Type, cancellationToken))
            return;

        // Analyze the surrounding statements. First, try a broader set of statements if the language supports
        // collection expressions. 
        var syntaxFacts = this.SyntaxFacts;
        using var analyzer = GetAnalyzer();

        var containingStatement = objectCreationExpression.FirstAncestorOrSelf<TStatementSyntax>();
        if (containingStatement != null && !IsValidContainingStatement(containingStatement))
            return;

        var collectionExpressionMatches = GetCollectionExpressionMatches();
        var collectionInitializerMatches = GetCollectionInitializerMatches();

        // if both fail, we have nothing to offer.
        if (collectionExpressionMatches is null && collectionInitializerMatches is null)
            return;

        // if one fails, prefer the other.  If both succeed, prefer the one with more matches.
        var (matches, shouldUseCollectionExpression, changesSemantics) =
            collectionExpressionMatches is null ? collectionInitializerMatches!.Value :
            collectionInitializerMatches is null ? collectionExpressionMatches!.Value :
            collectionExpressionMatches.Value.matches.Length >= collectionInitializerMatches.Value.matches.Length
                ? collectionExpressionMatches.Value

Review comment (suggestion):
```suggestion
```

comment doesn't seem particularly useful given the method already has a doc comment iwth the same info

Code after:
                    [preferInitializerOption.Notification, preferExpressionOption.Notification],
                    context.CancellationToken))
        {
            return;
        }

        // Object creation can only be converted to collection initializer if it implements the IEnumerable type.
        var objectType = context.SemanticModel.GetTypeInfo(objectCreationExpression, cancellationToken);
        if (objectType.Type == null || !objectType.Type.AllInterfaces.Contains(ienumerableType))
            return;

        // Analyze the surrounding statements. First, try a broader set of statements if the language supports
        // collection expressions. 
        var syntaxFacts = this.SyntaxFacts;
        using var analyzer = GetAnalyzer();

        var containingStatement = objectCreationExpression.FirstAncestorOrSelf<TStatementSyntax>();
        if (containingStatement != null && !IsValidContainingStatement(containingStatement))
            return;

        var collectionExpressionMatches = GetCollectionExpressionMatches();
        var collectionInitializerMatches = GetCollectionInitializerMatches();

        // if both fail, we have nothing to offer.
        if (collectionExpressionMatches is null && collectionInitializerMatches is null)
            return;

        // if one fails, prefer the other.  If both succeed, prefer the one with more matches.
        var (matches, shouldUseCollectionExpression, changesSemantics) =
            collectionExpressionMatches is null ? collectionInitializerMatches!.Value :
            collectionInitializerMatches is null ? collectionExpressionMatches!.Value :
            collectionExpressionMatches.Value.matches.Length >= collectionInitializerMatches.Value.matches.Length
                ? collectionExpressionMatches.Value
                : collectionInitializerMatches.Value;

        var nodes = containingStatement is null
            ? ImmutableArray<SyntaxNode>.Empty
            : [containingStatement];
        nodes = nodes.AddRange(matches.Select(static m => m.Node));
        if (syntaxFacts.ContainsInterleavedDirective(nodes, cancellationToken))
            return;

        var locations = ImmutableArray.Create(objectCreationExpression.GetLocation());

        var notification = shouldUseCollectionExpression ? preferExpressionOption.Notification : preferInitializerOption.Notification;
        var properties = shouldUseCollectionExpression ? UseCollectionInitializerHelpers.UseCollectionExpressionProperties : ImmutableDictionary<string, string?>.Empty;
        if (changesSemantics)
            properties = properties.Add(UseCollectionInitializerHelpers.ChangesSemanticsName, "");

        context.ReportDiagnostic(DiagnosticHelper.Create(
            s_descriptor,