public RedundantToStringCall()
        {
            var s = "foo";
            var t = "fee fie foe " + s.ToString();  // Noncompliant
            t = "fee fie foe " + s.ToString(System.Globalization.CultureInfo.InvariantCulture);
            var u = "" + 1.ToString(); //Noncompliant
            u = 1.ToString() + ""; // Noncompliant
            u = 1.ToString() // Noncompliant
                + 2.ToString(); // Noncompliant, note: this is why we don't have a global fix

            u += 2.ToString(); // Noncompliant
            u = 1.ToString() + 2;

            var x = 1 + 3;

            var v = string.Format("{0}",
                1.ToString()); //Noncompliant

            u = 1.ToString();

            var m = new MyClass();
            s = m.ToString() + "";

            s = base.ToString() + "";
        }
        public RedundantToStringCall()
        {
            var s = "foo";
            var t = "fee fie foe " + s;  // Fixed
            t = "fee fie foe " + s.ToString(System.Globalization.CultureInfo.InvariantCulture);
            var u = "" + 1.ToString(); // Compliant, value type
            u = new object() + ""; // Fixed
            u = new object() // Fixed
                + new object().ToString(); // Fixed

            u += new object(); // Fixed
            u = 1.ToString() + 2;

            var x = 1 + 3;

            var v = string.Format("{0}",
                new object()); //Fixed

            v = string.Format("{0}",
                1.ToString()); // Compliant, value type

            u = 1.ToString();

            var m = new MyClass();
            s = m.ToString() + "";

            s = base.ToString() + "";
        }
        public RedundantToStringCall()
        {
            var s = "foo";
            var t = "fee fie foe " + s.ToString();  // Noncompliant {{There's no need to call "ToString()" on a string.}}
//                                    ^^^^^^^^^^^
            t = "fee fie foe " + s.ToString(System.Globalization.CultureInfo.InvariantCulture);
            var u = "" + 1.ToString(); // Compliant, value type
            u = new object().ToString() + ""; // Noncompliant
            u = new object().ToString() // Noncompliant
                + new object().ToString(); // Noncompliant, note: this is why we don't have a global fix

            u += new object().ToString(); // Noncompliant {{There's no need to call "ToString()", the compiler will do it for you.}}
            u = 1.ToString() + 2;

            var x = 1 + 3;

            var v = string.Format("{0}",
                new object().ToString()); //Noncompliant

            v = string.Format("{0}",
                1.ToString()); // Compliant, value type

            u = 1.ToString();

            var m = new MyClass();
            s = m.ToString() + "";

            s = base.ToString() + "";
        }
        void Foo()
        {
            int i = 0;

            foo(i = 42); // Noncompliant
            foo(i += 42); // Noncompliant
            foo(i -= 42); // Noncompliant
            foo(i *= 42); // Noncompliant
            foo(i /= 42); // Noncompliant
            foo(i %= 42); // Noncompliant
            foo(i &= 1); // Noncompliant
            foo(i ^= 1); // Noncompliant
            foo(i |= 1); // Noncompliant
            foo(i <<= 1); // Noncompliant
            foo(i >>= 1); // Noncompliant

            i = 42;
            foo(i == 42);

            foo(
                (int x) =>
            {
                int a;
                a = 42;
                return a;
            });

            var b = true;

            if (b = false) { } // Noncompliant
            for (int j = 0; b = false; j++) // Noncompliant
            {
                
            }
            for (int j = 0; b == false; j++) 
            {

            }

            if (i == 0) i = 2;
            if ((i = 1) == 1) // Noncompliant
            {
            }

            string result = "";
            if (!string.IsNullOrEmpty(result)) result = result + " ";
            var v1 = new Action(delegate { });
            var v2 = new Action(delegate { var foo = 42; });
            var v3 = new Func<object, object>((x) => x = 42);
            var v4 = new Action<object>((x) => { x = 42; });
            var v5 = new { MyField = 42 };
            var v6 = new MyClass { MyField = 42 };
            var v7 = new MyClass() { MyField = 42 };
            var v8 = foo(x => { x = 42; return 0; } );
        }
        public RedundantNullableTypeComparison()
        {
            int? nullable = 42;
            bool comparison = nullable.GetType() == typeof(Nullable<int>); // Noncompliant, always false
//                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            comparison = nullable.GetType() != typeof(Nullable<int>); // Noncompliant {{Remove this redundant type comparison.}}

            comparison = new MyClass().GetType() != typeof(Nullable<int>);
            comparison = 42.GetType() != typeof(int);
            comparison = 42.ToString() != typeof(int);
            comparison = new object() != typeof(Nullable<int>);
        }
 public void EnumMemberAccess()
 {
     var m = new MyClass();
     Console.WriteLine(m.myEnum);
     m = null;
     if (m?.myEnum == MyEnum.One) // Noncompliant {{Change this condition so that it does not always evaluate to "false".}}
     {
     }
 }
 public Nested()
 {
     inst = new MyClass();
 }
        void Foo()
        {
            int i = 0;

            foo(i = 42); // Noncompliant
            foo(i += 42); // Noncompliant
            foo(i -= 42); // Noncompliant
            foo(i *= 42); // Noncompliant
            foo(i /= 42); // Noncompliant
            foo(i %= 42); // Noncompliant
            foo(i &= 1); // Noncompliant
            foo(i ^= 1); // Noncompliant
            foo(i |= 1); // Noncompliant
            foo(i <<= 1); // Noncompliant
            foo(i >>= 1); // Noncompliant

            i = 42;
            foo(i == 42);

            foo(
                (int x) =>
                {
                    int a;
                    a = 42;
                    return a;
                });

            var b = true;

            if (b = false) { } // Noncompliant
            if ((b = false)) { } // Noncompliant
            for (int j = 0; b &= false; j++) { } // Noncompliant
            for (int j = 0; b == false; j++) { }

            while (b &= false) { } // Noncompliant

            while ((i = 1) == 1) { } // Compliant

            if (i == 0) i = 2;
            if ((i = 1) <= 1) // Compliant
            {
            }
            b = (i = 1) <= 1; // Noncompliant

            var y = (b &= false) ? i : i * 2; // Noncompliant

            string result = "";
            if (!string.IsNullOrEmpty(result)) result = result + " ";
            var v1 = new Action(delegate { });
            var v2 = new Action(delegate { var foo = 42; });
            var v3 = new Func<object, object>((x) => x = 42);
            var v4 = new Action<object>((x) => { x = 42; });
            var v5 = new { MyField = 42 };
            var v6 = new MyClass { MyField = 42 };
            var v7 = new MyClass() { MyField = 42 };
            var v8 = foo(x => { x = 42; return 0; });

            var index = 0;
            new int[] { 0, 1, 2 }[index = 2] = 10; // Noncompliant
            new int[] { 0, 1, 2 }[(index = 2)] = 10; // Noncompliant

            var o = new object();
            var oo = new object();

            if (false && (oo = o) != null) // Compliant
            { }

            oo = (o) ?? (o = new object()); // Compliant
            oo = (o) ?? (object)(o = new object()); // Compliant

            oo = oo ?? (o = new object()); // Noncompliant

            if ((a = b = 0) != 0) { }  // Noncompliant
            int x = (a = b) + 5; // Noncompliant
        }
예제 #9
0
        void Foo()
        {
            int i = 0;

            foo(i = 42);   // Noncompliant {{Extract the assignment of 'i' from this expression.}}
//                ^
            foo(i  += 42); // Noncompliant
            foo(i  -= 42); // Noncompliant
            foo(i  *= 42); // Noncompliant
            foo(i  /= 42); // Noncompliant
            foo(i  %= 42); // Noncompliant
            foo(i  &= 1);  // Noncompliant
            foo(i  ^= 1);  // Noncompliant
            foo(i  |= 1);  // Noncompliant
            foo(i <<= 1);  // Noncompliant
            foo(i >>= 1);  // Noncompliant
//                ^^^

            i = 42;
            foo(i == 42);

            foo(
                (int x) =>
            {
                int a;
                a = 42;
                return(a);
            });

            var b = true;

            if (b = false)
            {
            }                  // Noncompliant
//                ^
            if ((b = false))
            {
            }                    // Noncompliant  {{Extract the assignment of 'b' from this expression.}}
            for (int j = 0; b &= false; j++)
            {
            }                                    // Noncompliant
            for (int j = 0; b == false; j++)
            {
            }

            // Fix S1121: NullReferenceException when while loop with assignment expression is within a for loop with no condition (#725)
            for (;;)
            {
                while ((b = GetBool()) == true)
                {
                }
            }

            while (b &= false)
            {
            }                      // Noncompliant

            while ((i = 1) == 1)
            {
            }                        // Compliant

            if (i == 0)
            {
                i = 2;
            }
            if ((i = 1) <= 1) // Compliant
            {
            }
            b = (i = 1) <= 1;                 // Noncompliant

            var y = (b &= false) ? i : i * 2; // Noncompliant

            string result = "";

            if (!string.IsNullOrEmpty(result))
            {
                result = result + " ";
            }
            var v1 = new Action(delegate { });
            var v2 = new Action(delegate { var foo = 42; });
            var v3 = new Func <object, object>((x) => x = 42);
            var v4 = new Action <object>((x) => { x = 42; });
            var v5 = new { MyField = 42 };
            var v6 = new MyClass {
                MyField = 42
            };
            var v7 = new MyClass()
            {
                MyField = 42
            };
            var v8 = foo(x => { x = 42; return(0); });

            var index = 0;

            new int[] { 0, 1, 2 }[index = 2]   = 10; // Noncompliant
            new int[] { 0, 1, 2 }[(index = 2)] = 10; // Noncompliant

            var o  = new object();
            var oo = new object();

            if (false && (oo = o) != null) // Compliant
            {
            }

            oo = (o) ?? (o = new object());         // Compliant
            oo = (o) ?? (object)(o = new object()); // Compliant

            oo = oo ?? (o = new object());          // Noncompliant

            if ((a = b = 0) != 0)
            {
            }                    // Compliant
            int x = (a = b) + 5; // Noncompliant
        }
 public Nested()
 {
     inst = new MyClass();
 }