Skip to content

Commit

Permalink
remove redundant casts via AsInstanceOf (#6744)
Browse files Browse the repository at this point in the history
Co-authored-by: Aaron Stannard <[email protected]>
  • Loading branch information
SimonCropp and Aaronontheweb authored May 10, 2023
1 parent d783933 commit 4f27bac
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 53 deletions.
4 changes: 2 additions & 2 deletions src/core/Akka.Persistence/Fsm/PersistentFSMBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,9 @@ protected virtual void LogTermination(FSMBase.Reason reason)
{
if (reason is FSMBase.Failure failure)
{
if (failure.Cause is Exception)
if (failure.Cause is Exception exception)
{
_log.Error(failure.Cause.AsInstanceOf<Exception>(), "terminating due to Failure");
_log.Error(exception, "terminating due to Failure");
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/core/Akka.Remote/Endpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1438,8 +1438,8 @@ private void EnqueueInBuffer(object message)
var send = message as EndpointManager.Send;
if (send != null && send.Message is IPriorityMessage)
_prioBuffer.AddLast(send);
else if (send != null && send.Message is ActorSelectionMessage &&
send.Message.AsInstanceOf<ActorSelectionMessage>().Message is IPriorityMessage)
else if (send != null && send.Message is ActorSelectionMessage actorSelectionMessage &&
actorSelectionMessage.Message is IPriorityMessage)
{
_prioBuffer.AddLast(send);
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka.Remote/EndpointManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ protected void Accepting()
//the quarantine uid has lost the race with some failure, do nothing
}
}
else if (policy.Item1 is Quarantined && policy.Item2 != null && policy.Item1.AsInstanceOf<Quarantined>().Uid == policy.Item2.Value)
else if (policy.Item1 is Quarantined quarantined && policy.Item2 != null && quarantined.Uid == policy.Item2.Value)
{
// the UID to be quarantined already exists, do nothing
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/Akka.Remote/RemoteActorRefProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,9 @@ public IInternalActorRef ActorOf(ActorSystemImpl system, Props props, IInternalA
.Aggregate(Deploy.None, (deploy1, deploy2) => deploy2.WithFallback(deploy1));

//match for remote scope
if (propsDeploy.Scope is RemoteScope)
if (propsDeploy.Scope is RemoteScope remoteScope)
{
var addr = propsDeploy.Scope.AsInstanceOf<RemoteScope>().Address;
var addr = remoteScope.Address;

//Even if this actor is in RemoteScope, it might still be a local address
if (HasAddress(addr))
Expand Down
4 changes: 2 additions & 2 deletions src/core/Akka.Remote/RemoteDeployer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ private static Deploy CheckRemoteRouterConfig(Deploy deploy)
var nodes = deploy.Config.GetStringList("target.nodes", new string[] { }).Select(Address.Parse).ToList();
if (nodes.Any() && deploy.RouterConfig != null)
{
if (deploy.RouterConfig is Pool)
if (deploy.RouterConfig is Pool pool)
return
deploy.WithRouterConfig(new RemoteRouterConfig(deploy.RouterConfig.AsInstanceOf<Pool>(), nodes));
deploy.WithRouterConfig(new RemoteRouterConfig(pool, nodes));
return deploy.WithScope(scope: Deploy.NoScopeGiven);
}
else
Expand Down
9 changes: 4 additions & 5 deletions src/core/Akka.Remote/Transport/AkkaProtocolTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1120,11 +1120,10 @@ AssociationHandle GetHandle(ProtocolStateData data)
// Invalidate exposed but still unfinished promise. The underlying association disappeared, so after
// registration immediately signal a disassociate
Disassociated disassociateNotification;
if (@event.Reason is Failure && @event.Reason.AsInstanceOf<Failure>().Cause is DisassociateInfo)
if (@event.Reason is Failure { Cause: DisassociateInfo disassociateInfo })
{
disassociateNotification =
new Disassociated(@event.Reason.AsInstanceOf<Failure>().Cause
.AsInstanceOf<DisassociateInfo>());
new Disassociated(disassociateInfo);
}
else
{
Expand All @@ -1140,10 +1139,10 @@ AssociationHandle GetHandle(ProtocolStateData data)
case ListenerReady lr:
{
Disassociated disassociateNotification;
if (@event.Reason is Failure failure && failure.Cause is DisassociateInfo)
if (@event.Reason is Failure { Cause: DisassociateInfo disassociateInfo })
{
disassociateNotification =
new Disassociated(failure.Cause.AsInstanceOf<DisassociateInfo>());
new Disassociated(disassociateInfo);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ protected override void InterceptAssociate(Address remoteAddress, TaskCompletion
/// <param name="ev">TBD</param>
public void Notify(IAssociationEvent ev)
{
if (ev is InboundAssociation && ShouldDropInbound(ev.AsInstanceOf<InboundAssociation>().Association.RemoteAddress, ev, "notify"))
if (ev is InboundAssociation inboundAssociation && ShouldDropInbound(inboundAssociation.Association.RemoteAddress, ev, "notify"))
{
//ignore
}
Expand Down Expand Up @@ -374,7 +374,10 @@ public bool ShouldDropOutbound(Address remoteAddress, object instance, string de

private IAssociationEvent InterceptInboundAssociation(IAssociationEvent ev)
{
if (ev is InboundAssociation) return new InboundAssociation(new FailureInjectorHandle(ev.AsInstanceOf<InboundAssociation>().Association, this));
if (ev is InboundAssociation inboundAssociation)
{
return new InboundAssociation(new FailureInjectorHandle(inboundAssociation.Association, this));
}
return ev;
}

Expand Down
56 changes: 25 additions & 31 deletions src/core/Akka.Remote/Transport/ThrottleTransportAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1115,29 +1115,28 @@ private void InitializeFSM()
{
When(ThrottlerState.WaitExposedHandle, @event =>
{
if (@event.FsmEvent is ThrottlerManager.Handle && @event.StateData is Uninitialized)
if (@event is { FsmEvent: ThrottlerManager.Handle handle, StateData: Uninitialized })
{
// register to downstream layer and wait for origin
OriginalHandle.ReadHandlerSource.SetResult(new ActorHandleEventListener(Self));
return
GoTo(ThrottlerState.WaitOrigin)
.Using(
new ExposedHandle(
@event.FsmEvent.AsInstanceOf<ThrottlerManager.Handle>().ThrottlerHandle));
new ExposedHandle(handle.ThrottlerHandle));
}
return null;
});

When(ThrottlerState.WaitOrigin, @event =>
{
if (@event.FsmEvent is InboundPayload && @event.StateData is ExposedHandle)
if (@event is { FsmEvent: InboundPayload payload, StateData: ExposedHandle handle })
{
var b = @event.FsmEvent.AsInstanceOf<InboundPayload>().Payload;
var b = payload.Payload;
ThrottledMessages.Enqueue(b);
var origin = PeekOrigin(b);
if (origin != null)
{
Manager.Tell(new ThrottlerManager.Checkin(origin, @event.StateData.AsInstanceOf<ExposedHandle>().Handle));
Manager.Tell(new ThrottlerManager.Checkin(origin, handle.Handle));
return GoTo(ThrottlerState.WaitMode);
}
return Stay();
Expand All @@ -1147,17 +1146,16 @@ private void InitializeFSM()

When(ThrottlerState.WaitMode, @event =>
{
if (@event.FsmEvent is InboundPayload)
if (@event.FsmEvent is InboundPayload payload)
{
var b = @event.FsmEvent.AsInstanceOf<InboundPayload>().Payload;
var b = payload.Payload;
ThrottledMessages.Enqueue(b);
return Stay();
}

if (@event.FsmEvent is ThrottleMode && @event.StateData is ExposedHandle)
if (@event is { FsmEvent: ThrottleMode mode, StateData: ExposedHandle handle })
{
var mode = @event.FsmEvent.AsInstanceOf<ThrottleMode>();
var exposedHandle = @event.StateData.AsInstanceOf<ExposedHandle>().Handle;
var exposedHandle = handle.Handle;
InboundThrottleMode = mode;
try
{
Expand Down Expand Up @@ -1191,16 +1189,15 @@ private void InitializeFSM()

When(ThrottlerState.WaitUpstreamListener, @event =>
{
if (@event.FsmEvent is InboundPayload)
if (@event.FsmEvent is InboundPayload payload)
{
var b = @event.FsmEvent.AsInstanceOf<InboundPayload>();
ThrottledMessages.Enqueue(b.Payload);
ThrottledMessages.Enqueue(payload.Payload);
return Stay();
}

if (@event.FsmEvent is ThrottlerManager.Listener)
if (@event.FsmEvent is ThrottlerManager.Listener listener)
{
UpstreamListener = @event.FsmEvent.AsInstanceOf<ThrottlerManager.Listener>().HandleEventListener;
UpstreamListener = listener.HandleEventListener;
Self.Tell(new Dequeue());
return GoTo(ThrottlerState.Throttling);
}
Expand All @@ -1210,19 +1207,17 @@ private void InitializeFSM()

When(ThrottlerState.WaitModeAndUpstreamListener, @event =>
{
if (@event.FsmEvent is ThrottlerManager.ListenerAndMode)
if (@event.FsmEvent is ThrottlerManager.ListenerAndMode listenerAndMode)
{
var listenerAndMode = @event.FsmEvent.AsInstanceOf<ThrottlerManager.ListenerAndMode>();
UpstreamListener = listenerAndMode.HandleEventListener;
InboundThrottleMode = listenerAndMode.Mode;
Self.Tell(new Dequeue());
return GoTo(ThrottlerState.Throttling);
}

if (@event.FsmEvent is InboundPayload)
if (@event.FsmEvent is InboundPayload inboundPayload)
{
var b = @event.FsmEvent.AsInstanceOf<InboundPayload>();
ThrottledMessages.Enqueue(b.Payload);
ThrottledMessages.Enqueue(inboundPayload.Payload);
return Stay();
}

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

When(ThrottlerState.Throttling, @event =>
{
if (@event.FsmEvent is ThrottleMode)
if (@event.FsmEvent is ThrottleMode mode)
{
var mode = @event.FsmEvent.AsInstanceOf<ThrottleMode>();
InboundThrottleMode = mode;
if (mode is Blackhole) ThrottledMessages = new Queue<ByteString>();
CancelTimer(DequeueTimerName);
Expand All @@ -1243,9 +1237,9 @@ private void InitializeFSM()
return Stay();
}

if (@event.FsmEvent is InboundPayload)
if (@event.FsmEvent is InboundPayload inboundPayload)
{
ForwardOrDelay(@event.FsmEvent.AsInstanceOf<InboundPayload>().Payload);
ForwardOrDelay(inboundPayload.Payload);
return Stay();
}

Expand All @@ -1269,9 +1263,9 @@ private void InitializeFSM()
WhenUnhandled(@event =>
{
// we should always set the throttling mode
if (@event.FsmEvent is ThrottleMode)
if (@event.FsmEvent is ThrottleMode mode)
{
InboundThrottleMode = @event.FsmEvent.AsInstanceOf<ThrottleMode>();
InboundThrottleMode = mode;
Sender.Tell(SetThrottleAck.Instance);
return Stay();
}
Expand All @@ -1281,9 +1275,9 @@ private void InitializeFSM()
return Stop(); // not notifying the upstream handler is intentional: we are relying on heartbeating
}

if (@event.FsmEvent is FailWith)
if (@event.FsmEvent is FailWith failWith)
{
var reason = @event.FsmEvent.AsInstanceOf<FailWith>().FailReason;
var reason = failWith.FailReason;
if (UpstreamListener != null) UpstreamListener.Notify(new Disassociated(reason));
return Stop();
}
Expand All @@ -1310,9 +1304,9 @@ private Address PeekOrigin(ByteString b)
try
{
var pdu = _codec.DecodePdu(b);
if (pdu is Associate)
if (pdu is Associate associate)
{
return pdu.AsInstanceOf<Associate>().Info.Origin;
return associate.Info.Origin;
}
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/Akka.Tests/Routing/RoutingSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ await AwaitConditionAsync(async () =>

var res = await ReceiveWhileAsync(100.Milliseconds(), x =>
{
if (x is IActorRef)
return x.AsInstanceOf<IActorRef>();
if (x is IActorRef actorRef)
return actorRef;

return null;
}, msgs: 2).ToListAsync();
Expand Down
4 changes: 2 additions & 2 deletions src/core/Akka/Actor/FSM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1332,9 +1332,9 @@ protected virtual void LogTermination(Reason reason)
{
if (reason is Failure failure)
{
if (failure.Cause is Exception)
if (failure.Cause is Exception exception)
{
_log.Error(failure.Cause.AsInstanceOf<Exception>(), "terminating due to Failure");
_log.Error(exception, "terminating due to Failure");
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/core/Akka/Serialization/Serialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,9 @@ public static string SerializedActorPath(IActorRef actorRef)

var path = actorRef.Path;
ExtendedActorSystem originalSystem = null;
if (actorRef is ActorRefWithCell)
if (actorRef is ActorRefWithCell actorRefWithCell)
{
originalSystem = actorRef.AsInstanceOf<ActorRefWithCell>().Underlying.System.AsInstanceOf<ExtendedActorSystem>();
originalSystem = actorRefWithCell.Underlying.System.AsInstanceOf<ExtendedActorSystem>();
}

if (CurrentTransportInformation == null)
Expand Down

0 comments on commit 4f27bac

Please sign in to comment.