Skip to content

When I convert an "if" statement containing a checked expression into a conditional expression in the c# editor, it is converted to unintended code. #70748

@vsfeedback

Description

@vsfeedback

This issue has been moved from a ticket on Developer Community.


[severity:It bothers me. A fix would be nice]

[Reproducibility conditions]

  1. In the c# editor, and
  2. An if statement of the form if (<condition>) return <stateme 1>; else return <statement 2>; exists, and
  3. condition or statement 1 or statement 2 is a checked expression (checked(<sub-statement>)) and
  4. When “Quick actions and refactoring…” and “Convert to conditional expression” are executed for an if statement.

[Expected behavior]

The if statement is converted to a conditional expression while keeping the checked expression.

[Actual operation]

Checked expressions are lost. (The checked expression checked(<sub-statement>) is converted to just <sub-statement>)

[Sample program]

using System;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;

namespace Experiment01
{
    internal class Program
    {
        [SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "<Pending>")]
        private static int Main(string[] args)
        {
            Console.WriteLine(PlusAndEquals(int.MaxValue, int.MaxValue, int.MinValue, int.MinValue));
            Console.WriteLine(Difference(int.MaxValue, int.MinValue));
            return 0;
        }

        private static string PlusAndEquals(int x1, int x2, int y1, int y2)
        {
            if (checked(x1 + x2 == y1 + y2)) // If this "if" is converted into a conditional expression, it will be converted into unintended code.
                return "OK";
            else
                return "NG";
        }

        private static int Difference(int x, int y)
        {
            if (x >= y) // If this "if" is converted into a conditional expression, it will be converted into unintended code.
                return checked(x - y);
            else
                return 0;
        }
    }
}
  

-When converting the if statement of the PlusAndEquals method to a conditional expression-

The overflow check for x1 + x2 == y1 + y2 will no longer be performed as shown in the code below.

        private static string PlusAndEquals(int x1, int x2, int y1, int y2)
        {
            return x1 + x2 == y1 + y2 ? "OK" : "NG";
        }
  

-When converting the if statement of the Difference method to a conditional expression-

The overflow check for x - y will no longer be performed as shown in the code below.

        private static int Difference(int x, int y)
        {
            return x >= y ? x - y : 0;
        }
  

Original Comments

Feedback Bot on 11/8/2023, 11:32 PM:

(private comment, text removed)


Original Solutions

(no solutions)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-IDEBughelp wantedThe issue is "up for grabs" - add a comment if you are interested in working on it

    Type

    No type

    Projects

    Status

    Completed

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions