Skip to content

Commit af987b9

Browse files
committed
WIP
1 parent a5ab4b8 commit af987b9

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

IDisposableAnalyzers.Test/AssemblyAttributes.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
typeof(System.Xaml.XamlLanguage),
1818
typeof(Moq.Mock<>),
1919
typeof(Ninject.KernelConfiguration),
20+
typeof(System.Windows.Forms.Form),
2021
typeof(Gu.Roslyn.AnalyzerExtensions.SyntaxTokenExt),
2122
typeof(Gu.Roslyn.CodeFixExtensions.Parse),
2223
typeof(NUnit.Framework.Assert))]

IDisposableAnalyzers.Test/Helpers/DisposableWalkerTests.Disposes.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,33 @@ internal C(string fileName)
144144
Assert.AreEqual(true, semanticModel.TryGetSymbol(value, CancellationToken.None, out ILocalSymbol symbol));
145145
Assert.AreEqual(true, DisposableWalker.Disposes(symbol, semanticModel, CancellationToken.None));
146146
}
147+
148+
[Test]
149+
public static void WhenAddedToFormComponents()
150+
{
151+
var code = @"
152+
namespace ValidCode
153+
{
154+
using System.IO;
155+
using System.Windows.Forms;
156+
157+
public class Winform : Form
158+
{
159+
Winform()
160+
{
161+
var stream = File.OpenRead(string.Empty);
162+
// Since this is added to components, it is automatically disposed of with the form.
163+
this.components.Add(stream);
164+
}
165+
}
166+
}";
167+
var syntaxTree = CSharpSyntaxTree.ParseText(code);
168+
var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
169+
var semanticModel = compilation.GetSemanticModel(syntaxTree);
170+
var value = syntaxTree.FindVariableDeclaration("stream = File.OpenRead(string.Empty)");
171+
Assert.AreEqual(true, semanticModel.TryGetSymbol(value, CancellationToken.None, out ILocalSymbol symbol));
172+
Assert.AreEqual(true, DisposableWalker.Disposes(symbol, semanticModel, CancellationToken.None));
173+
}
147174
}
148175
}
149176
}

IDisposableAnalyzers.Test/IDISP004DoNotIgnoreCreatedTests/Valid.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,28 @@ public async Task M()
787787
await System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(() => Task.FromResult(42)).ConfigureAwait(false);
788788
}
789789
}
790+
}";
791+
RoslynAssert.Valid(Analyzer, code);
792+
}
793+
794+
[Test]
795+
public static void WhenAddedToFormComponents()
796+
{
797+
var code = @"
798+
namespace ValidCode
799+
{
800+
using System.IO;
801+
using System.Windows.Forms;
802+
803+
public class Winform : Form
804+
{
805+
Winform()
806+
{
807+
var stream = File.OpenRead(string.Empty);
808+
// Since this is added to components, it is automatically disposed of with the form.
809+
this.components.Add(stream);
810+
}
811+
}
790812
}";
791813
RoslynAssert.Valid(Analyzer, code);
792814
}

0 commit comments

Comments
 (0)