Skip to content

Empty statement ";" suppresses try-catch-finally optimization in some circumstances #4729

@gafter

Description

@gafter

In https://github.com/dotnet/roslyn/pull/4569/files#r37368440 @AlekseyTs noted that we incorrectly treat a "no-op" statement as having side-effects. In the context where the problem occurs, that has the effect of sometimes suppressing the optimization that allows us to elide empty finally blocks, or elide catch blocks when the try block is empty. See case BoundKind.NoOpStatement below, which should return false.

        /// <summary>
        /// Is there any code to execute in the given statement that could have side-effects,
        /// such as throwing an exception? This implementation is conserviative, in the sense
        /// that it may return true when the statement actually may have no side effects.
        /// </summary>
        private static bool HasSideEffects(BoundStatement statement)
        {
            if (statement == null) return false;
            switch (statement.Kind)
            {
                case BoundKind.NoOpStatement:
                    return true;
                case BoundKind.Block:
                    {
                        var block = (BoundBlock)statement;
                        foreach (var stmt in block.Statements)
                        {

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions