コード例 #1
0
            public static void AssigningFieldGetAwaiterGetResult(string expression)
            {
                var code          = @"
namespace N
{
    using System;
    using System.IO;
    using System.Threading.Tasks;

    public sealed class C : IDisposable
    {
        private IDisposable disposable;

        public async Task M(string fileName)
        {
            this.disposable?.Dispose();
            this.disposable = Task.FromResult(File.OpenRead(fileName));
        }

        public void Dispose()
        {
            this.disposable?.Dispose();
        }
    }
}".AssertReplace("Task.FromResult(File.OpenRead(fileName))", expression);
                var syntaxTree    = CSharpSyntaxTree.ParseText(code);
                var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
                var semanticModel = compilation.GetSemanticModel(syntaxTree);
                var value         = syntaxTree.FindExpression("File.OpenRead(fileName)");

                Assert.AreEqual(true, Disposable.Assigns(value, semanticModel, CancellationToken.None, out var fieldOrProperty));
                Assert.AreEqual("disposable", fieldOrProperty.Name);
            }
コード例 #2
0
            public static void FieldAssignedViaCalledMethodParameter()
            {
                var code          = @"
namespace N
{
    using System;

    internal class C
    {
        private IDisposable disposable;

        internal C(IDisposable disposable)
        {
            this.M(disposable);
        }

        private void M(IDisposable disposable)
        {
            this.disposable = disposable;
        }
    }
}";
                var syntaxTree    = CSharpSyntaxTree.ParseText(code);
                var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
                var semanticModel = compilation.GetSemanticModel(syntaxTree);
                var value         = syntaxTree.FindParameter("IDisposable disposable");
                var symbol        = semanticModel.GetDeclaredSymbol(value, CancellationToken.None);

                Assert.AreEqual(true, LocalOrParameter.TryCreate(symbol, out var localOrParameter));
                Assert.AreEqual(true, Disposable.Assigns(localOrParameter, semanticModel, CancellationToken.None, out var field));
                Assert.AreEqual("N.C.disposable", field.Symbol.ToString());
            }
コード例 #3
0
            public static void WhenNotUsed()
            {
                var code          = @"
namespace N
{
    using System;

    internal class C
    {
        internal C(IDisposable disposable)
        {
        }
    }
}";
                var syntaxTree    = CSharpSyntaxTree.ParseText(code);
                var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
                var semanticModel = compilation.GetSemanticModel(syntaxTree);
                var value         = syntaxTree.FindParameter("IDisposable disposable");
                var symbol        = semanticModel.GetDeclaredSymbol(value, CancellationToken.None);

                Assert.AreEqual(true, LocalOrParameter.TryCreate(symbol, out var localOrParameter));
                Assert.AreEqual(false, Disposable.Assigns(localOrParameter, semanticModel, CancellationToken.None, out _));
            }