public static void DisposedByReturnValueCallWrappingStreamInReader() { var code = @" namespace N { using System.IO; public class C { public string M() { using (var reader = GetReader(File.OpenRead(string.Empty))) { return reader.ReadLine(); } } private static StreamReader GetReader(Stream stream) { return new StreamReader(stream); } } }"; var syntaxTree = CSharpSyntaxTree.ParseText(code); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes()); var semanticModel = compilation.GetSemanticModel(syntaxTree); var value = syntaxTree.FindArgument("File.OpenRead(string.Empty)"); Assert.AreEqual(true, Disposable.DisposedByReturnValue(value, semanticModel, CancellationToken.None, out _)); }
public static void InLeaveOpen(string expression, bool stores) { var code = @" namespace N { using System; using System.IO; using System.IO.Compression; using System.Security.Cryptography; using System.Text; public class C { private readonly IDisposable disposable; public C(Stream stream) { this.disposable = new BinaryReader(stream); } } }".AssertReplace("new BinaryReader(stream)", expression); var syntaxTree = CSharpSyntaxTree.ParseText(code); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes()); var semanticModel = compilation.GetSemanticModel(syntaxTree); var value = syntaxTree.FindParameter("stream"); Assert.AreEqual(true, semanticModel.TryGetSymbol(value, CancellationToken.None, out var symbol)); Assert.AreEqual(true, LocalOrParameter.TryCreate(symbol, out var localOrParameter)); Assert.AreEqual(stores, Disposable.Stores(localOrParameter, semanticModel, CancellationToken.None, out var container)); Assert.AreEqual(stores, Disposable.DisposedByReturnValue(syntaxTree.FindArgument("stream"), semanticModel, CancellationToken.None, out _)); if (stores) { Assert.AreEqual("N.C.disposable", container.ToString()); } }
public static void InHttpClient(string expression, bool stores) { var code = @" namespace N { using System.Net.Http; public class C { private readonly IDisposable disposable; public C(HttpClientHandler handler) { this.disposable = new HttpClient(handler); } } }".AssertReplace("new HttpClient(handler)", expression); var syntaxTree = CSharpSyntaxTree.ParseText(code); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes()); var semanticModel = compilation.GetSemanticModel(syntaxTree); var value = syntaxTree.FindParameter("handler"); Assert.AreEqual(true, semanticModel.TryGetSymbol(value, CancellationToken.None, out var symbol)); Assert.AreEqual(true, LocalOrParameter.TryCreate(symbol, out var localOrParameter)); Assert.AreEqual(stores, Disposable.Stores(localOrParameter, semanticModel, CancellationToken.None, out var container)); Assert.AreEqual(stores, Disposable.DisposedByReturnValue(syntaxTree.FindArgument("handler"), semanticModel, CancellationToken.None, out _)); if (stores) { Assert.AreEqual("N.C.disposable", container.ToString()); } }
public static void FactoryMethod() { var code = @" namespace N { using System; using System.IO; public sealed class Disposer : IDisposable { private readonly Stream stream; private Disposer(Stream stream) { this.stream = stream; } public static Disposer M() => new Disposer(File.OpenRead(string.Empty)); public void Dispose() { this.stream.Dispose(); } } }"; var syntaxTree = CSharpSyntaxTree.ParseText(code); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes()); var semanticModel = compilation.GetSemanticModel(syntaxTree); var value = syntaxTree.FindArgument("File.OpenRead(string.Empty)"); Assert.AreEqual(true, Disposable.DisposedByReturnValue(value, semanticModel, CancellationToken.None, out _)); }