Repository: vapor/vapor
File: Sources/Vapor/HTTP/Server/HTTPServer.swift (Swift)

Code before:
        
        switch address {
        case .none: 
            /// Use the configuration as is.
            break
        case .hostname(let hostname, let port): 
            /// Override the hostname, port, neither, or both.
            configuration.address = .hostname(hostname ?? configuration.hostname, port: port ?? configuration.port)
        case .unixDomainSocket: 
            /// Override the socket path.
            configuration.address = address!
        }
        
        /// Start the actual `HTTPServer`.
        try self.connection.withLockedValue {
            $0 = try HTTPServerConnection.start(
                application: self.application,
                server: self,
                responder: self.responder,
                configuration: configuration,
                on: self.eventLoopGroup
            ).wait()
        }

        /// Print starting message.
        configuration.logger.notice("Server started on \(self.localAddress!.description)")

        self.configuration = configuration
        self.didStart.withLockedValue { $0 = true }
    }
    
    public func shutdown() {
        guard let connection = self.connection.withLockedValue({ $0 }) else {
            return
        }
        self.configuration.logger.debug("Requesting HTTP server shutdown")
        do {
            try connection.close(timeout: self.configuration.shutdownTimeout).wait()
        } catch {
            self.configuration.logger.error("Could not stop HTTP server: \(error)")
        }
        self.configuration.logger.debug("HTTP server shutting down")
        self.didShutdown.withLockedValue { $0 = true }
    }

    public var localAddress: SocketAddress? {
        return self.connection.withLockedValue({ $0 })?.channel.localAddress
    }
    
    deinit {
        let started = self.didStart.withLockedValue { $0 }

Review comment (suggestion):
As a general rule force unwrapping should be avoided. What I'd do is just move the whole block down and use the port from the local address and fall back to the existing behaviour if it's nil

Code after:
        self.application = application
        self.responder = responder
        self._configuration = .init(configuration)
        self.eventLoopGroup = eventLoopGroup
        self.didStart = .init(false)
        self.didShutdown = .init(false)
        self.connection = .init(nil)
    }
    
    public func start(address: BindAddress?) throws {
        var configuration = self.configuration
        
        switch address {
        case .none: 
            /// Use the configuration as is.
            break
        case .hostname(let hostname, let port): 
            /// Override the hostname, port, neither, or both.
            configuration.address = .hostname(hostname ?? configuration.hostname, port: port ?? configuration.port)
        case .unixDomainSocket: 
            /// Override the socket path.
            configuration.address = address!
        }

        /// Log starting message for debugging before attempting to start the server.
        configuration.logger.debug("Server starting on \(configuration.addressDescription)")
        
        /// Start the actual `HTTPServer`.
        try self.connection.withLockedValue {
            $0 = try HTTPServerConnection.start(
                application: self.application,
                server: self,
                responder: self.responder,
                configuration: configuration,
                on: self.eventLoopGroup
            ).wait()
        }

        /// Overwrite configuration with actual address, if applicable.
        /// They may differ from the provided configuation if port 0 was provided, for example.
        if let localAddress = self.localAddress {
            if let hostname = localAddress.hostname, let port = localAddress.port {
                configuration.address = .hostname(hostname, port: port)
            } else if let pathname = localAddress.pathname {
                configuration.address = .unixDomainSocket(path: pathname)
            }
        }

        /// Log started message with the actual configuration.
        configuration.logger.notice("Server started on \(configuration.addressDescription)")