From 5817c2f967525a46c85bd4aeab7ff000344a91b4 Mon Sep 17 00:00:00 2001 From: Manish Vasani Date: Wed, 6 Dec 2017 14:57:38 -0800 Subject: [PATCH] Revert the change to comment out SystemCollectionsImmutable reference as large number of tests depend on it --- ...ectionOnAnImmutableCollectionValueTests.cs | 453 +++++------------- ...crosoft.NetCore.Analyzers.UnitTests.csproj | 3 + .../DiagnosticAnalyzerTestBase.cs | 7 +- 3 files changed, 137 insertions(+), 326 deletions(-) diff --git a/src/Microsoft.NetCore.Analyzers/UnitTests/ImmutableCollections/DoNotCallToImmutableCollectionOnAnImmutableCollectionValueTests.cs b/src/Microsoft.NetCore.Analyzers/UnitTests/ImmutableCollections/DoNotCallToImmutableCollectionOnAnImmutableCollectionValueTests.cs index f33635f605..d931108965 100644 --- a/src/Microsoft.NetCore.Analyzers/UnitTests/ImmutableCollections/DoNotCallToImmutableCollectionOnAnImmutableCollectionValueTests.cs +++ b/src/Microsoft.NetCore.Analyzers/UnitTests/ImmutableCollections/DoNotCallToImmutableCollectionOnAnImmutableCollectionValueTests.cs @@ -23,303 +23,6 @@ public class DoNotCallToImmutableCollectionOnAnImmutableCollectionValueTests : D nameof(ImmutableSortedDictionary) }; - #region CSharpCollectionsDefinition - - private const string CSharpCollectionsDefinition = @" -using System.Collections.Generic; -using System.Collections.Immutable; -using static System.Collections.Immutable.ImmutableExtensions; - -namespace System.Collections.Immutable -{ - public sealed partial class ImmutableArray : IEnumerable - { - public IEnumerator GetEnumerator() - { - throw new NotImplementedException(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - throw new NotImplementedException(); - } - } - - public sealed partial class ImmutableList : IEnumerable - { - public IEnumerator GetEnumerator() - { - throw new NotImplementedException(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - throw new NotImplementedException(); - } - } - - public sealed partial class ImmutableHashSet : IEnumerable - { - public IEnumerator GetEnumerator() - { - throw new NotImplementedException(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - throw new NotImplementedException(); - } - } - - public sealed partial class ImmutableSortedSet : IEnumerable - { - public IEnumerator GetEnumerator() - { - throw new NotImplementedException(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - throw new NotImplementedException(); - } - } - - public sealed partial class ImmutableDictionary : IEnumerable> - { - public IEnumerator> GetEnumerator() - { - throw new NotImplementedException(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - throw new NotImplementedException(); - } - } - - public sealed partial class ImmutableSortedDictionary : IEnumerable> - { - public IEnumerator> GetEnumerator() - { - throw new NotImplementedException(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - throw new NotImplementedException(); - } - } - - public static class ImmutableExtensions - { - public static ImmutableArray ToImmutableArray(this IEnumerable source) - { - return null; - } - - public static ImmutableArray ToImmutableArray(this IEnumerable source, IEqualityComparer comparer) - { - return null; - } - - public static ImmutableList ToImmutableList(this IEnumerable source) - { - return null; - } - - public static ImmutableList ToImmutableList(this IEnumerable source, IEqualityComparer comparer) - { - return null; - } - - public static ImmutableHashSet ToImmutableHashSet(this IEnumerable source) - { - return null; - } - - public static ImmutableHashSet ToImmutableHashSet(this IEnumerable source, IEqualityComparer comparer) - { - return null; - } - - public static ImmutableSortedSet ToImmutableSortedSet(this IEnumerable source) - { - return null; - } - - public static ImmutableSortedSet ToImmutableSortedSet(this IEnumerable source, IEqualityComparer comparer) - { - return null; - } - - public static ImmutableDictionary ToImmutableDictionary(this IEnumerable> source) - { - return null; - } - - public static ImmutableDictionary ToImmutableDictionary(this IEnumerable> source, IEqualityComparer keyComparer) - { - return null; - } - - public static ImmutableSortedDictionary ToImmutableSortedDictionary(this IEnumerable> source) - { - return null; - } - - public static ImmutableSortedDictionary ToImmutableSortedDictionary(this IEnumerable> source, IEqualityComparer keyComparer) - { - return null; - } - } -} -"; - #endregion - - #region VisualBasicCollectionsDefinition - - private const string VisualBasicCollectionsDefinition = @" -Imports System -Imports System.Collections -Imports System.Collections.Generic -Imports System.Collections.Immutable -Imports System.Collections.Immutable.ImmutableExtensions -Imports System.Runtime.CompilerServices - -Namespace System.Collections.Immutable - - Partial Public NotInheritable Class ImmutableArray(Of T) - Implements IEnumerable(Of T) - - Public Function GetEnumerator() As IEnumerator(Of T) Implements IEnumerable(Of T).GetEnumerator - Throw New NotImplementedException() - End Function - - Private Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator - Throw New NotImplementedException() - End Function - End Class - - Partial Public NotInheritable Class ImmutableList(Of T) - Implements IEnumerable(Of T) - Public Function GetEnumerator() As IEnumerator(Of T) Implements IEnumerable(Of T).GetEnumerator - Throw New NotImplementedException() - End Function - - Private Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator - Throw New NotImplementedException() - End Function - End Class - - Partial Public NotInheritable Class ImmutableHashSet(Of T) - Implements IEnumerable(Of T) - Public Function GetEnumerator() As IEnumerator(Of T) Implements IEnumerable(Of T).GetEnumerator - Throw New NotImplementedException() - End Function - - Private Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator - Throw New NotImplementedException() - End Function - End Class - - Partial Public NotInheritable Class ImmutableSortedSet(Of T) - Implements IEnumerable(Of T) - Public Function GetEnumerator() As IEnumerator(Of T) Implements IEnumerable(Of T).GetEnumerator - Throw New NotImplementedException() - End Function - - Private Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator - Throw New NotImplementedException() - End Function - End Class - - Partial Public NotInheritable Class ImmutableDictionary(Of TKey, TValue) - Implements IEnumerable(Of KeyValuePair(Of TKey, TValue)) - Public Function GetEnumerator() As IEnumerator(Of KeyValuePair(Of TKey, TValue)) Implements IEnumerable(Of KeyValuePair(Of TKey, TValue)).GetEnumerator - Throw New NotImplementedException() - End Function - - Private Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator - Throw New NotImplementedException() - End Function - End Class - - Partial Public NotInheritable Class ImmutableSortedDictionary(Of TKey, TValue) - Implements IEnumerable(Of KeyValuePair(Of TKey, TValue)) - Public Function GetEnumerator() As IEnumerator(Of KeyValuePair(Of TKey, TValue)) Implements IEnumerable(Of KeyValuePair(Of TKey, TValue)).GetEnumerator - Throw New NotImplementedException() - End Function - - Private Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator - Throw New NotImplementedException() - End Function - End Class - - Module ImmutableExtensions - - Function ToImmutableArray(Of T)(ByVal source As IEnumerable(Of T)) As ImmutableArray(Of T) - Return Nothing - End Function - - - Function ToImmutableArray(Of T)(ByVal source As IEnumerable(Of T), ByVal comparer As IEqualityComparer(Of T)) As ImmutableArray(Of T) - Return Nothing - End Function - - - Function ToImmutableList(Of T)(ByVal source As IEnumerable(Of T)) As ImmutableList(Of T) - Return Nothing - End Function - - - Function ToImmutableList(Of T)(ByVal source As IEnumerable(Of T), ByVal comparer As IEqualityComparer(Of T)) As ImmutableList(Of T) - Return Nothing - End Function - - - Function ToImmutableHashSet(Of T)(ByVal source As IEnumerable(Of T)) As ImmutableHashSet(Of T) - Return Nothing - End Function - - - Function ToImmutableHashSet(Of T)(ByVal source As IEnumerable(Of T), ByVal comparer As IEqualityComparer(Of T)) As ImmutableHashSet(Of T) - Return Nothing - End Function - - - Function ToImmutableSortedSet(Of T)(ByVal source As IEnumerable(Of T)) As ImmutableSortedSet(Of T) - Return Nothing - End Function - - - Function ToImmutableSortedSet(Of T)(ByVal source As IEnumerable(Of T), ByVal comparer As IEqualityComparer(Of T)) As ImmutableSortedSet(Of T) - Return Nothing - End Function - - - Function ToImmutableDictionary(Of TKey, TValue)(ByVal source As IEnumerable(Of KeyValuePair(Of TKey, TValue))) As ImmutableDictionary(Of TKey, TValue) - Return Nothing - End Function - - - Function ToImmutableDictionary(Of TKey, TValue)(ByVal source As IEnumerable(Of KeyValuePair(Of TKey, TValue)), ByVal keyComparer As IEqualityComparer(Of TKey)) As ImmutableDictionary(Of TKey, TValue) - Return Nothing - End Function - - - Function ToImmutableSortedDictionary(Of TKey, TValue)(ByVal source As IEnumerable(Of KeyValuePair(Of TKey, TValue))) As ImmutableSortedDictionary(Of TKey, TValue) - Return Nothing - End Function - - - Function ToImmutableSortedDictionary(Of TKey, TValue)(ByVal source As IEnumerable(Of KeyValuePair(Of TKey, TValue)), ByVal keyComparer As IEqualityComparer(Of TKey)) As ImmutableSortedDictionary(Of TKey, TValue) - Return Nothing - End Function - End Module -End Namespace -"; - #endregion - protected override DiagnosticAnalyzer GetBasicDiagnosticAnalyzer() { return new DoNotCallToImmutableCollectionOnAnImmutableCollectionValueAnalyzer(); @@ -336,7 +39,24 @@ protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() [MemberData(nameof(CollectionNames_Arity1))] public void NoDiagnosticCases_Arity1(string collectionName) { - VerifyCSharp(CSharpCollectionsDefinition + $@" + VerifyCSharp($@" +using System.Collections.Generic; +using System.Collections.Immutable; +using static System.Collections.Immutable.{collectionName}; + +static class Extensions +{{ + public static {collectionName} To{collectionName}(this IEnumerable items) + {{ + return default({collectionName}); + }} + + public static {collectionName} To{collectionName}(this IEnumerable items, IEqualityComparer comparer) + {{ + return default({collectionName}); + }} +}} + class C {{ public void M(IEnumerable p1, List p2, {collectionName} p3, IEqualityComparer comparer) @@ -353,7 +73,22 @@ public void M(IEnumerable p1, List p2, {collectionName} p3, IEqua }} "); - VerifyBasic(VisualBasicCollectionsDefinition + $@" + VerifyBasic($@" +Imports System.Collections.Generic +Imports System.Collections.Immutable + +Module Extensions + _ + Public Function To{collectionName}(Of TSource)(items As IEnumerable(Of TSource)) As {collectionName}(Of TSource) + Return Nothing + End Function + + _ + Public Function To{collectionName}(Of TSource)(items As IEnumerable(Of TSource), comparer as IEqualityComparer(Of TSource)) As {collectionName}(Of TSource) + Return Nothing + End Function +End Module + Class C Public Sub M(p1 As IEnumerable(Of Integer), p2 As List(Of Integer), p3 As {collectionName}(Of Integer), comparer As IEqualityComparer(Of Integer)) ' Allowed @@ -373,7 +108,24 @@ End Class [MemberData(nameof(CollectionNames_Arity2))] public void NoDiagnosticCases_Arity2(string collectionName) { - VerifyCSharp(CSharpCollectionsDefinition + $@" + VerifyCSharp($@" +using System.Collections.Generic; +using System.Collections.Immutable; +using static System.Collections.Immutable.{collectionName}; + +static class Extensions +{{ + public static {collectionName} To{collectionName}(this IEnumerable> items) + {{ + return default({collectionName}); + }} + + public static {collectionName} To{collectionName}(this IEnumerable> items, IEqualityComparer keyComparer) + {{ + return default({collectionName}); + }} +}} + class C {{ public void M(IEnumerable> p1, List> p2, {collectionName} p3, IEqualityComparer keyComparer) @@ -390,7 +142,22 @@ public void M(IEnumerable> p1, List _ + Public Function To{collectionName}(Of TKey, TValue)(items As IEnumerable(Of KeyValuePair(Of TKey, TValue))) As {collectionName}(Of TKey, TValue) + Return Nothing + End Function + + _ + Public Function To{collectionName}(Of TKey, TValue)(items As IEnumerable(Of KeyValuePair(Of TKey, TValue)), keyComparer As IEqualityComparer(Of TKey)) As {collectionName}(Of TKey, TValue) + Return Nothing + End Function +End Module + Class C Public Sub M(p1 As IEnumerable(Of KeyValuePair(Of Integer, Integer)), p2 As List(Of KeyValuePair(Of Integer, Integer)), p3 As {collectionName}(Of Integer, Integer), keyComparer As IEqualityComparer(Of Integer)) ' Allowed @@ -414,7 +181,19 @@ End Class [MemberData(nameof(CollectionNames_Arity1))] public void DiagnosticCases_Arity1(string collectionName) { - VerifyCSharp(CSharpCollectionsDefinition + $@" + VerifyCSharp($@" +using System.Collections.Generic; +using System.Collections.Immutable; +using static System.Collections.Immutable.{collectionName}; + +static class Extensions +{{ + public static {collectionName} To{collectionName}(this IEnumerable items) + {{ + return default({collectionName}); + }} +}} + class C {{ public void M(IEnumerable p1, List p2, {collectionName} p3) @@ -424,12 +203,22 @@ public void M(IEnumerable p1, List p2, {collectionName} p3) }} }} ", - // Test0.cs(154,9): warning RS0012: Do not call ToImmutableCollection on an ImmutableCollection value - GetCSharpResultAt(154, 9, collectionName), - // Test0.cs(155,9): warning RS0012: Do not call ToImmutableCollection on an ImmutableCollection value - GetCSharpResultAt(155, 9, collectionName)); + // Test0.cs(18,9): warning RS0012: Do not call ToImmutableCollection on an ImmutableCollection value + GetCSharpResultAt(18, 9, collectionName), + // Test0.cs(19,9): warning RS0012: Do not call ToImmutableCollection on an ImmutableCollection value + GetCSharpResultAt(19, 9, collectionName)); + + VerifyBasic($@" +Imports System.Collections.Generic +Imports System.Collections.Immutable + +Module Extensions + _ + Public Function To{collectionName}(Of TSource)(items As IEnumerable(Of TSource)) As {collectionName}(Of TSource) + Return Nothing + End Function +End Module - VerifyBasic(VisualBasicCollectionsDefinition + $@" Class C Public Sub M(p1 As IEnumerable(Of Integer), p2 As List(Of Integer), p3 As {collectionName}(Of Integer)) p1.To{collectionName}().To{collectionName}() @@ -437,17 +226,29 @@ Public Sub M(p1 As IEnumerable(Of Integer), p2 As List(Of Integer), p3 As {colle End Sub End Class ", - // Test0.vb(143,3): warning RS0012: Do not call ToImmutableCollection on an ImmutableCollection value - GetBasicResultAt(143, 3, collectionName), - // Test0.vb(144,3): warning RS0012: Do not call ToImmutableCollection on an ImmutableCollection value - GetBasicResultAt(144, 3, collectionName)); + // Test0.vb(14,3): warning RS0012: Do not call ToImmutableCollection on an ImmutableCollection value + GetBasicResultAt(14, 3, collectionName), + // Test0.vb(15,3): warning RS0012: Do not call ToImmutableCollection on an ImmutableCollection value + GetBasicResultAt(15, 3, collectionName)); } [Theory] [MemberData(nameof(CollectionNames_Arity2))] public void DiagnosticCases_Arity2(string collectionName) { - VerifyCSharp(CSharpCollectionsDefinition + $@" + VerifyCSharp($@" +using System.Collections.Generic; +using System.Collections.Immutable; +using static System.Collections.Immutable.{collectionName}; + +static class Extensions +{{ + public static {collectionName} To{collectionName}(this IEnumerable> items) + {{ + return default({collectionName}); + }} +}} + class C {{ public void M(IEnumerable> p1, List> p2, {collectionName} p3) @@ -457,12 +258,22 @@ public void M(IEnumerable> p1, List _ + Public Function To{collectionName}(Of TKey, TValue)(items As IEnumerable(Of KeyValuePair(Of TKey, TValue))) As {collectionName}(Of TKey, TValue) + Return Nothing + End Function +End Module - VerifyBasic(VisualBasicCollectionsDefinition + $@" Class C Public Sub M(p1 As IEnumerable(Of KeyValuePair(Of Integer, Integer)), p2 As List(Of KeyValuePair(Of Integer, Integer)), p3 As {collectionName}(Of Integer, Integer)) p1.To{collectionName}().To{collectionName}() @@ -470,10 +281,10 @@ Public Sub M(p1 As IEnumerable(Of KeyValuePair(Of Integer, Integer)), p2 As List End Sub End Class ", - // Test0.vb(143,3): warning RS0012: Do not call ToImmutableCollection on an ImmutableCollection value - GetBasicResultAt(143, 3, collectionName), - // Test0.vb(144,3): warning RS0012: Do not call ToImmutableCollection on an ImmutableCollection value - GetBasicResultAt(144, 3, collectionName)); + // Test0.vb(14,3): warning RS0012: Do not call ToImmutableCollection on an ImmutableCollection value + GetBasicResultAt(14, 3, collectionName), + // Test0.vb(15,3): warning RS0012: Do not call ToImmutableCollection on an ImmutableCollection value + GetBasicResultAt(15, 3, collectionName)); } #endregion diff --git a/src/Microsoft.NetCore.Analyzers/UnitTests/Microsoft.NetCore.Analyzers.UnitTests.csproj b/src/Microsoft.NetCore.Analyzers/UnitTests/Microsoft.NetCore.Analyzers.UnitTests.csproj index c2bfc9ee24..b04cfeb279 100644 --- a/src/Microsoft.NetCore.Analyzers/UnitTests/Microsoft.NetCore.Analyzers.UnitTests.csproj +++ b/src/Microsoft.NetCore.Analyzers/UnitTests/Microsoft.NetCore.Analyzers.UnitTests.csproj @@ -9,4 +9,7 @@ + + + \ No newline at end of file diff --git a/src/Test.Utilities/DiagnosticAnalyzerTestBase.cs b/src/Test.Utilities/DiagnosticAnalyzerTestBase.cs index 22917a48be..e8d01f62eb 100644 --- a/src/Test.Utilities/DiagnosticAnalyzerTestBase.cs +++ b/src/Test.Utilities/DiagnosticAnalyzerTestBase.cs @@ -29,15 +29,12 @@ public abstract class DiagnosticAnalyzerTestBase private static readonly MetadataReference s_codeAnalysisReference = MetadataReference.CreateFromFile(typeof(Compilation).Assembly.Location); private static readonly MetadataReference s_workspacesReference = MetadataReference.CreateFromFile(typeof(Workspace).Assembly.Location); private static readonly MetadataReference s_systemDiagnosticsDebugReference = MetadataReference.CreateFromFile(typeof(Debug).Assembly.Location); + private static readonly MetadataReference s_immutableCollectionsReference = MetadataReference.CreateFromFile(typeof(ImmutableArray).Assembly.Location); private static readonly MetadataReference s_systemDataReference = MetadataReference.CreateFromFile(typeof(System.Data.DataSet).Assembly.Location); protected static readonly CompilationOptions s_CSharpDefaultOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary); protected static readonly CompilationOptions s_CSharpUnsafeCodeDefaultOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary).WithAllowUnsafe(true); protected static readonly CompilationOptions s_visualBasicDefaultOptions = new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary); -#pragma warning disable CA1823 // Avoid unused private fields https://github.com/dotnet/roslyn-analyzers/issues/1318 - private static readonly MetadataReference s_immutableCollectionsReference = MetadataReference.CreateFromFile(typeof(ImmutableArray).Assembly.Location); -#pragma warning restore CA1823 // Avoid unused private fields - internal const string DefaultFilePathPrefix = "Test"; internal const string CSharpDefaultFileExt = "cs"; internal const string VisualBasicDefaultExt = "vb"; @@ -375,7 +372,7 @@ private static Project CreateProject( .AddMetadataReference(projectId, SystemRuntimeFacadeRef) .AddMetadataReference(projectId, SystemThreadingFacadeRef) .AddMetadataReference(projectId, SystemThreadingTaskFacadeRef) - //.AddMetadataReference(projectId, s_immutableCollectionsReference) https://github.com/dotnet/roslyn-analyzers/issues/1318 + .AddMetadataReference(projectId, s_immutableCollectionsReference) .AddMetadataReference(projectId, s_workspacesReference) .AddMetadataReference(projectId, s_systemDiagnosticsDebugReference) .AddMetadataReference(projectId, s_systemDataReference)