public static void FieldAddedToFormComponents(string expression)
        {
            var syntaxTree    = CSharpSyntaxTree.ParseText(@"
namespace N
{
    using System.IO;
    using System.Windows.Forms;

    public class Winform : Form
    {
        private readonly Stream stream;

        Winform()
        {
            this.stream = File.OpenRead(string.Empty);
            // Since this is added to components, it is automatically disposed of with the form.
            this.components.Add(this.stream);
        }
    }
}".AssertReplace("this.components.Add(this.stream)", expression));
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var declaration   = syntaxTree.FindFieldDeclaration("stream");
            var symbol        = semanticModel.GetDeclaredSymbolSafe(declaration, CancellationToken.None);

            Assert.AreEqual(Result.Yes, DisposableMember.IsDisposed(new FieldOrPropertyAndDeclaration(symbol, declaration), semanticModel, CancellationToken.None));
        }
Esempio n. 2
0
        public static void SimpleFieldIAsyncDisposable()
        {
            var syntaxTree    = CSharpSyntaxTree.ParseText(@"
namespace N
{
    using System;
    using System.IO;
    using System.Threading.Tasks;

    public class C : IAsyncDisposable
    {
        private readonly IAsyncDisposable disposable = File.OpenRead(string.Empty);

        public async ValueTask DisposeAsync()
        {
            await this.disposable.DisposeAsync();
        }
    }
}");
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var declaration   = syntaxTree.FindFieldDeclaration("disposable");
            var symbol        = semanticModel.GetDeclaredSymbolSafe(declaration, CancellationToken.None);

            Assert.AreEqual(Result.Yes, DisposableMember.IsDisposed(new FieldOrPropertyAndDeclaration(symbol, declaration), semanticModel, CancellationToken.None));
        }
        public static void DisposedInOverridden()
        {
            var syntaxTree    = CSharpSyntaxTree.ParseText(@"
namespace N
{
    using System;
    using System.IO;

    abstract class BaseClass : IDisposable
    {
        public void Dispose()
        {
            this.M();
        }

        protected abstract void M();
    }

    class C : BaseClass
    {
        private readonly IDisposable stream = File.OpenRead(string.Empty);

        protected override void M()
        {
            this.stream.Dispose();
        }
    }
}");
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var declaration   = syntaxTree.FindFieldDeclaration("stream");
            var symbol        = semanticModel.GetDeclaredSymbolSafe(declaration, CancellationToken.None);

            Assert.AreEqual(Result.Yes, DisposableMember.IsDisposed(new FieldOrPropertyAndDeclaration(symbol, declaration), semanticModel, CancellationToken.None));
        }
        public static void DisposedInExplicitImplementation()
        {
            var syntaxTree    = CSharpSyntaxTree.ParseText(@"
namespace N
{
    using System;
    using System.IO;

    sealed class C : IDisposable
    {
        private readonly IDisposable stream = File.OpenRead(string.Empty);

        void IDisposable.Dispose()
        {
            this.stream.Dispose();
        }
    }
}");
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var declaration   = syntaxTree.FindFieldDeclaration("stream");
            var symbol        = semanticModel.GetDeclaredSymbolSafe(declaration, CancellationToken.None);
            var field         = new FieldOrPropertyAndDeclaration(symbol, declaration);

            Assert.AreEqual(Result.Yes, DisposableMember.IsDisposed(field, semanticModel, CancellationToken.None));
        }