コード例 #1
0
        public async Task TestNoCodeFixForInvalidXmlAsync()
        {
            var testCode  = @"
/// <summary>
/// From A to B.
/// <p>
/// An unterminated second paragraph...
/// </summary>
class TestClass
{
}
";
            var fixedCode = testCode;

            await new Verify.Test
            {
                TestState =
                {
                    Sources             = { testCode },
                    ExpectedDiagnostics =
                    {
                        DiagnosticResult.CompilerWarning("CS1570").WithSpan(6, 7, 6, 14).WithMessage("XML comment has badly formed XML -- 'End tag 'summary' does not match the start tag 'p'.'"),
                        DiagnosticResult.CompilerWarning("CS1570").WithSpan(7, 1, 7,           1).WithMessage("XML comment has badly formed XML -- 'Expected an end tag for element 'summary'.'"),
                    },
                },
                FixedState =
                {
                    Sources         = { fixedCode },
                    InheritanceMode = StateInheritanceMode.AutoInheritAll,
                },
                CompilerDiagnostics = CompilerDiagnostics.Warnings,
            }.RunAsync();
        }
コード例 #2
0
        public async Task TestHtmlEntityReplacementAsync()
        {
            var testCode  = @"
/// <summary>
/// From A&rarr;B.
/// </summary>
class TestClass
{
}
";
            var fixedCode = @"
/// <summary>
/// From A→B.
/// </summary>
class TestClass
{
}
";

            await new Verify.Test
            {
                TestCode            = testCode,
                ExpectedDiagnostics = { DiagnosticResult.CompilerWarning("CS1570").WithSpan(3, 11, 3, 11).WithMessage("XML comment has badly formed XML -- 'Reference to undefined entity 'rarr'.'") },
                FixedCode           = fixedCode,
                CompilerDiagnostics = CompilerDiagnostics.Warnings,
            }.RunAsync();
        }
コード例 #3
0
        public async Task TestUnknownEntityNotReplacedAsync()
        {
            var testCode  = @"
/// <summary>
/// Unknown entity &myEntity;.
/// </summary>
class TestClass
{
}
";
            var fixedCode = testCode;

            await new Verify.Test
            {
                TestState =
                {
                    Sources             = { testCode                                                                                           },
                    ExpectedDiagnostics = { DiagnosticResult.CompilerWarning("CS1570").WithSpan(3, 20, 3, 20).WithMessage("XML comment has badly formed XML -- 'Reference to undefined entity 'myEntity'.'")},
                },
                FixedState =
                {
                    Sources         = { fixedCode },
                    InheritanceMode = StateInheritanceMode.AutoInheritAll,
                },
                CompilerDiagnostics = CompilerDiagnostics.Warnings,
            }.RunAsync();
        }
コード例 #4
0
        public async Task IAsyncEnumerable_Missing()
        {
            var source = @"
using System.Threading.Tasks;
using System.Collections.Generic;

class C
{
    async IAsyncEnumerable<int> M()
    {
        yield return 1;
    }
}" + AsyncStreamsTypes;

            await new VerifyCS.Test
            {
                ReferenceAssemblies = ReferenceAssemblies.NetStandard.NetStandard21,
                TestCode            = source,
                ExpectedDiagnostics =
                {
                    // /0/Test0.cs(7,33): warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
                    DiagnosticResult.CompilerWarning("CS1998").WithSpan(7, 33, 7, 34),
                },
                FixedCode = source,
            }.RunAsync();
        }
コード例 #5
0
        public async Task SimpleLambda_AsyncVoid_Missing()
        {
            var source = @"
using System;
using System.Threading.Tasks;

class C
{
    void M()
    {
        Action<int> a = async x => Console.WriteLine(x);
    }
}";

            await new VerifyCS.Test
            {
                ReferenceAssemblies = ReferenceAssemblies.NetStandard.NetStandard21,
                TestCode            = source,
                ExpectedDiagnostics =
                {
                    // /0/Test0.cs(9,33): warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
                    DiagnosticResult.CompilerWarning("CS1998").WithSpan(9, 33, 9, 35),
                },
                FixedCode = source,
            }.RunAsync();
        }
コード例 #6
0
        public async Task TestHtmlEntityReplacementInInvalidXmlAsync()
        {
            var testCode  = @"
/// <summary>
/// From A&rarr;B.
/// <p>
/// An unterminated second paragraph...
/// </summary>
class TestClass
{
}
";
            var fixedCode = @"
/// <summary>
/// From A→B.
/// <p>
/// An unterminated second paragraph...
/// </summary>
class TestClass
{
}
";

            await new Verify.Test
            {
                TestState =
                {
                    Sources             = { testCode },
                    ExpectedDiagnostics =
                    {
                        DiagnosticResult.CompilerWarning("CS1570").WithSpan(3, 11, 3,               11).WithMessage("XML comment has badly formed XML -- 'Reference to undefined entity 'rarr'.'"),
                        DiagnosticResult.CompilerWarning("CS1570").WithSpan(6,  7, 6, 14).WithMessage("XML comment has badly formed XML -- 'End tag 'summary' does not match the start tag 'p'.'"),
                        DiagnosticResult.CompilerWarning("CS1570").WithSpan(7,  1, 7,           1).WithMessage("XML comment has badly formed XML -- 'Expected an end tag for element 'summary'.'"),
                    },
                },
                FixedState =
                {
                    Sources             = { fixedCode },
                    ExpectedDiagnostics =
                    {
                        DiagnosticResult.CompilerWarning("CS1570").WithSpan(6, 7, 6, 14).WithMessage("XML comment has badly formed XML -- 'End tag 'summary' does not match the start tag 'p'.'"),
                        DiagnosticResult.CompilerWarning("CS1570").WithSpan(7, 1, 7,           1).WithMessage("XML comment has badly formed XML -- 'Expected an end tag for element 'summary'.'"),
                    },
                },
                CompilerDiagnostics = CompilerDiagnostics.Warnings,
            }.RunAsync();
        }
コード例 #7
0
    public async Task NonUnityClassIsExemptFromSuppressions()
    {
        const string test = @"
#nullable enable
using UnityEngine;

namespace Assets.Scripts
{
    public class Test /* is not a Unity object */
    {
        private GameObject field1;
        
        private GameObject property1 { get; set; }

        private void Start()
        {
            field1 = new GameObject();

            property1 = new GameObject();
        }
    }
}
";

        var context = AnalyzerVerificationContext.Default
                      .WithLanguageVersion(LanguageVersion.CSharp8)
                      .WithAnalyzerFilter("CS0169");

        DiagnosticResult[] diagnostics =
        {
            DiagnosticResult.CompilerWarning(NullableReferenceTypesSuppressor.Rule.SuppressedDiagnosticId)
            .WithMessageFormat(WarningFormat)
            .WithArguments("field",    "field1")
            .WithLocation(9,           28),

            DiagnosticResult.CompilerWarning(NullableReferenceTypesSuppressor.Rule.SuppressedDiagnosticId)
            .WithMessageFormat(WarningFormat)
            .WithArguments("property", "property1")
            .WithLocation(11,          28),
        };

        await VerifyCSharpDiagnosticAsync(context, test, diagnostics);
    }
コード例 #8
0
    public async Task RaisesWarningForGenericRedundantCode()
    {
        this.TestCode =
            @"
            using Remora.Results;

            public class Program
            {
                public static void Main()
                {
                    Result<int> b = default;
                    Result<int>.FromError(b.Error);
                }
            }
        ";

        this.ExpectedDiagnostics.Clear();
        this.ExpectedDiagnostics.Add(DiagnosticResult.CompilerWarning("REM0001").WithSpan(9, 21, 9, 51).WithArguments("b"));

        await RunAsync();
    }
    public async Task ReplacesConditionalWithArgument()
    {
        this.CompilerDiagnostics = CompilerDiagnostics.None;

        this.TestCode =
            @"
            using Remora.Results;

            public class Program
            {
                public static void Main()
                {
                    Result b = default;

                    var result = b.IsSuccess
                        ? Result.FromSuccess()
                        : b;
                }
            }
        ";

        this.ExpectedDiagnostics.Add(DiagnosticResult.CompilerWarning("REM0002").WithSpan(10, 34, 12, 28).WithArguments("b"));

        this.FixedCode =
            @"
            using Remora.Results;

            public class Program
            {
                public static void Main()
                {
                    Result b = default;

                    var result = b;
                }
            }
        ";

        await RunAsync();
    }
コード例 #10
0
    public async Task ReplacesCallWithArgument()
    {
        this.CompilerDiagnostics = CompilerDiagnostics.None;

        this.TestCode =
            @"
            struct Result { public static void FromError(object obj) { } public object Error; }

            public class Program
            {
                public static void Main()
                {
                    Result b;
                    Result.FromError(b.Error);
                }
            }
        ";

        this.ExpectedDiagnostics.Add(DiagnosticResult.CompilerWarning("REM0001").WithSpan(9, 21, 9, 46).WithArguments("b"));

        this.FixedCode =
            @"
            struct Result { public static void FromError(object obj) { } public object Error; }

            public class Program
            {
                public static void Main()
                {
                    Result b;
                    b;
                }
            }
        ";

        await RunAsync();
    }
コード例 #11
0
    public async Task RaisesWarningForInvertedRedundantCode()
    {
        this.TestCode =
            @"
            using Remora.Results;

            public class Program
            {
                public static void Main()
                {
                    Result b = default;

                    var result = !b.IsSuccess
                        ? b
                        : Result.FromSuccess();
                }
            }
        ";

        this.ExpectedDiagnostics.Clear();
        this.ExpectedDiagnostics.Add(DiagnosticResult.CompilerWarning("REM0002").WithSpan(10, 34, 12, 47).WithArguments("b"));

        await RunAsync();
    }
        public async Task VerifyInheritedInterfaceAnalyzers()
        {
            var test = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace ConsoleApplication1
{
    public interface ICustomInterface : IDisposable
    {
        void Method1(object p1);
        void Method1(string p1);
        void Method2(string p1, int p2);
        void Method2(string p1);
        void Method3();
    }

    class TypeName : ICustomInterface
    {
        private ICollection<ICustomInterface> _disposables;

        public void Method1(object p1)
        {
        }

        public void Method1(string p1)
        {
            var a = 1;
        }

        public void Method2(string p1, int p2)
        {
            throw new NotImplementedException();
        }

        public void Method2(string p1)
        {
            throw new NotImplementedException();
        }

        public void Method3()
        {
            throw new NotImplementedException();
        }

        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }
}";

            var fixtest = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace ConsoleApplication1
{
    public interface ICustomInterface : IDisposable
    {
        void Method1(object p1);
        void Method1(string p1);
        void Method2(string p1, int p2);
        void Method2(string p1);
        void Method3();
    }

    class TypeName : ICustomInterface
    {
        private ICollection<ICustomInterface> _disposables;

        public void Method1(object p1)
        {
            foreach (var item in _disposables)
            {
                item.Method1(p1);
            }
        }

        public void Method1(string p1)
        {
            var a = 1;
        }

        public void Method2(string p1, int p2)
        {
            foreach (var item in _disposables)
            {
                item.Method2(p1, p2);
            }
        }

        public void Method2(string p1)
        {
            foreach (var item in _disposables)
            {
                item.Method2(p1);
            }
        }

        public void Method3()
        {
            foreach (var item in _disposables)
            {
                item.Method3();
            }
        }

        public void Dispose()
        {
            foreach (var item in _disposables)
            {
                item.Dispose();
            }
        }
    }
}";

            await new CodeFixTest <Analyzer, FixByForwardingToCollectionChildren>()
            {
                TestBehaviors       = TestBehaviors.SkipGeneratedCodeCheck,
                CompilerDiagnostics = CompilerDiagnostics.Suggestions,
                TestState           =
                {
                    Sources             = { test },
                    ExpectedDiagnostics =
                    {
                        // Test0.cs(11,22): warning CS1591: Missing XML comment for publicly visible type or member 'ICustomInterface'
                        DiagnosticResult.CompilerWarning("CS1591").WithSpan(11,                      22, 11,                      38)
                        .WithArguments("ConsoleApplication1.ICustomInterface"),
// Test0.cs(13,14): warning CS1591: Missing XML comment for publicly visible type or member 'ICustomInterface.Method1(object)'
                        DiagnosticResult.CompilerWarning("CS1591").WithSpan(13,                      14, 13,      21)
                        .WithArguments("ConsoleApplication1.ICustomInterface.Method1(object)"),
// Test0.cs(14,14): warning CS1591: Missing XML comment for publicly visible type or member 'ICustomInterface.Method1(string)'
                        DiagnosticResult.CompilerWarning("CS1591").WithSpan(14,                      14, 14,      21)
                        .WithArguments("ConsoleApplication1.ICustomInterface.Method1(string)"),
// Test0.cs(15,14): warning CS1591: Missing XML comment for publicly visible type or member 'ICustomInterface.Method2(string, int)'
                        DiagnosticResult.CompilerWarning("CS1591").WithSpan(15,                      14, 15, 21)
                        .WithArguments("ConsoleApplication1.ICustomInterface.Method2(string, int)"),
// Test0.cs(16,14): warning CS1591: Missing XML comment for publicly visible type or member 'ICustomInterface.Method2(string)'
                        DiagnosticResult.CompilerWarning("CS1591").WithSpan(16,                      14, 16,      21)
                        .WithArguments("ConsoleApplication1.ICustomInterface.Method2(string)"),
// Test0.cs(17,14): warning CS1591: Missing XML comment for publicly visible type or member 'ICustomInterface.Method3()'
                        DiagnosticResult.CompilerWarning("CS1591").WithSpan(17,                      14, 17,            21)
                        .WithArguments("ConsoleApplication1.ICustomInterface.Method3()"),
// Test0.cs(22,47): warning CS0169: The field 'TypeName._disposables' is never used
                        DiagnosticResult.CompilerWarning("CS0169").WithSpan(22,                      47, 22,                 59)
                        .WithArguments("ConsoleApplication1.TypeName._disposables"),
// Test0.cs(24,21): info ACA0001: Forward execution of "Method1" to member "_disposables"
                        Verifier.Diagnostic().WithSpan(24,                                           21, 24, 28).WithArguments("Method1", "_disposables"),
// Test0.cs(30,17): warning CS0219: The variable 'a' is assigned but its value is never used
                        DiagnosticResult.CompilerWarning("CS0219").WithSpan(30,                      17, 30,      18).WithArguments("a"),
// Test0.cs(33,21): info ACA0001: Forward execution of "Method2" to member "_disposables"
                        Verifier.Diagnostic().WithSpan(33,                                           21, 33, 28).WithArguments("Method2", "_disposables"),
// Test0.cs(38,21): info ACA0001: Forward execution of "Method2" to member "_disposables"
                        Verifier.Diagnostic().WithSpan(38,                                           21, 38, 28).WithArguments("Method2", "_disposables"),
// Test0.cs(43,21): info ACA0001: Forward execution of "Method3" to member "_disposables"
                        Verifier.Diagnostic().WithSpan(43,                                           21, 43, 28).WithArguments("Method3", "_disposables"),
// Test0.cs(48,21): info ACA0001: Forward execution of "Dispose" to member "_disposables"
                        Verifier.Diagnostic().WithSpan(48,                                           21, 48, 28).WithArguments("Dispose", "_disposables")
                    },
                },
                FixedState =
                {
                    ExpectedDiagnostics =
                    {
                        // Test0.cs(11,22): warning CS1591: Missing XML comment for publicly visible type or member 'ICustomInterface'
                        DiagnosticResult.CompilerWarning("CS1591").WithSpan(11,                           22, 11, 38)
                        .WithArguments("ConsoleApplication1.ICustomInterface"),
// Test0.cs(13,14): warning CS1591: Missing XML comment for publicly visible type or member 'ICustomInterface.Method1(object)'
                        DiagnosticResult.CompilerWarning("CS1591").WithSpan(13,                           14, 13, 21)
                        .WithArguments("ConsoleApplication1.ICustomInterface.Method1(object)"),
// Test0.cs(14,14): warning CS1591: Missing XML comment for publicly visible type or member 'ICustomInterface.Method1(string)'
                        DiagnosticResult.CompilerWarning("CS1591").WithSpan(14,                           14, 14, 21)
                        .WithArguments("ConsoleApplication1.ICustomInterface.Method1(string)"),
// Test0.cs(15,14): warning CS1591: Missing XML comment for publicly visible type or member 'ICustomInterface.Method2(string, int)'
                        DiagnosticResult.CompilerWarning("CS1591").WithSpan(15,                           14, 15, 21)
                        .WithArguments("ConsoleApplication1.ICustomInterface.Method2(string, int)"),
// Test0.cs(16,14): warning CS1591: Missing XML comment for publicly visible type or member 'ICustomInterface.Method2(string)'
                        DiagnosticResult.CompilerWarning("CS1591").WithSpan(16,                           14, 16, 21)
                        .WithArguments("ConsoleApplication1.ICustomInterface.Method2(string)"),
// Test0.cs(17,14): warning CS1591: Missing XML comment for publicly visible type or member 'ICustomInterface.Method3()'
                        DiagnosticResult.CompilerWarning("CS1591").WithSpan(17,                           14, 17, 21)
                        .WithArguments("ConsoleApplication1.ICustomInterface.Method3()"),
// Test0.cs(22,47): warning CS0649: Field 'TypeName._disposables' is never assigned to, and will always have its default value null
                        DiagnosticResult.CompilerWarning("CS0649").WithSpan(22,                           47, 22, 59)
                        .WithArguments("ConsoleApplication1.TypeName._disposables",                  "null"),
// Test0.cs(34,17): warning CS0219: The variable 'a' is assigned but its value is never used
                        DiagnosticResult.CompilerWarning("CS0219").WithSpan(34,                           17, 34, 18).WithArguments("a")
                    },
                    Sources             ={ fixtest }
                },
            }.RunAsync();
        }
コード例 #13
0
    public async Task NullableReferenceTypesSuppressed()
    {
        const string test = @"
#nullable enable

using UnityEngine;

public class TestScript : MonoBehaviour
{
	private UnityEngine.Object field1;
	private GameObject field2;
	public UnityEngine.Object field3;

	public UnityEngine.Object Property1 { get; set; }
	public UnityEngine.Object Property2 { get { return property2Field; } set { property2Field = value; } }
	private UnityEngine.Object property2Field;

	[SerializeField] private GameObject serializedField;

	public static GameObject staticField;

	[HideInInspector] public GameObject hiddenField;
	[HideInInspector] public GameObject hiddenField2;

    private void Start()
    {
		field1 = new UnityEngine.Object();
		InitializeField2();

		Property1 = new UnityEngine.Object();
		Property2 = new UnityEngine.Object();

		staticField = new GameObject();

		hiddenField2 = new GameObject();
    }

	private void InitializeField2()
	{
		field2 = new GameObject();
	}
}
";

        DiagnosticResult[] suppressors =
        {
            ExpectSuppressor(NullableReferenceTypesSuppressor.Rule).WithLocation(8, 29),             //field1
            ExpectSuppressor(NullableReferenceTypesSuppressor.Rule).WithLocation(9, 21),             //field2
            ExpectSuppressor(NullableReferenceTypesSuppressor.Rule).WithLocation(10, 28),            //field3
            ExpectSuppressor(NullableReferenceTypesSuppressor.Rule).WithLocation(12, 28),            //Property1
            ExpectSuppressor(NullableReferenceTypesSuppressor.Rule).WithLocation(14, 29),            //property2Field
            ExpectSuppressor(NullableReferenceTypesSuppressor.Rule).WithLocation(16, 38),            //serializedField

            ExpectSuppressor(NullableReferenceTypesSuppressor.Rule).WithLocation(18, 27),            //staticField

            DiagnosticResult.CompilerWarning(NullableReferenceTypesSuppressor.Rule.SuppressedDiagnosticId)
            .WithMessageFormat(WarningFormat)
            .WithArguments("field", "hiddenField")
            .WithLocation(20, 38),                     //should throw on public fields that are not shown in the inspector

            ExpectSuppressor(NullableReferenceTypesSuppressor.Rule).WithLocation(21, 38)
        };

        var context = AnalyzerVerificationContext.Default
                      .WithLanguageVersion(LanguageVersion.CSharp8)
                      .WithAnalyzerFilter("CS0169");

        await VerifyCSharpDiagnosticAsync(context, test, suppressors);
    }