Only allocate error message if the error occurred

Suggested by clippy:

warning: use of `ok_or` followed by a function call
  --> src/handshake/server.rs:20:19
   |
20 |           let key = self.headers.find_first("Sec-WebSocket-Key")
   |  ___________________^ starting here...
21 | |             .ok_or(Error::Protocol("Missing Sec-WebSocket-Key".into()))?;
   | |_______________________________________________________________________^ ...ending here
   |
   = note: #[warn(or_fun_call)] on by default
help: try this
   |         let key = self.headers.find_first("Sec-WebSocket-Key").ok_or_else(|| Error::Protocol("Missing Sec-WebSocket-Key".into()))?;
   = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#or_fun_call
pull/7/head
Ran Benita 8 years ago
parent 3c600aa138
commit 76e80ca9a3
  1. 2
      src/client.rs
  2. 2
      src/handshake/server.rs

@ -73,7 +73,7 @@ fn wrap_stream(stream: TcpStream, _domain: &str, mode: Mode) -> Result<AutoStrea
fn connect_to_some<A>(addrs: A, url: &Url, mode: Mode) -> Result<AutoStream> fn connect_to_some<A>(addrs: A, url: &Url, mode: Mode) -> Result<AutoStream>
where A: Iterator<Item=SocketAddr> where A: Iterator<Item=SocketAddr>
{ {
let domain = url.host_str().ok_or(Error::Url("No host name in the URL".into()))?; let domain = url.host_str().ok_or_else(|| Error::Url("No host name in the URL".into()))?;
for addr in addrs { for addr in addrs {
debug!("Trying to contact {} at {}...", url, addr); debug!("Trying to contact {} at {}...", url, addr);
if let Ok(raw_stream) = TcpStream::connect(addr) { if let Ok(raw_stream) = TcpStream::connect(addr) {

@ -18,7 +18,7 @@ impl Request {
/// Reply to the response. /// Reply to the response.
pub fn reply(&self) -> Result<Vec<u8>> { pub fn reply(&self) -> Result<Vec<u8>> {
let key = self.headers.find_first("Sec-WebSocket-Key") let key = self.headers.find_first("Sec-WebSocket-Key")
.ok_or(Error::Protocol("Missing Sec-WebSocket-Key".into()))?; .ok_or_else(|| Error::Protocol("Missing Sec-WebSocket-Key".into()))?;
let reply = format!("\ let reply = format!("\
HTTP/1.1 101 Switching Protocols\r\n\ HTTP/1.1 101 Switching Protocols\r\n\
Connection: Upgrade\r\n\ Connection: Upgrade\r\n\

Loading…
Cancel
Save