Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AK2001 - Add as cast expression detection #114

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,47 @@ IMessageExtractor Create(){
""", new[]
{
(10, 26, 10, 48)
})
}),

(
// Simple message extractor edge case - using expression body instead of block
"""
// 01
using Akka.Cluster.Sharding;
public sealed class ShardMessageExtractor : HashCodeMessageExtractor
{
/// <summary>
/// We only ever run with a maximum of two nodes, so ~10 shards per node
/// </summary>
public ShardMessageExtractor(int shardCount = 20) : base(shardCount)
{
}

public override string EntityId(object message)
=> (message as ShardingEnvelope)?.EntityId ?? null;
}
""", new[]{(13, 13, 13, 40)}),

(
// Simple message extractor edge case - using `as`
"""
// 02
using Akka.Cluster.Sharding;
public sealed class ShardMessageExtractor : HashCodeMessageExtractor
{
/// <summary>
/// We only ever run with a maximum of two nodes, so ~10 shards per node
/// </summary>
public ShardMessageExtractor(int shardCount = 20) : base(shardCount)
{
}

public override string EntityId(object message)
{
return (message as ShardingEnvelope)?.EntityId ?? null;
}
}
""", new[]{(14, 17, 14, 44)}),
};

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,24 @@ private static void AnalyzeDeclaredVariableNodes(SyntaxNodeAnalysisContext ctx,

break;
}

case BinaryExpressionSyntax binaryExpressionSyntax when binaryExpressionSyntax.IsKind(SyntaxKind.AsExpression) && binaryExpressionSyntax.Right is TypeSyntax typeSyntax:
var typeSymbol = semanticModel.GetTypeInfo(typeSyntax).Type;
if (forbiddenTypes.Any(t => SymbolEqualityComparer.Default.Equals(t, typeSymbol)))
{
var location = binaryExpressionSyntax.GetLocation();

// duplicate
if (reportedLocations.Contains(location))
break;
var diagnostic = Diagnostic.Create(
RuleDescriptors
.Ak2001DoNotUseAutomaticallyHandledMessagesInShardMessageExtractor,
location);
ctx.ReportDiagnostic(diagnostic);
reportedLocations.Add(location);
}
break;
}
}
}