public static void AssigningFieldAwait(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 = await 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(false, Disposable.Ignores(value, semanticModel, CancellationToken.None)); }
////[TestCase("disposable ?? disposable")] ////[TestCase("true ? disposable : (IDisposable)null")] ////[TestCase("Tuple.Create(disposable, 1)")] ////[TestCase("(disposable, 1)")] ////[TestCase("new Tuple<IDisposable, int>(disposable, 1)")] ////[TestCase("new List<IDisposable> { disposable }")] ////[TestCase("new List<IDisposable>() { disposable }")] ////[TestCase("new List<IDisposable> { disposable, null }")] public static void ArgumentAssignedToTempLocal(string expression) { var code = @" namespace N { using System; using System.IO; public class C { public static void M(string fileName) { var value = M(File.OpenRead(fileName)); } public static int M(IDisposable disposable) { var temp = new List<IDisposable> { disposable }; return 1; } } }".AssertReplace("new List<IDisposable> { disposable }", 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.Ignores(value, semanticModel, CancellationToken.None)); }
public static void CtorArgAssignedNotAssigned() { var code = @" namespace N { using System; using System.IO; public class C : IDisposable { public C(IDisposable disposable) { } public void Dispose() { } public static void M(string fileName) { var c = new C(File.OpenRead(fileName)); } } }"; 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.Ignores(value, semanticModel, CancellationToken.None)); }
public static void AssignedToFieldAsync(string expression) { var code = @" namespace N { using System; using System.IO; public class C { private readonly object value; public static async Task M(string fileName) { this.value = await Task.Run(() => File.OpenRead(fileName)); } } }".AssertReplace("Task.Run(() => 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(false, Disposable.Ignores(value, semanticModel, CancellationToken.None)); }
public static void CompositeDisposableExtAddAndReturn(string expression) { var code = @" namespace N { using System; using System.IO; using System.Reactive.Disposables; public static class CompositeDisposableExt { public static T AddAndReturn<T>(this CompositeDisposable disposable, T item) where T : IDisposable { if (item != null) { disposable.Add(item); } return item; } } public sealed class C : IDisposable { private readonly CompositeDisposable disposable = new CompositeDisposable(); public void Dispose() { this.disposable.Dispose(); } internal object M(string fileName) { return this.disposable.AddAndReturn(File.OpenRead(fileName)); } } }".AssertReplace("disposable.AddAndReturn(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(false, Disposable.Ignores(value, semanticModel, CancellationToken.None)); }
public static void FileOpenReadReadAsync(string expression) { var code = @" namespace N { using System.IO; using System.Threading.Tasks; public class C { public async Task<int> M() => await File.OpenRead(string.Empty).ReadAsync(null, 0, 0); } }".AssertReplace("await File.OpenRead(string.Empty).ReadAsync(null, 0, 0)", 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(string.Empty)"); Assert.AreEqual(true, Disposable.Ignores(value, semanticModel, CancellationToken.None)); }
public static void ArgumentPassedTo(string expression) { var code = @" namespace N { using System; using System.IO; public static class C { public static object M() => string.Format(""{0}"", File.OpenRead(fileName)); } }".AssertReplace("string.Format(\"{0}\", 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.Ignores(value, semanticModel, CancellationToken.None)); }
public static void ReturnedExpressionBody() { var code = @" namespace N { using System; using System.IO; internal class C { internal IDisposable C(string fileName) => File.OpenRead(fileName); } }"; 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(false, Disposable.Ignores(value, semanticModel, CancellationToken.None)); }
public static void ReturnedInCompositeDisposable(string expression) { var code = @" namespace N { using System; using System.IO; using System.Reactive.Disposables; public class C { public static IDisposable M(string fileName) => new CompositeDisposable(File.OpenRead(fileName)); } }".AssertReplace("new CompositeDisposable(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(false, Disposable.Ignores(value, semanticModel, CancellationToken.None)); }
public static void ReturnedStreamWrappedInStreamReader(string expression) { var code = @" namespace N { using System.IO; public static class C { public StreamReader M1() => File.OpenRead(string.Empty).M2(); private static StreamReader M2(this Stream stream) => new StreamReader(stream); } }".AssertReplace("File.OpenRead(string.Empty).M2()", 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(string.Empty)"); Assert.AreEqual(false, Disposable.Ignores(value, semanticModel, CancellationToken.None)); }
public static void HostBuildRun() { var code = @" namespace N { using Microsoft.Extensions.Hosting; public class Program { public static void Main(string[] args) { Host.CreateDefaultBuilder(args).Build().Run(); } } }"; var syntaxTree = CSharpSyntaxTree.ParseText(code); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes()); var semanticModel = compilation.GetSemanticModel(syntaxTree); var value = syntaxTree.FindExpression("Host.CreateDefaultBuilder(args).Build()"); Assert.AreEqual(false, Disposable.Ignores(value, semanticModel, CancellationToken.None)); }
public static void DiscardedWrapped(string expression) { var code = @" namespace N { using System; using System.Collections.Generic; using System.IO; public class C : IDisposable { private readonly IDisposable disposable; public C(IDisposable disposable) { this.disposable = disposable; } public static void M(string fileName) { new C(File.OpenRead(fileName)); } public static void M(IDisposable _) { } public void Dispose() { this.disposable.Dispose(); } } }".AssertReplace("new C(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.Ignores(value, semanticModel, CancellationToken.None)); }
public static void UsingDeclaration(string expression) { var code = @" namespace N { using System; using System.IO; using System.Threading.Tasks; class C { C(string fileName) { using var disposable = File.OpenRead(fileName); } } }".AssertReplace("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(false, Disposable.Ignores(value, semanticModel, CancellationToken.None)); }