Skip to content

Commit b5fd4b5

Browse files
Remove local function
1 parent 16c310e commit b5fd4b5

File tree

1 file changed

+23
-29
lines changed

1 file changed

+23
-29
lines changed

StyleCop.Analyzers/StyleCop.Analyzers/Lightup/LightupHelpers.cs

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,14 @@ internal static bool CanWrapObject(object obj, Type underlyingType)
7171
wrappedObject = SupportedObjectWrappers.GetOrAdd(underlyingType, static _ => new ConcurrentDictionary<Type, bool>());
7272
}
7373

74-
// Avoid creating the delegate and capture class if the value already exists
75-
return wrappedObject.TryGetValue(obj.GetType(), out var canCast)
76-
? canCast
77-
: GetOrAdd(obj, underlyingType, wrappedObject);
78-
79-
// Don't inline this method. Otherwise a capture class is generated on each call to CanWrapObject.
80-
static bool GetOrAdd(object obj, Type underlyingType, ConcurrentDictionary<Type, bool> wrappedObject)
81-
=> wrappedObject.GetOrAdd(
82-
obj.GetType(),
83-
kind => underlyingType.GetTypeInfo().IsAssignableFrom(obj.GetType().GetTypeInfo()));
74+
// Avoid creating the delegate and capture class
75+
if (!wrappedObject.TryGetValue(obj.GetType(), out var canCast))
76+
{
77+
canCast = underlyingType.GetTypeInfo().IsAssignableFrom(obj.GetType().GetTypeInfo());
78+
wrappedObject.TryAdd(obj.GetType(), canCast);
79+
}
80+
81+
return canCast;
8482
}
8583

8684
internal static bool CanWrapNode(SyntaxNode node, Type underlyingType)
@@ -103,16 +101,14 @@ internal static bool CanWrapNode(SyntaxNode node, Type underlyingType)
103101
wrappedSyntax = SupportedSyntaxWrappers.GetOrAdd(underlyingType, static _ => new ConcurrentDictionary<SyntaxKind, bool>());
104102
}
105103

106-
// Avoid creating the delegate and capture class if the value already exists
107-
return wrappedSyntax.TryGetValue(node.Kind(), out var canCast)
108-
? canCast
109-
: GetOrAdd(node, underlyingType, wrappedSyntax);
104+
// Avoid creating the delegate and capture class
105+
if (!wrappedSyntax.TryGetValue(node.Kind(), out var canCast))
106+
{
107+
canCast = underlyingType.GetTypeInfo().IsAssignableFrom(node.GetType().GetTypeInfo());
108+
wrappedSyntax.TryAdd(node.Kind(), canCast);
109+
}
110110

111-
// Don't inline this method. Otherwise a capture class is generated on each call to CanWrapNode.
112-
static bool GetOrAdd(SyntaxNode node, Type underlyingType, ConcurrentDictionary<SyntaxKind, bool> wrappedSyntax) =>
113-
wrappedSyntax.GetOrAdd(
114-
node.Kind(),
115-
kind => underlyingType.GetTypeInfo().IsAssignableFrom(node.GetType().GetTypeInfo()));
111+
return canCast;
116112
}
117113

118114
internal static bool CanWrapOperation(IOperation operation, Type underlyingType)
@@ -135,16 +131,14 @@ internal static bool CanWrapOperation(IOperation operation, Type underlyingType)
135131
wrappedSyntax = SupportedOperationWrappers.GetOrAdd(underlyingType, static _ => new ConcurrentDictionary<OperationKind, bool>());
136132
}
137133

138-
// Avoid creating the delegate if the value already exists
139-
return wrappedSyntax.TryGetValue(operation.Kind, out var canCast)
140-
? canCast
141-
: GetOrAdd(operation, underlyingType, wrappedSyntax);
142-
143-
// Don't inline this method. Otherwise a capture class is generated on each call to CanWrapOperation.
144-
static bool GetOrAdd(IOperation operation, Type underlyingType, ConcurrentDictionary<OperationKind, bool> wrappedSyntax) =>
145-
wrappedSyntax.GetOrAdd(
146-
operation.Kind,
147-
kind => underlyingType.GetTypeInfo().IsAssignableFrom(operation.GetType().GetTypeInfo()));
134+
// Avoid creating the delegate and capture class
135+
if (!wrappedSyntax.TryGetValue(operation.Kind, out var canCast))
136+
{
137+
canCast = underlyingType.GetTypeInfo().IsAssignableFrom(operation.GetType().GetTypeInfo());
138+
wrappedSyntax.TryAdd(operation.Kind, canCast);
139+
}
140+
141+
return canCast;
148142
}
149143

150144
internal static Func<TOperation, TProperty> CreateOperationPropertyAccessor<TOperation, TProperty>(Type type, string propertyName)

0 commit comments

Comments
 (0)