diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/ImmutableCollections/DoNotCallToImmutableCollectionOnAnImmutableCollectionValue.cs b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/ImmutableCollections/DoNotCallToImmutableCollectionOnAnImmutableCollectionValue.cs index d91e660a60..7f23c6f9ad 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/ImmutableCollections/DoNotCallToImmutableCollectionOnAnImmutableCollectionValue.cs +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/ImmutableCollections/DoNotCallToImmutableCollectionOnAnImmutableCollectionValue.cs @@ -92,7 +92,8 @@ public override void Initialize(AnalysisContext context) } if (invocation.GetReceiverType(operationContext.Compilation, beforeConversion: true, cancellationToken: operationContext.CancellationToken) is INamedTypeSymbol receiverType - && receiverType.DerivesFromOrImplementsAnyConstructionOf(immutableCollectionType)) + && receiverType.DerivesFromOrImplementsAnyConstructionOf(immutableCollectionType) + && ((INamedTypeSymbol)invocation.Type!).TypeArguments[0].Equals(receiverType.TypeArguments[0])) { operationContext.ReportDiagnostic( invocation.CreateDiagnostic( diff --git a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/ImmutableCollections/DoNotCallToImmutableCollectionOnAnImmutableCollectionValueTests.cs b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/ImmutableCollections/DoNotCallToImmutableCollectionOnAnImmutableCollectionValueTests.cs index d42c6504f1..f553d45484 100644 --- a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/ImmutableCollections/DoNotCallToImmutableCollectionOnAnImmutableCollectionValueTests.cs +++ b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/ImmutableCollections/DoNotCallToImmutableCollectionOnAnImmutableCollectionValueTests.cs @@ -54,14 +54,23 @@ static class Extensions }} }} +interface IValue +{{ +}} + +class Value : IValue +{{ +}} + class C {{ - public void M(IEnumerable p1, List p2, {collectionName} p3, IEqualityComparer comparer) + public void M(IEnumerable p1, List p2, {collectionName} p3, {collectionName} p4, IEqualityComparer comparer) {{ // Allowed p1.To{collectionName}(); p2.To{collectionName}(); p3.To{collectionName}(comparer); // Potentially modifies the collection + p4.To{collectionName}(); // Changes the generic type Extensions.To{collectionName}(p1); Extensions.To{collectionName}(p2);