From 6ec46827e7719da88c5dfda67b138bf93bdadd47 Mon Sep 17 00:00:00 2001 From: Sergiu Puscas Date: Mon, 5 Apr 2021 18:41:17 +0200 Subject: [PATCH] Fix clippy warnings. --- src/rust/api/client.rs | 29 +++++++++++++---------------- src/rust/cli/client.rs | 31 ++++++++++++++----------------- 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/src/rust/api/client.rs b/src/rust/api/client.rs index ee7cb3ee1e..625183454b 100644 --- a/src/rust/api/client.rs +++ b/src/rust/api/client.rs @@ -1,4 +1,3 @@ -use std; use std::error; use std::fmt::{self, Display}; use std::io::{self, Cursor, Read, Seek, SeekFrom, Write}; @@ -9,18 +8,16 @@ use std::time::Duration; use itertools::Itertools; -use hyper; use hyper::header::{HeaderMap, AUTHORIZATION, CONTENT_LENGTH, CONTENT_TYPE, USER_AGENT}; use hyper::Method; use hyper::StatusCode; use hyper::body::Buf; use mime::{Attr, Mime, SubLevel, TopLevel, Value}; -use oauth2; use serde_json as json; -const LINE_ENDING: &'static str = "\r\n"; +const LINE_ENDING: &str = "\r\n"; pub enum Retry { /// Signal you don't want to retry @@ -306,9 +303,9 @@ impl Display for Error { err.domain, err.message, err.reason, - match &err.location { - &Some(ref loc) => format!("@{}", loc), - &None => String::new(), + match err.location { + Some(ref loc) => format!("@{}", loc), + None => String::new(), } )?; } @@ -356,7 +353,7 @@ pub struct MethodInfo { pub http_method: Method, } -const BOUNDARY: &'static str = "MDuXWGyeE33QFXGchb2VFWc4Z7945d"; +const BOUNDARY: &str = "MDuXWGyeE33QFXGchb2VFWc4Z7945d"; /// Provides a `Read` interface that converts multiple parts into the protocol /// identified by [RFC2387](https://tools.ietf.org/html/rfc2387). @@ -417,14 +414,14 @@ impl<'a> MultiPartReader<'a> { /// Returns true if we are totally used fn is_depleted(&self) -> bool { - self.raw_parts.len() == 0 + self.raw_parts.is_empty() && self.current_part.is_none() && self.last_part_boundary.is_none() } /// Returns true if we are handling our last part fn is_last_part(&self) -> bool { - self.raw_parts.len() == 0 && self.current_part.is_some() + self.raw_parts.is_empty() && self.current_part.is_some() } } @@ -519,12 +516,12 @@ pub struct XUploadContentType(pub Mime); impl ::std::ops::Deref for XUploadContentType { type Target = Mime; - fn deref<'a>(&'a self) -> &'a Mime { + fn deref(&self) -> &Mime { &self.0 } } impl ::std::ops::DerefMut for XUploadContentType { - fn deref_mut<'a>(&'a mut self) -> &'a mut Mime { + fn deref_mut(&mut self) -> &mut Mime { &mut self.0 } } @@ -594,11 +591,11 @@ pub struct RangeResponseHeader(pub Chunk); impl RangeResponseHeader { fn from_bytes(raw: &[u8]) -> Self { - if raw.len() > 0 { + if !raw.is_empty() { if let Ok(s) = std::str::from_utf8(raw) { - const PREFIX: &'static str = "bytes "; - if s.starts_with(PREFIX) { - if let Ok(c) = ::from_str(&s[PREFIX.len()..]) { + const PREFIX: &str = "bytes "; + if let Some(stripped) = s.strip_prefix(PREFIX) { + if let Ok(c) = ::from_str(&stripped) { return RangeResponseHeader(c); } } diff --git a/src/rust/cli/client.rs b/src/rust/cli/client.rs index f6dce598ba..109de6b2b0 100644 --- a/src/rust/cli/client.rs +++ b/src/rust/cli/client.rs @@ -3,7 +3,6 @@ use mime::Mime; use oauth2::{ApplicationSecret, ConsoleApplicationSecret}; use serde_json as json; use serde_json::value::Value; -use strsim; use std::env; use std::error::Error as StdError; @@ -141,7 +140,7 @@ impl From<&'static str> for FieldCursor { } } -fn assure_entry<'a, 'b>(m: &'a mut json::Map, k: &'b String) -> &'a mut Value { +fn assure_entry<'a>(m: &'a mut json::Map, k: &str) -> &'a mut Value { if m.contains_key(k) { return m.get_mut(k).expect("value to exist"); } @@ -151,7 +150,7 @@ fn assure_entry<'a, 'b>(m: &'a mut json::Map, k: &'b String) -> & impl FieldCursor { pub fn set(&mut self, value: &str) -> Result<(), CLIError> { - if value.len() == 0 { + if value.is_empty() { return Err(CLIError::Field(FieldError::Empty)); } @@ -164,7 +163,7 @@ impl FieldCursor { let mut fields = self.0.clone(); let push_field = |fs: &mut Vec, f: &mut String| { - if f.len() > 0 { + if !f.is_empty() { fs.push(f.clone()); f.truncate(0); } @@ -187,10 +186,8 @@ impl FieldCursor { } } else { num_conscutive_field_seps = 0; - if cid == 1 { - if first_is_field_sep { - fields.truncate(0); - } + if cid == 1 && first_is_field_sep { + fields.truncate(0); } field.push(c); } @@ -214,7 +211,7 @@ impl FieldCursor { } pub fn did_you_mean(value: &str, possible_values: &[&str]) -> Option { - if value.len() == 0 { + if value.is_empty() { return None; } @@ -224,7 +221,7 @@ impl FieldCursor { let mut output = String::new(); let push_field = |fs: &mut String, f: &mut String| { - if f.len() > 0 { + if !f.is_empty() { fs.push_str(match did_you_mean(&f, possible_values) { Some(candidate) => candidate, None => &f, @@ -248,7 +245,7 @@ impl FieldCursor { push_field(&mut output, &mut field); - if &output == value { + if output == value { None } else { Some(output) @@ -263,7 +260,7 @@ impl FieldCursor { err: &mut InvalidOptionsError, orig_cursor: &FieldCursor, ) { - assert!(self.0.len() > 0); + assert!(!self.0.is_empty()); for field in &self.0[..self.0.len() - 1] { let tmp = object; @@ -353,7 +350,7 @@ pub fn parse_kv_arg<'a>( match kv.find('=') { None => { add_err(); - return (kv, None); + (kv, None) } Some(pos) => { let key = &kv[..pos]; @@ -598,7 +595,7 @@ impl fmt::Display for CLIError { Some(v) => format!(" Did you mean '{}' ?", v), None => String::new(), }; - write!(f, "Parameter '{}' is unknown.{}\n", param_name, suffix) + writeln!(f, "Parameter '{}' is unknown.{}", param_name, suffix) } CLIError::InvalidKeyValueSyntax(ref kv, is_hashmap) => { let hashmap_info = if is_hashmap { "hashmap " } else { "" }; @@ -637,7 +634,7 @@ impl InvalidOptionsError { pub fn single(err: CLIError, exit_code: i32) -> InvalidOptionsError { InvalidOptionsError { issues: vec![err], - exit_code: exit_code, + exit_code, } } @@ -651,12 +648,12 @@ impl InvalidOptionsError { pub fn assure_config_dir_exists(dir: &str) -> Result { let trdir = dir.trim(); - if trdir.len() == 0 { + if trdir.is_empty() { return Err(CLIError::Configuration(ConfigurationError::DirectoryUnset)); } let expanded_config_dir = if trdir.as_bytes()[0] == b'~' { - match env::var("HOME").ok().or(env::var("UserProfile").ok()) { + match env::var("HOME").ok().or_else(|| env::var("UserProfile").ok()) { None => { return Err(CLIError::Configuration( ConfigurationError::HomeExpansionFailed(trdir.to_string()),