mirror of
https://github.com/apple/swift-nio
synced 2026-11-27 05:04:22 +00:00
Prefer if case over switch
Motivation:
There are quite a few `switch`es that cover just two cases. Explicitly state this via an `if case` + `else` structure.
Modifications:
Changes something like:
switch a {
case .b:
return c
default:
return d
}
to
if case .b = a {
return c
} else {
return d
}
Result:
This should prevent misinterpretation and thus potential bugs in the future.
This commit is contained in:
1 parent
4c89a4e675
commit
a095decbc2
11 files changed
+46
-69
No files matched your search
@@ -528,11 +528,10 @@ internal class HappyEyeballsConnector {
|
||||
channelFuture.whenSuccess { channel in
|
||||
// If we are in the complete state then we want to abandon this channel. Otherwise, begin
|
||||
// connecting.
|
||||
switch self.state {
|
||||
case .complete:
|
||||
if case .complete = self.state {
|
||||
self.pendingConnections.remove(element: channelFuture)
|
||||
channel.close(promise: nil)
|
||||
default:
|
||||
} else {
|
||||
channel.connect(to: target).map {
|
||||
// The channel has connected. If we are in the complete state we want to abandon this channel.
|
||||
// Otherwise, fire the channel connected event. Either way we don't want the channel future to
|
||||
@@ -540,24 +539,22 @@ internal class HappyEyeballsConnector {
|
||||
// we want to use.
|
||||
self.pendingConnections.remove(element: channelFuture)
|
||||
|
||||
switch self.state {
|
||||
case .complete:
|
||||
if case .complete = self.state {
|
||||
channel.close(promise: nil)
|
||||
default:
|
||||
} else {
|
||||
self.processInput(.connectSuccess)
|
||||
self.resolutionPromise.succeed(result: channel)
|
||||
}
|
||||
}.whenFailure { err in
|
||||
// The connection attempt failed. If we're in the complete state then there's nothing
|
||||
// to do. Otherwise, notify the state machine of the failure.
|
||||
switch self.state {
|
||||
case .complete:
|
||||
assert(self.pendingConnections.index { $0 === channelFuture } == nil, "failed but was still in pending connections")
|
||||
default:
|
||||
self.error.connectionErrors.append(SingleConnectionFailure(target: target, error: err))
|
||||
self.pendingConnections.remove(element: channelFuture)
|
||||
self.processInput(.connectFailed)
|
||||
}
|
||||
}.whenFailure { err in
|
||||
// The connection attempt failed. If we're in the complete state then there's nothing
|
||||
// to do. Otherwise, notify the state machine of the failure.
|
||||
if case .complete = self.state {
|
||||
assert(self.pendingConnections.index { $0 === channelFuture } == nil, "failed but was still in pending connections")
|
||||
} else {
|
||||
self.error.connectionErrors.append(SingleConnectionFailure(target: target, error: err))
|
||||
self.pendingConnections.remove(element: channelFuture)
|
||||
self.processInput(.connectFailed)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,10 +258,9 @@ internal enum Posix {
|
||||
return fd
|
||||
}
|
||||
|
||||
switch result {
|
||||
case .processed(let fd):
|
||||
if case .processed(let fd) = result {
|
||||
return fd
|
||||
default:
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,11 +155,8 @@ public final class HTTPResponseDecoder: HTTPDecoder<HTTPClientResponsePart>, Cha
|
||||
}
|
||||
|
||||
public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
|
||||
switch unwrapOutboundIn(data) {
|
||||
case .head(let head):
|
||||
if case .head(let head) = unwrapOutboundIn(data) {
|
||||
methods.append(head.method)
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
ctx.write(data, promise: promise)
|
||||
|
||||
@@ -103,13 +103,10 @@ public final class HTTPResponseCompressor: ChannelDuplexHandler {
|
||||
}
|
||||
|
||||
public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
|
||||
let httpData = unwrapInboundIn(data)
|
||||
switch httpData {
|
||||
case .head(let requestHead):
|
||||
if case .head(let requestHead) = unwrapInboundIn(data) {
|
||||
acceptQueue.append(requestHead.headers.getCanonicalForm("accept-encoding"))
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
ctx.fireChannelRead(data)
|
||||
}
|
||||
|
||||
|
||||
@@ -995,11 +995,10 @@ extension HTTPResponseStatus {
|
||||
/// - Parameter buffer: A buffer to write the serialized bytes into. Will increment
|
||||
/// the writer index of this buffer.
|
||||
func write(buffer: inout ByteBuffer) {
|
||||
switch self {
|
||||
case .ok:
|
||||
if case .ok = self {
|
||||
// Optimize for 200 ok, which should be the most likely code (...hopefully).
|
||||
buffer.write(staticString: status200)
|
||||
default:
|
||||
} else {
|
||||
buffer.write(string: String(code))
|
||||
buffer.write(string: " ")
|
||||
buffer.write(string: reasonPhrase)
|
||||
|
||||
@@ -190,7 +190,7 @@ private final class HTTPHandler: ChannelInboundHandler {
|
||||
case .end:
|
||||
self.state.requestComplete()
|
||||
default:
|
||||
()
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -92,10 +92,9 @@ public class ApplicationProtocolNegotiationHandler: ChannelInboundHandler {
|
||||
return
|
||||
}
|
||||
|
||||
switch tlsEvent {
|
||||
case .handshakeCompleted(let p):
|
||||
if case .handshakeCompleted(let p) = tlsEvent {
|
||||
handshakeCompleted(context: ctx, negotiatedProtocol: p)
|
||||
default:
|
||||
} else {
|
||||
ctx.fireUserInboundEventTriggered(event)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -301,28 +301,25 @@ class ChannelPipelineTest: XCTestCase {
|
||||
|
||||
/* the first thing, we should receive is `[-2]` as it shouldn't hit any `MarkingOutboundHandler`s (`4`) */
|
||||
var outbound = channel.readOutbound()
|
||||
switch outbound {
|
||||
case .some(.byteBuffer(var buf)):
|
||||
if case .some(.byteBuffer(var buf)) = outbound {
|
||||
XCTAssertEqual("[-2]", buf.readString(length: buf.readableBytes))
|
||||
default:
|
||||
} else {
|
||||
XCTFail("wrong contents: \(outbound.debugDescription)")
|
||||
}
|
||||
|
||||
/* the next thing we should receive is `[-2, 4]` as the first `WriteOnReadHandler` (receiving `[2]`) is behind the `MarkingOutboundHandler` (`4`) */
|
||||
outbound = channel.readOutbound()
|
||||
switch outbound {
|
||||
case .some(.byteBuffer(var buf)):
|
||||
if case .some(.byteBuffer(var buf)) = outbound {
|
||||
XCTAssertEqual("[-2, 4]", buf.readString(length: buf.readableBytes))
|
||||
default:
|
||||
} else {
|
||||
XCTFail("wrong contents: \(outbound.debugDescription)")
|
||||
}
|
||||
|
||||
/* and finally, we're waiting for `[-2, -6, 4]` as the second `WriteOnReadHandler`s (receiving `[2, 4]`) is behind the `MarkingOutboundHandler` (`4`) */
|
||||
outbound = channel.readOutbound()
|
||||
switch outbound {
|
||||
case .some(.byteBuffer(var buf)):
|
||||
if case .some(.byteBuffer(var buf)) = outbound {
|
||||
XCTAssertEqual("[-2, -6, 4]", buf.readString(length: buf.readableBytes))
|
||||
default:
|
||||
} else {
|
||||
XCTFail("wrong contents: \(outbound.debugDescription)")
|
||||
}
|
||||
|
||||
|
||||
@@ -1129,10 +1129,9 @@ public class ChannelTests: XCTestCase {
|
||||
let written = try buffer.withUnsafeReadableBytes { p in
|
||||
try accepted.write(pointer: p.baseAddress!.assumingMemoryBound(to: UInt8.self), size: 4)
|
||||
}
|
||||
switch written {
|
||||
case .processed(let numBytes):
|
||||
if case .processed(let numBytes) = written {
|
||||
XCTAssertEqual(4, numBytes)
|
||||
default:
|
||||
} else {
|
||||
XCTFail()
|
||||
}
|
||||
try byteCountingHandler.assertReceived(buffer: buffer)
|
||||
|
||||
@@ -312,10 +312,9 @@ public class HappyEyeballsTest : XCTestCase {
|
||||
loop.advanceTime(by: .seconds(1))
|
||||
XCTAssertEqual(resolver.events, expectedQueries + [.cancel])
|
||||
|
||||
switch channelFuture.getError() {
|
||||
case .some(ChannelError.connectTimeout(let amount)):
|
||||
if case .some(ChannelError.connectTimeout(let amount)) = channelFuture.getError() {
|
||||
XCTAssertEqual(amount, .seconds(10))
|
||||
default:
|
||||
} else {
|
||||
XCTFail("Got \(String(describing: channelFuture.getError()))")
|
||||
}
|
||||
|
||||
@@ -523,14 +522,13 @@ public class HappyEyeballsTest : XCTestCase {
|
||||
XCTAssertEqual(resolver.events, expectedQueries)
|
||||
|
||||
// But we should have failed.
|
||||
switch channelFuture.getError() {
|
||||
case .some(ChannelError.connectFailed(let inner)):
|
||||
if case .some(ChannelError.connectFailed(let inner)) = channelFuture.getError() {
|
||||
XCTAssertEqual(inner.host, "example.com")
|
||||
XCTAssertEqual(inner.port, 80)
|
||||
XCTAssertNil(inner.dnsAError)
|
||||
XCTAssertNil(inner.dnsAAAAError)
|
||||
XCTAssertEqual(inner.connectionErrors.count, 0)
|
||||
default:
|
||||
} else {
|
||||
XCTFail("Got \(String(describing: channelFuture.getError()))")
|
||||
}
|
||||
}
|
||||
@@ -556,14 +554,13 @@ public class HappyEyeballsTest : XCTestCase {
|
||||
XCTAssertEqual(resolver.events, expectedQueries)
|
||||
|
||||
// But we should have failed.
|
||||
switch channelFuture.getError() {
|
||||
case .some(ChannelError.connectFailed(let inner)):
|
||||
if case .some(ChannelError.connectFailed(let inner)) = channelFuture.getError() {
|
||||
XCTAssertEqual(inner.host, "example.com")
|
||||
XCTAssertEqual(inner.port, 80)
|
||||
XCTAssertEqual(inner.dnsAError as? DummyError ?? DummyError(), v4Error)
|
||||
XCTAssertEqual(inner.dnsAAAAError as? DummyError ?? DummyError(), v6Error)
|
||||
XCTAssertEqual(inner.connectionErrors.count, 0)
|
||||
default:
|
||||
} else {
|
||||
XCTFail("Got \(String(describing: channelFuture.getError()))")
|
||||
}
|
||||
}
|
||||
@@ -692,8 +689,7 @@ public class HappyEyeballsTest : XCTestCase {
|
||||
XCTAssertTrue(channelFuture.fulfilled)
|
||||
|
||||
// Check the error.
|
||||
switch channelFuture.getError() {
|
||||
case .some(ChannelError.connectFailed(let inner)):
|
||||
if case .some(ChannelError.connectFailed(let inner)) = channelFuture.getError() {
|
||||
XCTAssertEqual(inner.host, "example.com")
|
||||
XCTAssertEqual(inner.port, 80)
|
||||
XCTAssertNil(inner.dnsAError)
|
||||
@@ -703,7 +699,7 @@ public class HappyEyeballsTest : XCTestCase {
|
||||
for (idx, error) in inner.connectionErrors.enumerated() {
|
||||
XCTAssertEqual(error.error as? DummyError, errors[idx])
|
||||
}
|
||||
default:
|
||||
} else {
|
||||
XCTFail("Got \(String(describing: channelFuture.getError()))")
|
||||
}
|
||||
}
|
||||
@@ -1145,10 +1141,9 @@ public class HappyEyeballsTest : XCTestCase {
|
||||
XCTAssertEqual(errors.count, 20)
|
||||
|
||||
XCTAssertTrue(channelFuture.fulfilled)
|
||||
switch channelFuture.getError() {
|
||||
case .some(ChannelError.connectFailed(let inner)):
|
||||
if case .some(ChannelError.connectFailed(let inner)) = channelFuture.getError() {
|
||||
XCTAssertEqual(inner.connectionErrors.map { $0.error as! DummyError }, errors)
|
||||
default:
|
||||
} else {
|
||||
XCTFail("Got unexpected error: \(String(describing: channelFuture.getError()))")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,8 +53,7 @@ class SocketAddressTest: XCTestCase {
|
||||
func testCanCreateIPv4AddressFromString() throws {
|
||||
let sa = try SocketAddress(ipAddress: "127.0.0.1", port: 80)
|
||||
let expectedAddress: [UInt8] = [0x7F, 0x00, 0x00, 0x01]
|
||||
switch sa {
|
||||
case .v4(let address):
|
||||
if case .v4(let address) = sa {
|
||||
var addr = address.address
|
||||
let host = address.host
|
||||
XCTAssertEqual(host, "")
|
||||
@@ -66,7 +65,7 @@ class SocketAddressTest: XCTestCase {
|
||||
XCTAssertEqual(rc, 0)
|
||||
}
|
||||
}
|
||||
default:
|
||||
} else {
|
||||
XCTFail("Invalid address: \(sa)")
|
||||
}
|
||||
}
|
||||
@@ -74,8 +73,7 @@ class SocketAddressTest: XCTestCase {
|
||||
func testCanCreateIPv6AddressFromString() throws {
|
||||
let sa = try SocketAddress(ipAddress: "fe80::5", port: 443)
|
||||
let expectedAddress: [UInt8] = [0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05]
|
||||
switch sa {
|
||||
case .v6(let address):
|
||||
if case .v6(let address) = sa {
|
||||
var addr = address.address
|
||||
let host = address.host
|
||||
XCTAssertEqual(host, "")
|
||||
@@ -89,7 +87,7 @@ class SocketAddressTest: XCTestCase {
|
||||
XCTAssertEqual(rc, 0)
|
||||
}
|
||||
}
|
||||
default:
|
||||
} else {
|
||||
XCTFail("Invalid address: \(sa)")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user