From 7cd31bd425efc599f1c54534cc2ad109dd07d14d Mon Sep 17 00:00:00 2001 From: Astro Date: Fri, 21 Jul 2017 00:19:08 +0200 Subject: [PATCH] delint (clippy) --- examples/echo_bot.rs | 23 +++++++++++------------ src/client/auth.rs | 9 +++------ src/client/bind.rs | 13 +++++-------- src/client/event.rs | 12 ++++++------ src/client/mod.rs | 9 +++------ src/happy_eyeballs.rs | 12 ++++-------- src/stream_start.rs | 4 +--- src/xmpp_codec.rs | 37 ++++++++++++++++++++++--------------- 8 files changed, 55 insertions(+), 64 deletions(-) diff --git a/examples/echo_bot.rs b/examples/echo_bot.rs index 2307cdf643416b46a20b3ee50d7eaf3f64bdba05..40b20ac95fb86f3fed6afd51a65a761b37f7fb24 100644 --- a/examples/echo_bot.rs +++ b/examples/echo_bot.rs @@ -54,18 +54,17 @@ fn main() { let presence = make_presence(); send(presence); - } else if let Some(stanza) = event.into_stanza() { - if let Ok(message) = Message::try_from(stanza) { - if message.type_ != MessageType::Error { - // This is a message we'll echo - match (message.from, message.bodies.get("")) { - (Some(from), Some(body)) => { - let reply = make_reply(from, body); - send(reply); - }, - _ => (), - }; - } + } else if let Some(message) = event.into_stanza() + .and_then(|stanza| Message::try_from(stanza).ok()) + { + // This is a message we'll echo + match (message.from, message.bodies.get("")) { + (Some(from), Some(body)) => + if message.type_ != MessageType::Error { + let reply = make_reply(from, body); + send(reply); + }, + _ => (), } } diff --git a/src/client/auth.rs b/src/client/auth.rs index dce2f86c831c38103f007c88c7dcb00730f0cec7..05006fdd5053fe7b767a448c08275b61ba7c48f1 100644 --- a/src/client/auth.rs +++ b/src/client/auth.rs @@ -130,12 +130,9 @@ impl Future for ClientAuth { if stanza.name() == "failure" && stanza.ns() == Some(NS_XMPP_SASL) => { - let mut e = None; - for child in stanza.children() { - e = Some(child.name().clone()); - break - } - let e = e.unwrap_or_else(|| "Authentication failure"); + let e = stanza.children().next() + .map(|child| child.name()) + .unwrap_or("Authentication failure"); Err(e.to_owned()) }, Ok(Async::Ready(event)) => { diff --git a/src/client/bind.rs b/src/client/bind.rs index 7800bbcca160a3acec647fa8cd9a64f20dee4dde..1541afd3543c4284cae66c45405d83869243c650 100644 --- a/src/client/bind.rs +++ b/src/client/bind.rs @@ -44,13 +44,10 @@ fn make_bind_request(resource: Option<&String>) -> Element { .attr("id", BIND_REQ_ID); let mut bind_el = Element::builder("bind") .ns(NS_XMPP_BIND); - match resource { - Some(resource) => { - let resource_el = Element::builder("resource") - .append(resource); - bind_el = bind_el.append(resource_el.build()); - }, - None => (), + if let Some(resource) = resource { + let resource_el = Element::builder("resource") + .append(resource); + bind_el = bind_el.append(resource_el.build()); } iq.append(bind_el.build()) .build() @@ -87,7 +84,7 @@ impl Future for ClientBind { && iq.attr("id") == Some(BIND_REQ_ID) => { match iq.attr("type") { Some("result") => { - get_bind_response_jid(&iq) + get_bind_response_jid(iq) .map(|jid| stream.jid = jid); Ok(Async::Ready(stream)) }, diff --git a/src/client/event.rs b/src/client/event.rs index cae6ceaafa8bef99dbacd39db3bae778d126184c..8ff44bd70a84ded7200eace6cea4055015a7e29a 100644 --- a/src/client/event.rs +++ b/src/client/event.rs @@ -9,22 +9,22 @@ pub enum Event { impl Event { pub fn is_online(&self) -> bool { - match self { - &Event::Online => true, + match *self { + Event::Online => true, _ => false, } } pub fn is_stanza(&self, name: &str) -> bool { - match self { - &Event::Stanza(ref stanza) => stanza.name() == name, + match *self { + Event::Stanza(ref stanza) => stanza.name() == name, _ => false, } } pub fn as_stanza(&self) -> Option<&Element> { - match self { - &Event::Stanza(ref stanza) => Some(stanza), + match *self { + Event::Stanza(ref stanza) => Some(stanza), _ => None, } } diff --git a/src/client/mod.rs b/src/client/mod.rs index aca0fdd20294cc45324a2492361eac8a12062a5b..b5d7e8fcef6b52554faff5f6c1f702471f169ca7 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -130,10 +130,6 @@ impl Stream for Client { }, ClientState::Connected(mut stream) => { match stream.poll() { - Ok(Async::NotReady) => { - self.state = ClientState::Connected(stream); - Ok(Async::NotReady) - }, Ok(Async::Ready(None)) => { // EOF self.state = ClientState::Disconnected; @@ -143,6 +139,7 @@ impl Stream for Client { self.state = ClientState::Connected(stream); Ok(Async::Ready(Some(ClientEvent::Stanza(stanza)))) }, + Ok(Async::NotReady) | Ok(Async::Ready(_)) => { self.state = ClientState::Connected(stream); Ok(Async::NotReady) @@ -179,8 +176,8 @@ impl Sink for Client { } fn poll_complete(&mut self) -> Poll<(), Self::SinkError> { - match &mut self.state { - &mut ClientState::Connected(ref mut stream) => + match self.state { + ClientState::Connected(ref mut stream) => stream.poll_complete() .map_err(|e| e.description().to_owned()), _ => diff --git a/src/happy_eyeballs.rs b/src/happy_eyeballs.rs index 3cd42f4e615d0ad47514b4e93142266ef759f844..806472c5d602b4b32e082841a997f7b8634cc0ed 100644 --- a/src/happy_eyeballs.rs +++ b/src/happy_eyeballs.rs @@ -46,8 +46,7 @@ impl Future for Connecter { fn poll(&mut self) -> Poll { match self.lookup.as_mut().map(|mut lookup| lookup.poll()) { - None => (), - Some(Ok(Async::NotReady)) => (), + None | Some(Ok(Async::NotReady)) => (), Some(Ok(Async::Ready(found_srvs))) => { self.lookup = None; match found_srvs { @@ -62,8 +61,7 @@ impl Future for Connecter { } match self.srvs.as_mut().map(|mut srv| srv.poll()) { - None => (), - Some(Ok(Async::NotReady)) => (), + None | Some(Ok(Async::NotReady)) => (), Some(Ok(Async::Ready(None))) => self.srvs = None, Some(Ok(Async::Ready(Some(srv_item)))) => { @@ -99,10 +97,8 @@ impl Future for Connecter { }, } }); - match connected_stream { - Some(tcp_stream) => - return Ok(Async::Ready(tcp_stream)), - None => (), + if let Some(tcp_stream) = connected_stream { + return Ok(Async::Ready(tcp_stream)); } if self.lookup.is_none() && diff --git a/src/stream_start.rs b/src/stream_start.rs index 4d79d4d9286cd99d74c25f36846df4bae2806734..b97e87d99d80ecbc734a1c848ab033180d2180a8 100644 --- a/src/stream_start.rs +++ b/src/stream_start.rs @@ -89,9 +89,7 @@ impl Future for StreamStart { } else { (StreamStartState::RecvFeatures(stream, stream_ns), Ok(Async::NotReady)) }, - Ok(Async::Ready(_)) => - (StreamStartState::RecvFeatures(stream, stream_ns), Ok(Async::NotReady)), - Ok(Async::NotReady) => + Ok(Async::Ready(_)) | Ok(Async::NotReady) => (StreamStartState::RecvFeatures(stream, stream_ns), Ok(Async::NotReady)), Err(e) => return Err(e), diff --git a/src/xmpp_codec.rs b/src/xmpp_codec.rs index 6686ed2b6c9b23b8a0f64f462e602ac63b07bc44..bb725835d57949a97e1be48cf82a8ff9dfc21994 100644 --- a/src/xmpp_codec.rs +++ b/src/xmpp_codec.rs @@ -48,9 +48,8 @@ impl ParserSink { fn lookup_ns(&self, prefix: &Option) -> Option<&str> { for nss in self.ns_stack.iter().rev() { - match nss.get(prefix) { - Some(ns) => return Some(ns), - None => (), + if let Some(ns) = nss.get(prefix) { + return Some(ns); } } @@ -77,9 +76,11 @@ impl ParserSink { let el = { let mut el_builder = Element::builder(tag.name.local.as_ref()); - match self.lookup_ns(&tag.name.prefix.map(|prefix| prefix.as_ref().to_owned())) { - Some(el_ns) => el_builder = el_builder.ns(el_ns), - None => (), + if let Some(el_ns) = self.lookup_ns( + &tag.name.prefix.map(|prefix| + prefix.as_ref().to_owned()) + ) { + el_builder = el_builder.ns(el_ns); } for attr in &tag.attrs { match attr.name.local.as_ref() { @@ -178,7 +179,7 @@ pub struct XMPPCodec { impl XMPPCodec { pub fn new() -> Self { - let queue = Rc::new(RefCell::new((VecDeque::new()))); + let queue = Rc::new(RefCell::new(VecDeque::new())); let sink = ParserSink::new(queue.clone()); // TODO: configure parser? let parser = XmlTokenizer::new(sink, Default::default()); @@ -191,13 +192,19 @@ impl XMPPCodec { } } +impl Default for XMPPCodec { + fn default() -> Self { + Self::new() + } +} + impl Decoder for XMPPCodec { type Item = Packet; type Error = Error; fn decode(&mut self, buf: &mut BytesMut) -> Result, Self::Error> { let buf1: Box> = - if self.buf.len() > 0 && buf.len() > 0 { + if ! self.buf.is_empty() && ! buf.is_empty() { let mut prefix = std::mem::replace(&mut self.buf, vec![]); prefix.extend_from_slice(buf.take().as_ref()); Box::new(prefix) @@ -207,7 +214,7 @@ impl Decoder for XMPPCodec { let buf1 = buf1.as_ref().as_ref(); match from_utf8(buf1) { Ok(s) => { - if s.len() > 0 { + if ! s.is_empty() { println!("<< {}", s); let tendril = FromIterator::from_iter(s.chars()); self.parser.feed(tendril); @@ -257,7 +264,7 @@ impl Encoder for XMPPCodec { Packet::StreamStart(start_attrs) => { let mut buf = String::new(); write!(buf, "(el: &Element, writer: &mut W, parent_ns: Option<& write!(writer, "<")?; write!(writer, "{}", el.name())?; - if let Some(ref ns) = el.ns() { + if let Some(ns) = el.ns() { if parent_ns.map(|s| s.as_ref()) != el.ns() { write!(writer, " xmlns=\"{}\"", ns)?; } @@ -320,10 +327,10 @@ pub fn write_element(el: &Element, writer: &mut W, parent_ns: Option<& write!(writer, ">")?; for node in el.nodes() { - match node { - &Node::Element(ref child) => + match *node { + Node::Element(ref child) => write_element(child, writer, el.ns())?, - &Node::Text(ref text) => + Node::Text(ref text) => write_text(text, writer)?, } } @@ -332,7 +339,7 @@ pub fn write_element(el: &Element, writer: &mut W, parent_ns: Option<& Ok(()) } -/// Copied from RustyXML for now +/// Copied from `RustyXML` for now pub fn escape(input: &str) -> String { let mut result = String::with_capacity(input.len());