"Invert if" possible bug?

Hi,
I'm using the latest build of R#7 and I ran into a situation where the "invert if" option may not work properly.
Consider the following code:
class Program
    {
        private object test = new object();

        static void Main(string[] args)
        {
        }

        private void DoSomething()
        {
            var content = this.test as TestType;
            if (content != null)
            {
                var myInterface = content.Content as IMyInterface;
                if (myInterface != null)
                {
                    myInterface.HelloWorld();
                    return;
                }
            }

            this.LocalMethod();
        }

        private void LocalMethod()
        {
            throw new NotImplementedException();
        }


        public interface IMyInterface
        {
            void HelloWorld();
        }

        public class TestType
        {
            public object Content { get; set; }
        }
    }

In the DoSomething method I have 2 'if' statements.
Using R#, if I invert the first 'if' statement, I get this:
private void DoSomething()
        {
            var content = this.test as TestType;
            if (content == null)
            {
            }
            else
            {
                var myInterface = content.Content as IMyInterface;
                if (myInterface != null)
                {
                    myInterface.HelloWorld();
                    return;
                }
            }

            this.LocalMethod();
        }
That is valid. If the content is null, the second 'if' will no longer be evaluated and the "LocalMethod" will be executed next.


But, if I invert the second "if" statement (using R# again), I get this:

        private void DoSomething()
        {
            var content = this.test as TestType;
            if (content != null)
            {
                var myInterface = content.Content as IMyInterface;
                if (myInterface == null)
                {
                    return;
                }

                myInterface.HelloWorld();
                return;
            }

            this.LocalMethod();
        }
In the original code, if the "myInterface" was null, the "LocalMethod" was executed next. If I invert the 'if' statement, if the "myInterface" is null, the metod will actually return and "LocalMethod" will never be executed. I think that is a bug. I've tested it in R#8 and it is also present there.
I think the correct invertion should have been similar to the the way the 1st 'if' is inverted.

Thanks,
Cristian

2 comments
Comment actions Permalink

We would really appreciate if somone from JetBrains would have a look at this. We just discovered a bug in production code introduced by this.

We are using R# 8.1.

Thanks again,
Cristian

0

Please sign in to leave a comment.