Skip to content

Commit 0517d5e

Browse files
committed
Don't report inconsitencies if lifetimes match
We automatically deduplicate service registrations that are exactly the same, so we don't need to report that case.
1 parent fa1b5bd commit 0517d5e

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

src/CodeAnalysis.Tests/ConventionAnalyzerTests.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,58 @@ public static void Main()
214214

215215
await test.RunAsync();
216216
}
217+
218+
[Fact]
219+
public async Task NoWarnIfMultipleSameLifetime()
220+
{
221+
var test = new CSharpSourceGeneratorTest<IncrementalGenerator, DefaultVerifier>
222+
{
223+
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck,
224+
TestCode =
225+
"""
226+
using System;
227+
using Microsoft.Extensions.DependencyInjection;
228+
229+
public interface IRepository { }
230+
231+
[Service]
232+
public class MyRepository : IRepository { }
233+
234+
public static class Program
235+
{
236+
public static void Main()
237+
{
238+
var services = new ServiceCollection();
239+
services.AddServices(typeof(IRepository));
240+
}
241+
}
242+
""",
243+
TestState =
244+
{
245+
AnalyzerConfigFiles =
246+
{
247+
("/.editorconfig",
248+
"""
249+
is_global = true
250+
build_property.AddServicesExtension = true
251+
""")
252+
},
253+
Sources =
254+
{
255+
StaticGenerator.AddServicesExtension,
256+
StaticGenerator.ServiceAttribute,
257+
StaticGenerator.ServiceAttributeT,
258+
},
259+
ReferenceAssemblies = new ReferenceAssemblies(
260+
"net8.0",
261+
new PackageIdentity(
262+
"Microsoft.NETCore.App.Ref", "8.0.0"),
263+
Path.Combine("ref", "net8.0"))
264+
.AddPackages(ImmutableArray.Create(
265+
new PackageIdentity("Microsoft.Extensions.DependencyInjection", "8.0.0")))
266+
},
267+
};
268+
269+
await test.RunAsync();
270+
}
217271
}

src/DependencyInjection.Tests/GenerationTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,9 @@ public class SmsNotificationService : INotificationService
381381

382382
// Showcases that legacy generic Service<TKey> attribute still works
383383
[Service("email")]
384+
#pragma warning disable CS0618 // Type or member is obsolete
384385
[Service<string>("default")]
386+
#pragma warning restore CS0618 // Type or member is obsolete
385387
public class EmailNotificationService : INotificationService
386388
{
387389
public string Notify(string message) => $"[Email] {message}";

src/DependencyInjection/IncrementalGenerator.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,18 @@ void ReportInconsistencies(SourceProductionContext context, ImmutableArray<Servi
283283
// report if within the group, there are different lifetimes with the same key (or no key)
284284
foreach (var keyed in group.GroupBy(x => x.Key?.Value).Where(g => g.Count() > 1))
285285
{
286-
var lifetimes = string.Join(", ", keyed.Select(x => x.Lifetime).Distinct()
287-
.Select(x => x switch { 0 => "Singleton", 1 => "Scoped", 2 => "Transient", _ => "Unknown" }));
286+
var lifetimes = keyed.Select(x => x.Lifetime).Distinct()
287+
.Select(x => x switch { 0 => "Singleton", 1 => "Scoped", 2 => "Transient", _ => "Unknown" })
288+
.ToArray();
289+
290+
if (lifetimes.Length == 1)
291+
continue;
288292

289293
var location = keyed.Where(x => x.Location != null).FirstOrDefault()?.Location;
290294
var otherLocations = keyed.Where(x => x.Location != null).Skip(1).Select(x => x.Location!);
291295

292296
context.ReportDiagnostic(Diagnostic.Create(AmbiguousLifetime,
293-
location, otherLocations, keyed.First().Type.ToDisplayString(), lifetimes));
297+
location, otherLocations, keyed.First().Type.ToDisplayString(), string.Join(", ", lifetimes)));
294298
}
295299
}
296300
}

0 commit comments

Comments
 (0)