From 570d59648db2024f9b9c2dd8eff57dfb1f8abdc3 Mon Sep 17 00:00:00 2001 From: Manfred Brands Date: Tue, 17 Oct 2023 12:37:20 +0800 Subject: [PATCH] Also look at static methods for NUnit1032 --- ...ldsAndPropertiesInTearDownAnalyzerTests.cs | 50 +++++++++++++++++++ ...seFieldsAndPropertiesInTearDownAnalyzer.cs | 4 +- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/nunit.analyzers.tests/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzerTests.cs b/src/nunit.analyzers.tests/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzerTests.cs index 89ad2065..c47d0446 100644 --- a/src/nunit.analyzers.tests/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzerTests.cs +++ b/src/nunit.analyzers.tests/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzerTests.cs @@ -830,5 +830,55 @@ private void B(string one, bool keepGoing) RoslynAssert.Valid(analyzer, testCode); } + + [TestCase("")] + [TestCase("static")] + public void StaticFieldsNeedDisposingInOneTimeTearDown(string modifier) + { + var testCode = TestUtility.WrapClassInNamespaceAndAddUsing($@" + public {modifier} class StaticFields + {{ + private static readonly IDisposable? ↓field = new DummyDisposable(); + + [Test] + public {modifier} void TestMethod() + {{ + Assert.That(field, Is.Not.Null); + }} + + {DummyDisposable} + }} + "); + + RoslynAssert.Diagnostics(analyzer, expectedDiagnostic, testCode); + } + + [TestCase("")] + [TestCase("static")] + public void StaticFieldsAreDisposed(string modifier) + { + var testCode = TestUtility.WrapClassInNamespaceAndAddUsing($@" + public {modifier} class StaticFields + {{ + private static readonly IDisposable? field = new DummyDisposable(); + + [OneTimeTearDown] + public {modifier} void OneTimeTearDown() + {{ + field?.Dispose(); + }} + + [Test] + public {modifier} void TestMethod() + {{ + Assert.That(field, Is.Not.Null); + }} + + {DummyDisposable} + }} + "); + + RoslynAssert.Valid(analyzer, testCode); + } } } diff --git a/src/nunit.analyzers/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzer.cs b/src/nunit.analyzers/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzer.cs index 988433a1..14843e26 100644 --- a/src/nunit.analyzers/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzer.cs +++ b/src/nunit.analyzers/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzer.cs @@ -113,7 +113,7 @@ private static void AnalyzeDisposableFields(SyntaxNodeAnalysisContext context) ImmutableHashSet disposeMethods = StandardDisposeMethods; #if NETSTANDARD2_0_OR_GREATER - // Are there any additional methods configured that are considers Dispose Methods + // Are there any additional methods configured that are considered Dispose Methods // e.g. DisposeIfDisposeable or Release AnalyzerConfigOptions options = context.Options.AnalyzerConfigOptionsProvider.GetOptions(classDeclaration.SyntaxTree); if (options.TryGetValue("dotnet_diagnostic.NUnit1032.additional_dispose_methods", out string? value)) @@ -127,7 +127,7 @@ private static void AnalyzeDisposableFields(SyntaxNodeAnalysisContext context) Parameters parameters = new(model, typeSymbol, disposeMethods, symbolNames); ImmutableArray members = typeSymbol.GetMembers(); - var methods = members.OfType().Where(m => !m.IsStatic).ToArray(); + var methods = members.OfType().ToArray(); var oneTimeTearDownMethods = methods.Where(m => HasAttribute(m, NUnitFrameworkConstants.NameOfOneTimeTearDownAttribute)).ToImmutableHashSet(SymbolEqualityComparer.Default); var oneTimeSetUpMethods = methods.Where(m => m.MethodKind == MethodKind.Constructor || HasAttribute(m, NUnitFrameworkConstants.NameOfOneTimeSetUpAttribute)).ToImmutableHashSet(SymbolEqualityComparer.Default); var setUpMethods = methods.Where(m => HasAttribute(m, NUnitFrameworkConstants.NameOfSetUpAttribute)).ToImmutableHashSet(SymbolEqualityComparer.Default);