diff --git a/src/SimpleInjector.Tests.Unit/Diagnostics/DisposableTransientComponentsTests.cs b/src/SimpleInjector.Tests.Unit/Diagnostics/DisposableTransientComponentsTests.cs index 8c2898d80..994e5492d 100644 --- a/src/SimpleInjector.Tests.Unit/Diagnostics/DisposableTransientComponentsTests.cs +++ b/src/SimpleInjector.Tests.Unit/Diagnostics/DisposableTransientComponentsTests.cs @@ -25,6 +25,28 @@ public void Analyze_TransientRegistrationForDisposableComponent_Warning() container.Verify(VerificationOption.VerifyOnly); + // Act + var results = Analyzer.Analyze(container).OfType() + .ToArray(); + + // Assert + Assert.AreEqual(1, results.Length, Actual(results)); + Assert.AreEqual( + "DisposablePlugin is registered for IPlugin as transient, but implements IDisposable.", + results.Single().Description, + Actual(results)); + } + + [TestMethod] + public void Analyze_TransientRegistrationForConcreteDisposableComponent_Warning() + { + // Arrange + var container = new Container(); + + container.Register(); + + container.Verify(VerificationOption.VerifyOnly); + // Act var results = Analyzer.Analyze(container).OfType() .ToArray(); @@ -54,7 +76,7 @@ public void Analyze_TransientRegistrationForAsyncDisposableComponent_Warning() // Assert Assert.AreEqual(1, results.Length, Actual(results)); Assert.AreEqual( - "AsyncDisposablePlugin is registered as transient, but implements IAsyncDisposable.", + "AsyncDisposablePlugin is registered for IPlugin as transient, but implements IAsyncDisposable.", results.Single().Description, Actual(results)); } @@ -76,7 +98,7 @@ public void Analyze_TransientRegistrationForComponentImplementingBothIDisposable // Assert Assert.AreEqual(1, results.Length, Actual(results)); Assert.AreEqual( - "DisposableAsyncDisposablePlugin is registered as transient, but implements IDisposable.", + "DisposableAsyncDisposablePlugin is registered for IPlugin as transient, but implements IDisposable.", results.Single().Description, Actual(results)); } diff --git a/src/SimpleInjector/Diagnostics/Analyzers/DisposableTransientComponentAnalyzer.cs b/src/SimpleInjector/Diagnostics/Analyzers/DisposableTransientComponentAnalyzer.cs index 2a64069e5..3c07602b5 100644 --- a/src/SimpleInjector/Diagnostics/Analyzers/DisposableTransientComponentAnalyzer.cs +++ b/src/SimpleInjector/Diagnostics/Analyzers/DisposableTransientComponentAnalyzer.cs @@ -37,17 +37,22 @@ where registration.ShouldNotBeSuppressed(this.DiagnosticType) var results = from producer in invalidProducers select new DisposableTransientComponentDiagnosticResult( - producer.ServiceType, producer, BuildDescription(producer.Registration.ImplementationType)); + producer.ServiceType, producer, BuildDescription(producer)); return results.ToArray(); } - private static string BuildDescription(Type implementationType) => + private static string BuildDescription(InstanceProducer producer) => string.Format( CultureInfo.InvariantCulture, - "{0} is registered as transient, but implements {1}.", - implementationType.FriendlyName(), - typeof(IDisposable).IsAssignableFrom(implementationType) ? "IDisposable" : "IAsyncDisposable"); + "{0} is registered {1}as transient, but implements {2}.", + producer.Registration.ImplementationType.FriendlyName(), + producer.ServiceType == producer.Registration.ImplementationType + ? string.Empty + : $"for {producer.ServiceType.FriendlyName()} ", + typeof(IDisposable).IsAssignableFrom(producer.Registration.ImplementationType) + ? "IDisposable" + : "IAsyncDisposable"); private static string ComponentPlural(int number) => number == 1 ? "component" : "components"; }