Skip to content

Commit 4f27bac

Browse files
remove redundant casts via AsInstanceOf (#6744)
Co-authored-by: Aaron Stannard <[email protected]>
1 parent d783933 commit 4f27bac

File tree

11 files changed

+49
-53
lines changed

11 files changed

+49
-53
lines changed

src/core/Akka.Persistence/Fsm/PersistentFSMBase.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -633,9 +633,9 @@ protected virtual void LogTermination(FSMBase.Reason reason)
633633
{
634634
if (reason is FSMBase.Failure failure)
635635
{
636-
if (failure.Cause is Exception)
636+
if (failure.Cause is Exception exception)
637637
{
638-
_log.Error(failure.Cause.AsInstanceOf<Exception>(), "terminating due to Failure");
638+
_log.Error(exception, "terminating due to Failure");
639639
}
640640
else
641641
{

src/core/Akka.Remote/Endpoint.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1438,8 +1438,8 @@ private void EnqueueInBuffer(object message)
14381438
var send = message as EndpointManager.Send;
14391439
if (send != null && send.Message is IPriorityMessage)
14401440
_prioBuffer.AddLast(send);
1441-
else if (send != null && send.Message is ActorSelectionMessage &&
1442-
send.Message.AsInstanceOf<ActorSelectionMessage>().Message is IPriorityMessage)
1441+
else if (send != null && send.Message is ActorSelectionMessage actorSelectionMessage &&
1442+
actorSelectionMessage.Message is IPriorityMessage)
14431443
{
14441444
_prioBuffer.AddLast(send);
14451445
}

src/core/Akka.Remote/EndpointManager.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ protected void Accepting()
755755
//the quarantine uid has lost the race with some failure, do nothing
756756
}
757757
}
758-
else if (policy.Item1 is Quarantined && policy.Item2 != null && policy.Item1.AsInstanceOf<Quarantined>().Uid == policy.Item2.Value)
758+
else if (policy.Item1 is Quarantined quarantined && policy.Item2 != null && quarantined.Uid == policy.Item2.Value)
759759
{
760760
// the UID to be quarantined already exists, do nothing
761761
}

src/core/Akka.Remote/RemoteActorRefProvider.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,9 @@ public IInternalActorRef ActorOf(ActorSystemImpl system, Props props, IInternalA
378378
.Aggregate(Deploy.None, (deploy1, deploy2) => deploy2.WithFallback(deploy1));
379379

380380
//match for remote scope
381-
if (propsDeploy.Scope is RemoteScope)
381+
if (propsDeploy.Scope is RemoteScope remoteScope)
382382
{
383-
var addr = propsDeploy.Scope.AsInstanceOf<RemoteScope>().Address;
383+
var addr = remoteScope.Address;
384384

385385
//Even if this actor is in RemoteScope, it might still be a local address
386386
if (HasAddress(addr))

src/core/Akka.Remote/RemoteDeployer.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ private static Deploy CheckRemoteRouterConfig(Deploy deploy)
6464
var nodes = deploy.Config.GetStringList("target.nodes", new string[] { }).Select(Address.Parse).ToList();
6565
if (nodes.Any() && deploy.RouterConfig != null)
6666
{
67-
if (deploy.RouterConfig is Pool)
67+
if (deploy.RouterConfig is Pool pool)
6868
return
69-
deploy.WithRouterConfig(new RemoteRouterConfig(deploy.RouterConfig.AsInstanceOf<Pool>(), nodes));
69+
deploy.WithRouterConfig(new RemoteRouterConfig(pool, nodes));
7070
return deploy.WithScope(scope: Deploy.NoScopeGiven);
7171
}
7272
else

src/core/Akka.Remote/Transport/AkkaProtocolTransport.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1120,11 +1120,10 @@ AssociationHandle GetHandle(ProtocolStateData data)
11201120
// Invalidate exposed but still unfinished promise. The underlying association disappeared, so after
11211121
// registration immediately signal a disassociate
11221122
Disassociated disassociateNotification;
1123-
if (@event.Reason is Failure && @event.Reason.AsInstanceOf<Failure>().Cause is DisassociateInfo)
1123+
if (@event.Reason is Failure { Cause: DisassociateInfo disassociateInfo })
11241124
{
11251125
disassociateNotification =
1126-
new Disassociated(@event.Reason.AsInstanceOf<Failure>().Cause
1127-
.AsInstanceOf<DisassociateInfo>());
1126+
new Disassociated(disassociateInfo);
11281127
}
11291128
else
11301129
{
@@ -1140,10 +1139,10 @@ AssociationHandle GetHandle(ProtocolStateData data)
11401139
case ListenerReady lr:
11411140
{
11421141
Disassociated disassociateNotification;
1143-
if (@event.Reason is Failure failure && failure.Cause is DisassociateInfo)
1142+
if (@event.Reason is Failure { Cause: DisassociateInfo disassociateInfo })
11441143
{
11451144
disassociateNotification =
1146-
new Disassociated(failure.Cause.AsInstanceOf<DisassociateInfo>());
1145+
new Disassociated(disassociateInfo);
11471146
}
11481147
else
11491148
{

src/core/Akka.Remote/Transport/FailureInjectorTransportAdapter.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ protected override void InterceptAssociate(Address remoteAddress, TaskCompletion
304304
/// <param name="ev">TBD</param>
305305
public void Notify(IAssociationEvent ev)
306306
{
307-
if (ev is InboundAssociation && ShouldDropInbound(ev.AsInstanceOf<InboundAssociation>().Association.RemoteAddress, ev, "notify"))
307+
if (ev is InboundAssociation inboundAssociation && ShouldDropInbound(inboundAssociation.Association.RemoteAddress, ev, "notify"))
308308
{
309309
//ignore
310310
}
@@ -374,7 +374,10 @@ public bool ShouldDropOutbound(Address remoteAddress, object instance, string de
374374

375375
private IAssociationEvent InterceptInboundAssociation(IAssociationEvent ev)
376376
{
377-
if (ev is InboundAssociation) return new InboundAssociation(new FailureInjectorHandle(ev.AsInstanceOf<InboundAssociation>().Association, this));
377+
if (ev is InboundAssociation inboundAssociation)
378+
{
379+
return new InboundAssociation(new FailureInjectorHandle(inboundAssociation.Association, this));
380+
}
378381
return ev;
379382
}
380383

src/core/Akka.Remote/Transport/ThrottleTransportAdapter.cs

+25-31
Original file line numberDiff line numberDiff line change
@@ -1115,29 +1115,28 @@ private void InitializeFSM()
11151115
{
11161116
When(ThrottlerState.WaitExposedHandle, @event =>
11171117
{
1118-
if (@event.FsmEvent is ThrottlerManager.Handle && @event.StateData is Uninitialized)
1118+
if (@event is { FsmEvent: ThrottlerManager.Handle handle, StateData: Uninitialized })
11191119
{
11201120
// register to downstream layer and wait for origin
11211121
OriginalHandle.ReadHandlerSource.SetResult(new ActorHandleEventListener(Self));
11221122
return
11231123
GoTo(ThrottlerState.WaitOrigin)
11241124
.Using(
1125-
new ExposedHandle(
1126-
@event.FsmEvent.AsInstanceOf<ThrottlerManager.Handle>().ThrottlerHandle));
1125+
new ExposedHandle(handle.ThrottlerHandle));
11271126
}
11281127
return null;
11291128
});
11301129

11311130
When(ThrottlerState.WaitOrigin, @event =>
11321131
{
1133-
if (@event.FsmEvent is InboundPayload && @event.StateData is ExposedHandle)
1132+
if (@event is { FsmEvent: InboundPayload payload, StateData: ExposedHandle handle })
11341133
{
1135-
var b = @event.FsmEvent.AsInstanceOf<InboundPayload>().Payload;
1134+
var b = payload.Payload;
11361135
ThrottledMessages.Enqueue(b);
11371136
var origin = PeekOrigin(b);
11381137
if (origin != null)
11391138
{
1140-
Manager.Tell(new ThrottlerManager.Checkin(origin, @event.StateData.AsInstanceOf<ExposedHandle>().Handle));
1139+
Manager.Tell(new ThrottlerManager.Checkin(origin, handle.Handle));
11411140
return GoTo(ThrottlerState.WaitMode);
11421141
}
11431142
return Stay();
@@ -1147,17 +1146,16 @@ private void InitializeFSM()
11471146

11481147
When(ThrottlerState.WaitMode, @event =>
11491148
{
1150-
if (@event.FsmEvent is InboundPayload)
1149+
if (@event.FsmEvent is InboundPayload payload)
11511150
{
1152-
var b = @event.FsmEvent.AsInstanceOf<InboundPayload>().Payload;
1151+
var b = payload.Payload;
11531152
ThrottledMessages.Enqueue(b);
11541153
return Stay();
11551154
}
11561155

1157-
if (@event.FsmEvent is ThrottleMode && @event.StateData is ExposedHandle)
1156+
if (@event is { FsmEvent: ThrottleMode mode, StateData: ExposedHandle handle })
11581157
{
1159-
var mode = @event.FsmEvent.AsInstanceOf<ThrottleMode>();
1160-
var exposedHandle = @event.StateData.AsInstanceOf<ExposedHandle>().Handle;
1158+
var exposedHandle = handle.Handle;
11611159
InboundThrottleMode = mode;
11621160
try
11631161
{
@@ -1191,16 +1189,15 @@ private void InitializeFSM()
11911189

11921190
When(ThrottlerState.WaitUpstreamListener, @event =>
11931191
{
1194-
if (@event.FsmEvent is InboundPayload)
1192+
if (@event.FsmEvent is InboundPayload payload)
11951193
{
1196-
var b = @event.FsmEvent.AsInstanceOf<InboundPayload>();
1197-
ThrottledMessages.Enqueue(b.Payload);
1194+
ThrottledMessages.Enqueue(payload.Payload);
11981195
return Stay();
11991196
}
12001197

1201-
if (@event.FsmEvent is ThrottlerManager.Listener)
1198+
if (@event.FsmEvent is ThrottlerManager.Listener listener)
12021199
{
1203-
UpstreamListener = @event.FsmEvent.AsInstanceOf<ThrottlerManager.Listener>().HandleEventListener;
1200+
UpstreamListener = listener.HandleEventListener;
12041201
Self.Tell(new Dequeue());
12051202
return GoTo(ThrottlerState.Throttling);
12061203
}
@@ -1210,19 +1207,17 @@ private void InitializeFSM()
12101207

12111208
When(ThrottlerState.WaitModeAndUpstreamListener, @event =>
12121209
{
1213-
if (@event.FsmEvent is ThrottlerManager.ListenerAndMode)
1210+
if (@event.FsmEvent is ThrottlerManager.ListenerAndMode listenerAndMode)
12141211
{
1215-
var listenerAndMode = @event.FsmEvent.AsInstanceOf<ThrottlerManager.ListenerAndMode>();
12161212
UpstreamListener = listenerAndMode.HandleEventListener;
12171213
InboundThrottleMode = listenerAndMode.Mode;
12181214
Self.Tell(new Dequeue());
12191215
return GoTo(ThrottlerState.Throttling);
12201216
}
12211217

1222-
if (@event.FsmEvent is InboundPayload)
1218+
if (@event.FsmEvent is InboundPayload inboundPayload)
12231219
{
1224-
var b = @event.FsmEvent.AsInstanceOf<InboundPayload>();
1225-
ThrottledMessages.Enqueue(b.Payload);
1220+
ThrottledMessages.Enqueue(inboundPayload.Payload);
12261221
return Stay();
12271222
}
12281223

@@ -1231,9 +1226,8 @@ private void InitializeFSM()
12311226

12321227
When(ThrottlerState.Throttling, @event =>
12331228
{
1234-
if (@event.FsmEvent is ThrottleMode)
1229+
if (@event.FsmEvent is ThrottleMode mode)
12351230
{
1236-
var mode = @event.FsmEvent.AsInstanceOf<ThrottleMode>();
12371231
InboundThrottleMode = mode;
12381232
if (mode is Blackhole) ThrottledMessages = new Queue<ByteString>();
12391233
CancelTimer(DequeueTimerName);
@@ -1243,9 +1237,9 @@ private void InitializeFSM()
12431237
return Stay();
12441238
}
12451239

1246-
if (@event.FsmEvent is InboundPayload)
1240+
if (@event.FsmEvent is InboundPayload inboundPayload)
12471241
{
1248-
ForwardOrDelay(@event.FsmEvent.AsInstanceOf<InboundPayload>().Payload);
1242+
ForwardOrDelay(inboundPayload.Payload);
12491243
return Stay();
12501244
}
12511245

@@ -1269,9 +1263,9 @@ private void InitializeFSM()
12691263
WhenUnhandled(@event =>
12701264
{
12711265
// we should always set the throttling mode
1272-
if (@event.FsmEvent is ThrottleMode)
1266+
if (@event.FsmEvent is ThrottleMode mode)
12731267
{
1274-
InboundThrottleMode = @event.FsmEvent.AsInstanceOf<ThrottleMode>();
1268+
InboundThrottleMode = mode;
12751269
Sender.Tell(SetThrottleAck.Instance);
12761270
return Stay();
12771271
}
@@ -1281,9 +1275,9 @@ private void InitializeFSM()
12811275
return Stop(); // not notifying the upstream handler is intentional: we are relying on heartbeating
12821276
}
12831277

1284-
if (@event.FsmEvent is FailWith)
1278+
if (@event.FsmEvent is FailWith failWith)
12851279
{
1286-
var reason = @event.FsmEvent.AsInstanceOf<FailWith>().FailReason;
1280+
var reason = failWith.FailReason;
12871281
if (UpstreamListener != null) UpstreamListener.Notify(new Disassociated(reason));
12881282
return Stop();
12891283
}
@@ -1310,9 +1304,9 @@ private Address PeekOrigin(ByteString b)
13101304
try
13111305
{
13121306
var pdu = _codec.DecodePdu(b);
1313-
if (pdu is Associate)
1307+
if (pdu is Associate associate)
13141308
{
1315-
return pdu.AsInstanceOf<Associate>().Info.Origin;
1309+
return associate.Info.Origin;
13161310
}
13171311
return null;
13181312
}

src/core/Akka.Tests/Routing/RoutingSpec.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ await AwaitConditionAsync(async () =>
184184

185185
var res = await ReceiveWhileAsync(100.Milliseconds(), x =>
186186
{
187-
if (x is IActorRef)
188-
return x.AsInstanceOf<IActorRef>();
187+
if (x is IActorRef actorRef)
188+
return actorRef;
189189

190190
return null;
191191
}, msgs: 2).ToListAsync();

src/core/Akka/Actor/FSM.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1332,9 +1332,9 @@ protected virtual void LogTermination(Reason reason)
13321332
{
13331333
if (reason is Failure failure)
13341334
{
1335-
if (failure.Cause is Exception)
1335+
if (failure.Cause is Exception exception)
13361336
{
1337-
_log.Error(failure.Cause.AsInstanceOf<Exception>(), "terminating due to Failure");
1337+
_log.Error(exception, "terminating due to Failure");
13381338
}
13391339
else
13401340
{

src/core/Akka/Serialization/Serialization.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -561,9 +561,9 @@ public static string SerializedActorPath(IActorRef actorRef)
561561

562562
var path = actorRef.Path;
563563
ExtendedActorSystem originalSystem = null;
564-
if (actorRef is ActorRefWithCell)
564+
if (actorRef is ActorRefWithCell actorRefWithCell)
565565
{
566-
originalSystem = actorRef.AsInstanceOf<ActorRefWithCell>().Underlying.System.AsInstanceOf<ExtendedActorSystem>();
566+
originalSystem = actorRefWithCell.Underlying.System.AsInstanceOf<ExtendedActorSystem>();
567567
}
568568

569569
if (CurrentTransportInformation == null)

0 commit comments

Comments
 (0)