From 989971453d861341169f8f7452005c5d4f9b143b Mon Sep 17 00:00:00 2001 From: Manfred Brands Date: Mon, 4 Dec 2023 11:36:31 +0800 Subject: [PATCH] Ignore explicit implementations of properties This to cope with cases where a field and an explicit property have the same name. --- ...ldsAndPropertiesInTearDownAnalyzerTests.cs | 38 +++++++++++++++++++ ...seFieldsAndPropertiesInTearDownAnalyzer.cs | 3 ++ 2 files changed, 41 insertions(+) diff --git a/src/nunit.analyzers.tests/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzerTests.cs b/src/nunit.analyzers.tests/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzerTests.cs index e127d0df..76ce0981 100644 --- a/src/nunit.analyzers.tests/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzerTests.cs +++ b/src/nunit.analyzers.tests/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzerTests.cs @@ -1013,5 +1013,43 @@ public void SomeTest() RoslynAssert.Valid(analyzer, testCodePart1, testCodePart2); } + + [Test] + public void DoesNotThrowExceptionsWhenUsingExplicitProperties() + { + var testCode = TestUtility.WrapClassInNamespaceAndAddUsing(@" + [TestFixture] + public abstract class Test : IDataProvider + { + private DataReader DataReader; + + DataReader IDataProvider.DataReader + { + get => DataReader; + set => DataReader = value; + } + + protected Test() => DataReader = new DataReader(this); + + [OneTimeSetUp] + public void SetListener() => throw new NotImplementedException(); + } + + public interface IDataProvider + { + DataReader DataReader + { + get; + set; + } + } + + public sealed class DataReader + { + public DataReader(IDataProvider provider) => throw new NotImplementedException(); + }"); + + RoslynAssert.Valid(analyzer, testCode); + } } } diff --git a/src/nunit.analyzers/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzer.cs b/src/nunit.analyzers/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzer.cs index 60fbc4d1..068eed30 100644 --- a/src/nunit.analyzers/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzer.cs +++ b/src/nunit.analyzers/DisposeFieldsAndPropertiesInTearDown/DisposeFieldsAndPropertiesInTearDownAnalyzer.cs @@ -115,6 +115,9 @@ private static void AnalyzeDisposableFields(SyntaxNodeAnalysisContext context) foreach (var property in propertyDeclarations) { + if (property.ExplicitInterfaceSpecifier is not null) + continue; + if (property.Initializer is not null) { if (NeedsDisposal(model, property.Initializer.Value))