diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/LightupHelpers.cs b/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/LightupHelpers.cs index b247b263e..43e460faf 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/LightupHelpers.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/LightupHelpers.cs @@ -65,15 +65,13 @@ internal static bool CanWrapObject(object obj, Type underlyingType) return false; } - ConcurrentDictionary wrappedObject = SupportedObjectWrappers.GetOrAdd(underlyingType, _ => new ConcurrentDictionary()); + ConcurrentDictionary wrappedObject = SupportedObjectWrappers.GetOrAdd(underlyingType, static _ => new ConcurrentDictionary()); - // Avoid creating the delegate if the value already exists - bool canCast; - if (!wrappedObject.TryGetValue(obj.GetType(), out canCast)) + // Avoid creating a delegate and capture class + if (!wrappedObject.TryGetValue(obj.GetType(), out var canCast)) { - canCast = wrappedObject.GetOrAdd( - obj.GetType(), - kind => underlyingType.GetTypeInfo().IsAssignableFrom(obj.GetType().GetTypeInfo())); + canCast = underlyingType.GetTypeInfo().IsAssignableFrom(obj.GetType().GetTypeInfo()); + wrappedObject.TryAdd(obj.GetType(), canCast); } return canCast; @@ -93,15 +91,13 @@ internal static bool CanWrapNode(SyntaxNode node, Type underlyingType) return false; } - ConcurrentDictionary wrappedSyntax = SupportedSyntaxWrappers.GetOrAdd(underlyingType, _ => new ConcurrentDictionary()); + ConcurrentDictionary wrappedSyntax = SupportedSyntaxWrappers.GetOrAdd(underlyingType, static _ => new ConcurrentDictionary()); - // Avoid creating the delegate if the value already exists - bool canCast; - if (!wrappedSyntax.TryGetValue(node.Kind(), out canCast)) + // Avoid creating a delegate and capture class + if (!wrappedSyntax.TryGetValue(node.Kind(), out var canCast)) { - canCast = wrappedSyntax.GetOrAdd( - node.Kind(), - kind => underlyingType.GetTypeInfo().IsAssignableFrom(node.GetType().GetTypeInfo())); + canCast = underlyingType.GetTypeInfo().IsAssignableFrom(node.GetType().GetTypeInfo()); + wrappedSyntax.TryAdd(node.Kind(), canCast); } return canCast; @@ -121,15 +117,13 @@ internal static bool CanWrapOperation(IOperation operation, Type underlyingType) return false; } - ConcurrentDictionary wrappedSyntax = SupportedOperationWrappers.GetOrAdd(underlyingType, _ => new ConcurrentDictionary()); + ConcurrentDictionary wrappedSyntax = SupportedOperationWrappers.GetOrAdd(underlyingType, static _ => new ConcurrentDictionary()); - // Avoid creating the delegate if the value already exists - bool canCast; - if (!wrappedSyntax.TryGetValue(operation.Kind, out canCast)) + // Avoid creating a delegate and capture class + if (!wrappedSyntax.TryGetValue(operation.Kind, out var canCast)) { - canCast = wrappedSyntax.GetOrAdd( - operation.Kind, - kind => underlyingType.GetTypeInfo().IsAssignableFrom(operation.GetType().GetTypeInfo())); + canCast = underlyingType.GetTypeInfo().IsAssignableFrom(operation.GetType().GetTypeInfo()); + wrappedSyntax.TryAdd(operation.Kind, canCast); } return canCast;