Repository: dotnet/roslyn
File: src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewriter_StringConcat.cs (C#)

Code before:
                    {
                        binaryOperator = null;
                        return false;
                    }
                }
            }
        }

        /// <summary>
        /// Visits the given argument if necessary and adds it to the final arguments list. It is expected that <paramref name="finalArguments"/> is being in reverse order, due to the left-recursive
        /// nature of the binary tree that we're traversing.
        /// </summary>
        /// <remarks>
        /// This method may end up deciding that the passed argument doesn't need to be included in the concat argument list (if, for example, it's a null constant or an empty string), and not add it
        /// to <paramref name="finalArguments"/>. It will also fold consecutive constant strings or chars into a single string constant, to avoid unnecessary concatenation. It may also do other optimizations,
        /// such as deconstructing nested string.Concat calls.
        /// </remarks>
        private void VisitAndAddConcatArgumentInReverseOrder(BoundExpression argument, bool argumentAlreadyVisited, ArrayBuilder<BoundExpression> finalArguments, ref StringConcatOptimizationPatterns stringConcatOptimizationPatterns)
        {
            Debug.Assert(argument is not BoundBinaryOperator { InterpolatedStringHandlerData: null } op || !IsBinaryStringConcatenation(op));
            if (!argumentAlreadyVisited)
            {
                argument = VisitExpression(argument);
            }

            if (argument is BoundConversion conversion && StringConcatOptimizationPatterns.IsBoxedChar(conversion, out BoundExpression? operand))
            {
                argument = operand;
            }
            else if (argument is BoundCall call)
            {
                if (stringConcatOptimizationPatterns.IsWellKnownConcatMethod(call, out var concatArguments))
                {
                    for (int i = concatArguments.Length - 1; i >= 0; i--)
                    {
                        VisitAndAddConcatArgumentInReverseOrder(concatArguments[i], argumentAlreadyVisited: true, finalArguments, ref stringConcatOptimizationPatterns);
                    }

                    return;
                }
                else if (stringConcatOptimizationPatterns.IsCharToString(call, out var charExpression))
                {
                    argument = charExpression;
                }
            }
            else if (argument is BoundNullCoalescingOperator nullCoalescingOperator && StringConcatOptimizationPatterns.IsNullCoalesceWithEmptyString(nullCoalescingOperator, out operand))
            {
                argument = operand;
            }

            switch (argument.ConstantValueOpt)

Review comment (suggestion):
Rather than duplicating these checks, making it non-obvious why the assert is what it is, I've opted to pull them into the single `StringConcatOptimizationPatterns` struct as a single place for all the different possible checks.

Code after:
                    {
                        return true;
                    }
                    else
                    {
                        binaryOperator = null;
                        return false;
                    }
                }
            }
        }

        /// <summary>
        /// Given a visited argument to a string concatenation, attempts to simplify it for inclusion in the final argument list to <code>string.Concat</code>. This includes:
        /// <list type="bullet">
        /// <item>Removing null or empty string constants</item>
        /// <item>Merging consecutive string or char constants into a single string constant</item>
        /// <item>Deconstructing nested string.Concat calls into their arguments</item>
        /// <item>Converting char.ToString() calls into the underlying char expression</item>
        /// <item>Converting `strValue ?? ""` into just `strValue`</item>
        /// </list>
        /// If the argument is simplified into a single argument, it is returned as <code>singleConcatArgument</code>. If it is deconstructed into multiple arguments (as in the case of
        /// nested string.Concat calls), those arguments are returned as <code>nestedConcatArguments</code>. If the argument is optimized away entirely (as in the case of null or empty string constants),
        /// then both return values will be null/default. If the argument is merged into the following argument, then <paramref name="followingArgument"/> will be updated to reflect that, and both return values will be null/default.
        /// </summary>
        private (BoundExpression? singleConcatArgument, ImmutableArray<BoundExpression> nestedConcatArguments) SimplifyConcatArgument(BoundExpression argument, [NotNullIfNotNull(nameof(followingArgument))] ref BoundExpression? followingArgument, ref WellKnownConcatRelatedMethods wellKnownConcatOptimizationMethods)
        {
            if (argument is BoundConversion { ConversionKind: ConversionKind.Boxing, Type.SpecialType: SpecialType.System_Object, Operand: { Type.SpecialType: SpecialType.System_Char } operand })
            {
                argument = operand;
            }
            else if (argument is BoundCall call)
            {
                if (wellKnownConcatOptimizationMethods.IsWellKnownConcatMethod(call, out var concatArguments))
                {
                    return (null, concatArguments);
                }
                else if (wellKnownConcatOptimizationMethods.IsCharToString(call, out var charExpression))
                {
                    argument = charExpression;
                }
            }
            // This is `strValue ?? ""`, possibly from a nested binary addition of an interpolated string. We can just directly use the left operand
            else if (argument is BoundNullCoalescingOperator { LeftOperand: { Type.SpecialType: SpecialType.System_String } left, RightOperand: BoundLiteral { ConstantValueOpt: { IsString: true, RopeValue.IsEmpty: true } } })
            {
                argument = left;
            }

            switch (argument.ConstantValueOpt)
            {
                case { IsNull: true } or { IsString: true, RopeValue.IsEmpty: true }: