diff --git a/src/nunit.analyzers.tests/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzerTests.cs b/src/nunit.analyzers.tests/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzerTests.cs index 1be4a7d7..2dbabdff 100644 --- a/src/nunit.analyzers.tests/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzerTests.cs +++ b/src/nunit.analyzers.tests/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzerTests.cs @@ -1034,6 +1034,8 @@ public partial class TestFixture {{ private const double Tolerance = 1E-10; + private IDisposable? field; + private static IDisposable? Property {{ get; set; }} [SetUp] @@ -1059,6 +1061,8 @@ public partial class TestFixture [Test] public void SomeTest() { + // field needs Disposal, but as TearDown is in a different source 'file' we cannot check if it is. + field = new DummyDisposable(); SomeAsserts(); } }"); diff --git a/src/nunit.analyzers/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzer.cs b/src/nunit.analyzers/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzer.cs index aec9123b..6c4b8606 100644 --- a/src/nunit.analyzers/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzer.cs +++ b/src/nunit.analyzers/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzer.cs @@ -366,6 +366,12 @@ private static bool NeedsDisposal(SemanticModel model, ExpressionSyntax expressi { if (IsPossibleDisposableCreation(expression)) { + if (model.SyntaxTree != expression.SyntaxTree) + { + // It might need it, but we cannot check it. + return false; + } + ITypeSymbol? instanceType = model.GetTypeInfo(expression).Type; return instanceType is not null && instanceType.IsDisposable() &&