From 7e4c15fd3789313885e3db86807c2246b83771b1 Mon Sep 17 00:00:00 2001 From: Martin Strecker Date: Tue, 3 May 2022 12:43:19 +0200 Subject: [PATCH] Remove Lookup impl and use MultiValueDict --- .../Rules/RedundantInheritanceList.cs | 12 ++-- .../Common/MultiValueDictionary.cs | 68 ++----------------- 2 files changed, 12 insertions(+), 68 deletions(-) diff --git a/analyzers/src/SonarAnalyzer.CSharp/Rules/RedundantInheritanceList.cs b/analyzers/src/SonarAnalyzer.CSharp/Rules/RedundantInheritanceList.cs index 77829ba5767..a31a30663b7 100644 --- a/analyzers/src/SonarAnalyzer.CSharp/Rules/RedundantInheritanceList.cs +++ b/analyzers/src/SonarAnalyzer.CSharp/Rules/RedundantInheritanceList.cs @@ -116,25 +116,25 @@ private static void ReportRedundantInterfaces(SyntaxNodeAnalysisContext context, } } - private static ILookup GetImplementedInterfaceMappings(BaseListSyntax baseList, SemanticModel semanticModel) => + private static MultiValueDictionary GetImplementedInterfaceMappings(BaseListSyntax baseList, SemanticModel semanticModel) => baseList.Types .Select(baseType => semanticModel.GetSymbolInfo(baseType.Type).Symbol as INamedTypeSymbol) .WhereNotNull() .Distinct() - .ToLookup(x => x.AllInterfaces.AsEnumerable()); + .ToMultiValueDictionary(x => x.AllInterfaces); private static INamedTypeSymbol CollidingDeclaration(INamedTypeSymbol declaredType, INamedTypeSymbol interfaceType, - ILookup interfaceMappings) + MultiValueDictionary interfaceMappings) { - var collisionMapping = interfaceMappings.FirstOrDefault(x => x.Key.IsInterface() && x.Contains(interfaceType)); - if (collisionMapping?.Key is not null) + var collisionMapping = interfaceMappings.FirstOrDefault(x => x.Key.IsInterface() && x.Value.Contains(interfaceType)); + if (collisionMapping.Key is not null) { return collisionMapping.Key; } var baseClassMapping = interfaceMappings.FirstOrDefault(x => x.Key.IsClass()); - if (baseClassMapping?.Key is null) + if (baseClassMapping.Key is null) { return null; } diff --git a/analyzers/src/SonarAnalyzer.Common/Common/MultiValueDictionary.cs b/analyzers/src/SonarAnalyzer.Common/Common/MultiValueDictionary.cs index 14a74a9689b..35e55d8ed78 100644 --- a/analyzers/src/SonarAnalyzer.Common/Common/MultiValueDictionary.cs +++ b/analyzers/src/SonarAnalyzer.Common/Common/MultiValueDictionary.cs @@ -86,10 +86,12 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont public static class MultiValueDictionaryExtensions { - public static MultiValueDictionary ToMultiValueDictionary( - this IEnumerable source, - Func keySelector, - Func> elementSelector) + public static MultiValueDictionary ToMultiValueDictionary(this IEnumerable source, Func> elementSelector) => + source.ToMultiValueDictionary(x => x, elementSelector); + + public static MultiValueDictionary ToMultiValueDictionary(this IEnumerable source, + Func keySelector, + Func> elementSelector) { var dictionary = new MultiValueDictionary(); foreach (var item in source) @@ -98,64 +100,6 @@ public static MultiValueDictionary ToMultiValueDictionary ToLookup(this IEnumerable source, Func> elementsSelector) => - source.ToLookup(x => x, elementsSelector); - - public static ILookup ToLookup(this IEnumerable source, - Func keySelector, - Func> elementsSelector) => - new Lookup(source.ToDictionary(keySelector, elementsSelector)); - - public static ILookup ToLookup(this IReadOnlyDictionary> source) => - new Lookup(source); - - private sealed class Lookup : ILookup - { - public Lookup(IReadOnlyDictionary> source) - { - Source = source; - } - private IReadOnlyDictionary> Source { get; } - - public IEnumerable this[TKey key] => Source[key]; - - public int Count => Source.Count; - - public bool Contains(TKey key) => Source.ContainsKey(key); - public IEnumerator> GetEnumerator() => new Enumerator(this); - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - - private readonly struct Enumerator : IEnumerator> - { - public Enumerator(Lookup lookup) - { - DictEnumerator = lookup.Source.GetEnumerator(); - } - private IEnumerator>> DictEnumerator { get; } - - public IGrouping Current => new Grouping(DictEnumerator.Current.Key, DictEnumerator.Current.Value); - - public void Dispose() => DictEnumerator.Dispose(); - public bool MoveNext() => DictEnumerator.MoveNext(); - public void Reset() => DictEnumerator.Reset(); - object IEnumerator.Current => Current; - - private readonly struct Grouping : IGrouping - { - public Grouping(TKey key, IEnumerable value) - { - Key = key; - Value = value; - } - - public TKey Key { get; } - private IEnumerable Value { get; } - public IEnumerator GetEnumerator() => Value.GetEnumerator(); - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - } - } - } } #endregion Extensions