Skip to content

Commit

Permalink
Sync with #DotNetAnalyzers/StyleCopAnalyzers/3711 from StyleCop
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-strecker-sonarsource committed Oct 18, 2023
1 parent 423e1fe commit c59a99b
Showing 1 changed file with 21 additions and 36 deletions.
57 changes: 21 additions & 36 deletions analyzers/src/SonarAnalyzer.CFG/ShimLayer/LightupHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,16 @@ internal static bool CanWrapObject(object obj, Type underlyingType)
return false;
}

// Avoid creating the delegate if the value already exists
if (!SupportedObjectWrappers.TryGetValue(underlyingType, out var wrappedObject))
ConcurrentDictionary<Type, bool> wrappedObject = SupportedObjectWrappers.GetOrAdd(underlyingType, static _ => new ConcurrentDictionary<Type, bool>());

// Avoid creating a delegate and capture class
if (!wrappedObject.TryGetValue(obj.GetType(), out var canCast))
{
wrappedObject = SupportedObjectWrappers.GetOrAdd(underlyingType, static _ => new ConcurrentDictionary<Type, bool>());
canCast = underlyingType.GetTypeInfo().IsAssignableFrom(obj.GetType().GetTypeInfo());
wrappedObject.TryAdd(obj.GetType(), canCast);
}

// Avoid creating the delegate if the value already exists
return wrappedObject.TryGetValue(obj.GetType(), out var canCast)
? canCast
: GetOrAdd(obj, underlyingType, wrappedObject);

// Dont' inline this method. The capture class will span the whole method otherwise.
#pragma warning disable HAA0301, HAA0302 // Display class allocation to capture closure
static bool GetOrAdd(object obj, Type underlyingType, ConcurrentDictionary<Type, bool> wrappedObject) =>
wrappedObject.GetOrAdd(obj.GetType(), kind => underlyingType.GetTypeInfo().IsAssignableFrom(obj.GetType().GetTypeInfo()));
#pragma warning restore HAA0301, HAA0302
return canCast;
}

[PerformanceSensitive("https://github.com/SonarSource/sonar-dotnet/issues/8106", AllowCaptures = false, AllowGenericEnumeration = false, AllowImplicitBoxing = false)]
Expand All @@ -88,19 +82,16 @@ internal static bool CanWrapNode(SyntaxNode node, Type underlyingType)
return false;
}

// Avoid creating the delegate if the value already exists
if (!SupportedSyntaxWrappers.TryGetValue(underlyingType, out var wrappedSyntax))
ConcurrentDictionary<SyntaxKind, bool> wrappedSyntax = SupportedSyntaxWrappers.GetOrAdd(underlyingType, static _ => new ConcurrentDictionary<SyntaxKind, bool>());

// Avoid creating a delegate and capture class
if (!wrappedSyntax.TryGetValue(node.Kind(), out var canCast))
{
wrappedSyntax = SupportedSyntaxWrappers.GetOrAdd(underlyingType, static _ => new ConcurrentDictionary<SyntaxKind, bool>());
canCast = underlyingType.GetTypeInfo().IsAssignableFrom(node.GetType().GetTypeInfo());
wrappedSyntax.TryAdd(node.Kind(), canCast);
}

return wrappedSyntax.TryGetValue(node.Kind(), out var canCast) ? canCast : GetOrAdd(node, underlyingType, wrappedSyntax);

// Dont' inline this method. The capture class will span the whole method otherwise.
#pragma warning disable HAA0301, HAA0302 // Display class allocation to capture closure
static bool GetOrAdd(SyntaxNode node, Type underlyingType, ConcurrentDictionary<SyntaxKind, bool> wrappedSyntax) =>
wrappedSyntax.GetOrAdd(node.Kind(), kind => underlyingType.GetTypeInfo().IsAssignableFrom(node.GetType().GetTypeInfo()));
#pragma warning restore HAA0301, HAA0302
return canCast;
}

[PerformanceSensitive("https://github.com/SonarSource/sonar-dotnet/issues/8106", AllowCaptures = false, AllowGenericEnumeration = false, AllowImplicitBoxing = false)]
Expand All @@ -118,22 +109,16 @@ internal static bool CanWrapOperation(IOperation operation, Type underlyingType)
return false;
}

// Avoid creating the delegate if the value already exists
if (!SupportedOperationWrappers.TryGetValue(underlyingType, out var wrappedOperation))
ConcurrentDictionary<OperationKind, bool> wrappedSyntax = SupportedOperationWrappers.GetOrAdd(underlyingType, static _ => new ConcurrentDictionary<OperationKind, bool>());

// Avoid creating a delegate and capture class
if (!wrappedSyntax.TryGetValue(operation.Kind, out var canCast))
{
wrappedOperation = SupportedOperationWrappers.GetOrAdd(underlyingType, static _ => new ConcurrentDictionary<OperationKind, bool>());
canCast = underlyingType.GetTypeInfo().IsAssignableFrom(operation.GetType().GetTypeInfo());
wrappedSyntax.TryAdd(operation.Kind, canCast);
}

// Avoid creating the delegate if the value already exists
return wrappedOperation.TryGetValue(operation.Kind, out var canCast)
? canCast
: GetOrAdd(operation, underlyingType, wrappedOperation);

// Dont' inline this method. The capture class will span the whole method otherwise.
#pragma warning disable HAA0301, HAA0302 // Display class allocation to capture closure
static bool GetOrAdd(IOperation operation, Type underlyingType, ConcurrentDictionary<OperationKind, bool> wrappedOperation) =>
wrappedOperation.GetOrAdd(operation.Kind, kind => underlyingType.GetTypeInfo().IsAssignableFrom(operation.GetType().GetTypeInfo()));
#pragma warning restore HAA0301, HAA0302
return canCast;
}

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

0 comments on commit c59a99b

Please sign in to comment.