diff --git a/Sources/NIO/HappyEyeballs.swift b/Sources/NIO/HappyEyeballs.swift index 19cfc0d28..7ba5c83c6 100644 --- a/Sources/NIO/HappyEyeballs.swift +++ b/Sources/NIO/HappyEyeballs.swift @@ -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) + } } } } diff --git a/Sources/NIO/System.swift b/Sources/NIO/System.swift index 318afc33e..921fd5316 100644 --- a/Sources/NIO/System.swift +++ b/Sources/NIO/System.swift @@ -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 } } diff --git a/Sources/NIOHTTP1/HTTPDecoder.swift b/Sources/NIOHTTP1/HTTPDecoder.swift index 4905d6f53..9bfbb8d35 100644 --- a/Sources/NIOHTTP1/HTTPDecoder.swift +++ b/Sources/NIOHTTP1/HTTPDecoder.swift @@ -155,11 +155,8 @@ public final class HTTPResponseDecoder: HTTPDecoder, Cha } public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise?) { - 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) diff --git a/Sources/NIOHTTP1/HTTPResponseCompressor.swift b/Sources/NIOHTTP1/HTTPResponseCompressor.swift index bb5e5913e..eaea2b59e 100644 --- a/Sources/NIOHTTP1/HTTPResponseCompressor.swift +++ b/Sources/NIOHTTP1/HTTPResponseCompressor.swift @@ -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) } diff --git a/Sources/NIOHTTP1/HTTPTypes.swift b/Sources/NIOHTTP1/HTTPTypes.swift index ab4682f41..51607c679 100644 --- a/Sources/NIOHTTP1/HTTPTypes.swift +++ b/Sources/NIOHTTP1/HTTPTypes.swift @@ -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) diff --git a/Sources/NIOHTTP1Server/main.swift b/Sources/NIOHTTP1Server/main.swift index 5eb962ee6..44d97722d 100644 --- a/Sources/NIOHTTP1Server/main.swift +++ b/Sources/NIOHTTP1Server/main.swift @@ -190,7 +190,7 @@ private final class HTTPHandler: ChannelInboundHandler { case .end: self.state.requestComplete() default: - () + break } } diff --git a/Sources/NIOTLS/ApplicationProtocolNegotiationHandler.swift b/Sources/NIOTLS/ApplicationProtocolNegotiationHandler.swift index e962a3820..e8b83ce82 100644 --- a/Sources/NIOTLS/ApplicationProtocolNegotiationHandler.swift +++ b/Sources/NIOTLS/ApplicationProtocolNegotiationHandler.swift @@ -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) } } diff --git a/Tests/NIOTests/ChannelPipelineTest.swift b/Tests/NIOTests/ChannelPipelineTest.swift index 7f4d084c0..d2a1a292f 100644 --- a/Tests/NIOTests/ChannelPipelineTest.swift +++ b/Tests/NIOTests/ChannelPipelineTest.swift @@ -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)") } diff --git a/Tests/NIOTests/ChannelTests.swift b/Tests/NIOTests/ChannelTests.swift index 79043af3f..edaab59de 100644 --- a/Tests/NIOTests/ChannelTests.swift +++ b/Tests/NIOTests/ChannelTests.swift @@ -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) diff --git a/Tests/NIOTests/HappyEyeballsTest.swift b/Tests/NIOTests/HappyEyeballsTest.swift index 8d6f5f8fa..81447fbfb 100644 --- a/Tests/NIOTests/HappyEyeballsTest.swift +++ b/Tests/NIOTests/HappyEyeballsTest.swift @@ -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()))") } } diff --git a/Tests/NIOTests/SocketAddressTest.swift b/Tests/NIOTests/SocketAddressTest.swift index 70dbc6aa2..deb7eedae 100644 --- a/Tests/NIOTests/SocketAddressTest.swift +++ b/Tests/NIOTests/SocketAddressTest.swift @@ -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)") } }