Beispiel #1
0
        int Test(object a, object b, object y, bool condition)
        {
            object x;

            if (a != null) // Noncompliant {{Use the '??' operator here.}}
//          ^^
            {
                /*some comment*/
                x = a;
            }
            else
            {
                x = b /*some other comment*/;
            }

            x = a != null ? (a) : b; // Noncompliant {{Use the '??' operator here.}}
//              ^^^^^^^^^^^^^^^^^^^
            x = a != null ? a : a;   // Compliant, triggers S2758

            int i = 5;
            var z = i == null ? 4 : i;                              //can't be converted

            x = (y == null) ? Identity(new object()) : Identity(y); // Noncompliant {{Use the '??' operator here.}}
//              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

            x = a ?? b;
            x = a ?? b;
            x = y ?? new object();
            x = condition ? a : b;

            if (condition) // Noncompliant {{Use the '?:' operator here.}}
            {
                x = a;
            }
            else
            {
                x = b;
            }

            if (condition) // Noncompliant
            {
                x = Identity(new object());
            }
            else
            {
                x = IdentityAnyOtherMethod(y);
            }

            if (condition) // Noncompliant
            {
                Identity(new object());
            }
            else
            {
                Identity(y);
            }

            if (condition) // Noncompliant
            {
                return(1);
            }
            else
            {
                return(2);
            }

            if (condition)
            {
                return(1);
            }
            else if (condition) //Compliant
            {
                return(2);
            }
            else
            {
                return(3);
            }

            X o = null;

            if (o == null) //Non-compliant, but not handled because of the type difference, and there is no fix for it
            {
                x = new Y();
            }
            else
            {
                x = o;
            }

            bool?value = null;

            if (value == null) // Compliant - FN:  x ??= false can be used instead
            {
                value = false;
            }

            var yyy = new Y();

            if (condition) //Noncompliant
            {
                x = Identity(new Y());
            }
            else
            {
                x = Identity(yyy);
            }

            if (condition) //Noncompliant
            {
                x = Identity(new Y());
            }
            else
            {
                x = Identity(new X());
            }

            Base elem;

            if (condition) // Non-compliant, but not handled because of the type difference
            {
                elem = new A();
            }
            else
            {
                elem = new B();
            }

            if (condition) //Noncompliant
            {
                x = Identity(new Y());
            }
            else
            {
                x = Identity(yyy);
            }

            if (condition) // Noncompliant
            {
                elem = new A();
            }
            else
            {
                elem = null;
            }
            if (condition) // Non-compliant, but not handled because of the type difference
            {
                elem = new A();
            }
            else
            {
                elem = new NonExistentType(); // Error [CS0246]
            }

            if (false) // Noncompliant
            {
                elem = null;
            }
            else
            {
                elem = (null);
            }
        }
        int Test(object a, object b, object y, bool condition)
        {
            object x;

            x = a ?? b /*some other comment*/;

            x = a ?? b;            // Fixed
            x = a != null ? a : a; // Compliant, triggers S2758

            int i = 5;
            var z = i == null ? 4 : i;       //can't be converted

            x = Identity(y ?? new object()); // Fixed

            a ??= b;                         // Fixed
            a ??= b;                         // Fixed
            a ??= b;                         // Fixed
            a ??= b;                         // Fixed
            a ??= b;                         // Fixed
            a ??= b;                         // Fixed

            x = a ?? b;
            x = a ?? b;
            x = y ?? new object();
            x = condition ? a : b;

            x = condition ? a : b;

            x = a ?? b;

            x = condition ? Identity(new object()) : IdentityAnyOtherMethod(y);

            Identity(condition ? new object() : y);

            return(condition ? 1 : 2);

            if (condition)
            {
                return(1);
            }
            else if (condition) //Compliant
            {
                return(2);
            }
            else
            {
                return(3);
            }

            X o = null;

            if (o == null) //Non-compliant, but not handled because of the type difference, and there is no fix for it
            {
                x = new Y();
            }
            else
            {
                x = o;
            }

            //This will be CodeFix-ed
            a ??= b;

            if (a != null)
            {
                a = b;
            }

            bool?value = null;

            value ??= false;  //This will be CodeFix-ed and this comment should be preserved

            var yyy = new Y();

            x = condition ? Identity(new Y()) : Identity(yyy);

            x = condition ? Identity(new Y()) : Identity(new X());

            // Removing space from "if (" on next line will fail the test.
            // https://github.com/SonarSource/sonar-dotnet/issues/3064
            Identity(yyy ?? new Y());

            Identity(yyy ?? new Y());

            Base elem;

            if (condition) // Non-compliant, but not handled because of the type difference
            {
                elem = new A();
            }
            else
            {
                elem = new B();
            }

            x = condition ? Identity(new Y()) : Identity(yyy);

            elem = condition ? new A() : null;
            if (condition) // Non-compliant, but not handled because of the type difference
            {
                elem = new A();
            }
            else
            {
                elem = new NonExistentType(); // Error [CS0246]
            }

            elem = false ? null : (null);
        }
        int Test(object a, object b, object y, bool condition)
        {
            object x;

            x = a ?? b /*some other comment*/;

            x = a ?? b;            // Fixed
            x = a != null ? a : a; // Compliant, triggers S2758

            int i = 5;
            var z = i == null ? 4 : i;       //can't be converted

            x = Identity(y ?? new object()); // Fixed

            x = a ?? b;
            x = a ?? b;
            x = y ?? new object();
            x = condition ? a : b;

            x = condition ? a : b;

            x = condition ? Identity(new object()) : IdentityAnyOtherMethod(y);

            Identity(condition ? new object() : y);

            return(condition ? 1 : 2);

            if (condition)
            {
                return(1);
            }
            else if (condition) //Compliant
            {
                return(2);
            }
            else
            {
                return(3);
            }

            X o = null;

            if (o == null) //Non-compliant, but not handled because of the type difference, and there is no fix for it
            {
                x = new Y();
            }
            else
            {
                x = o;
            }

            bool?value = null;

            if (value == null) // Compliant - FN:  x ??= false can be used instead
            {
                value = false;
            }

            var yyy = new Y();

            x = condition ? Identity(new Y()) : Identity(yyy);

            x = condition ? Identity(new Y()) : Identity(new X());

            Base elem;

            if (condition) // Non-compliant, but not handled because of the type difference
            {
                elem = new A();
            }
            else
            {
                elem = new B();
            }

            x = condition ? Identity(new Y()) : Identity(yyy);

            elem = condition ? new A() : null;
            if (condition) // Non-compliant, but not handled because of the type difference
            {
                elem = new A();
            }
            else
            {
                elem = new NonExistentType(); // Error [CS0246]
            }

            elem = false ? null : (null);
        }
Beispiel #4
0
        int Test(object a, object b, object y, bool condition)
        {
            object x;

            if (a != null) // Noncompliant {{Use the '??' operator here.}}
//          ^^
            {
                /*some comment*/
                x = a;
            }
            else
            {
                x = b /*some other comment*/;
            }

            x = a != null ? (a) : b; // Noncompliant {{Use the '??' operator here.}}
//              ^^^^^^^^^^^^^^^^^^^
            x = a != null ? a : a;   // Compliant, triggers S2758

            int i = 5;
            var z = i == null ? 4 : i;                              //can't be converted

            x = (y == null) ? Identity(new object()) : Identity(y); // Noncompliant {{Use the '??' operator here.}}
//              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

            a = a ?? b;                  // Noncompliant {{Use the '??=' operator here.}}
//          ^^^^^^^^^^
            a = a != null ? (a) : b;     // Noncompliant {{Use the '??=' operator here.}}
//          ^^^^^^^^^^^^^^^^^^^^^^^
            a = null == a ? b : (a);     // Noncompliant {{Use the '??=' operator here.}}
            a = (a ?? b);                // Noncompliant {{Use the '??=' operator here.}}
            a = ((a ?? b));              // Noncompliant {{Use the '??=' operator here.}}
            a = ((null == a ? b : (a))); // Noncompliant {{Use the '??=' operator here.}}
//          ^^^^^^^^^^^^^^^^^^^^^^^^^^^

            x = a ?? b;
            x = a ?? b;
            x = y ?? new object();
            x = condition ? a : b;

            if (condition) // Noncompliant {{Use the '?:' operator here.}}
            {
                x = a;
            }
            else
            {
                x = b;
            }

            if ((a != null)) // Noncompliant {{Use the '??' operator here.}}
            {
                x = a;
            }
            else
            {
                x = b;
            }

            if (condition) // Noncompliant
            {
                x = Identity(new object());
            }
            else
            {
                x = IdentityAnyOtherMethod(y);
            }

            if (condition) // Noncompliant
            {
                Identity(new object());
            }
            else
            {
                Identity(y);
            }

            if (condition) // Noncompliant
            {
                return(1);
            }
            else
            {
                return(2);
            }

            if (condition)
            {
                return(1);
            }
            else if (condition) //Compliant
            {
                return(2);
            }
            else
            {
                return(3);
            }

            X o = null;

            if (o == null) //Non-compliant, but not handled because of the type difference, and there is no fix for it
            {
                x = new Y();
            }
            else
            {
                x = o;
            }

            //This will be CodeFix-ed
            if (a == null) // Noncompliant {{Use the '??=' operator here.}}
            {
                a = b;
            }

            if (a != null)
            {
                a = b;
            }

            bool?value = null;

            if (value == null)  // Noncompliant {{Use the '??=' operator here.}}
            {
                value = false;  //This will be CodeFix-ed and this comment should be preserved
            }
            var yyy = new Y();

            if (condition) //Noncompliant
            {
                x = Identity(new Y());
            }
            else
            {
                x = Identity(yyy);
            }

            if (condition) //Noncompliant
            {
                x = Identity(new Y());
            }
            else
            {
                x = Identity(new X());
            }

            // Removing space from "if (" on next line will fail the test.
            // https://github.com/SonarSource/sonar-dotnet/issues/3064
            if (yyy == null) // Noncompliant
            {
                Identity(new Y());
            }
            else
            {
                Identity(yyy);
            }

            if (((yyy == null))) // Noncompliant
            {
                Identity(new Y());
            }
            else
            {
                Identity(((yyy)));
            }

            Base elem;

            if (condition) // Non-compliant, but not handled because of the type difference
            {
                elem = new A();
            }
            else
            {
                elem = new B();
            }

            if (condition) //Noncompliant
            {
                x = Identity(new Y());
            }
            else
            {
                x = Identity(yyy);
            }

            if (condition) // Noncompliant
            {
                elem = new A();
            }
            else
            {
                elem = null;
            }
            if (condition) // Non-compliant, but not handled because of the type difference
            {
                elem = new A();
            }
            else
            {
                elem = new NonExistentType(); // Error [CS0246]
            }

            if (false) // Noncompliant
            {
                elem = null;
            }
            else
            {
                elem = (null);
            }
        }