From de4d37acfc43323aa1d4c97cacc8021835ac645a Mon Sep 17 00:00:00 2001 From: Luca Bruno Date: Sun, 14 Oct 2018 20:44:35 +0000 Subject: [PATCH] dkregistry: fix all clippy warnings This fixes all warnings currently reported by clippy. --- examples/image.rs | 2 +- examples/login.rs | 2 +- examples/tags.rs | 2 +- examples/trace.rs | 2 +- src/reference.rs | 32 ++++++++++++++++---------------- src/render.rs | 2 +- src/v2/auth.rs | 6 +++--- src/v2/config.rs | 12 ++++++++---- 8 files changed, 32 insertions(+), 28 deletions(-) diff --git a/examples/image.rs b/examples/image.rs index 62470ff8..068f9514 100644 --- a/examples/image.rs +++ b/examples/image.rs @@ -71,7 +71,7 @@ fn run(dkr_ref: &reference::Reference, user: Option, passwd: Option, passwd: Option) -> Result<()> { return Err("no login performed, but already authenticated".into()); } - let fut_token = try!(dclient.login(vec![])); + let fut_token = try!(dclient.login(&[])); let token = try!(tcore.run(fut_token)); let futauth = try!(dclient.is_auth(Some(token.token()))); diff --git a/examples/tags.rs b/examples/tags.rs index 2b5634b5..f3b9f57e 100644 --- a/examples/tags.rs +++ b/examples/tags.rs @@ -54,7 +54,7 @@ fn run(host: &str, user: Option, passwd: Option, image: &str) -> return Err("API v2 not supported".into()); } - let fut_token = try!(dclient.login(vec![&format!("repository:{}:pull", image)])); + let fut_token = try!(dclient.login(&[&format!("repository:{}:pull", image)])); let token_auth = try!(tcore.run(fut_token)); let futauth = try!(dclient.is_auth(Some(token_auth.token()))); diff --git a/examples/trace.rs b/examples/trace.rs index 824d9077..9452ea94 100644 --- a/examples/trace.rs +++ b/examples/trace.rs @@ -77,7 +77,7 @@ fn run(dkr_ref: &reference::Reference, user: Option, passwd: Option Result<(), fmt::Error> { - let v = match self { - &Version::Tag(ref s) => ":".to_string() + s, - &Version::Digest(ref t, ref d) => "@".to_string() + t + ":" + d, + let v = match *self { + Version::Tag(ref s) => ":".to_string() + s, + Version::Digest(ref t, ref d) => "@".to_string() + t + ":" + d, }; write!(f, "{}", v) } @@ -76,9 +76,9 @@ impl fmt::Debug for Version { impl fmt::Display for Version { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - let v = match self { - &Version::Tag(ref s) => s.to_string(), - &Version::Digest(ref t, ref d) => t.to_string() + ":" + d, + let v = match *self { + Version::Tag(ref s) => s.to_string(), + Version::Digest(ref t, ref d) => t.to_string() + ":" + d, }; write!(f, "{}", v) } @@ -96,13 +96,13 @@ pub struct Reference { impl Reference { pub fn new(registry: Option, repository: String, version: Option) -> Self { - let reg = registry.unwrap_or("registry-1.docker.io".to_string()); - let ver = version.unwrap_or(Version::Tag("latest".to_string())); + let reg = registry.unwrap_or_else(|| "registry-1.docker.io".to_string()); + let ver = version.unwrap_or_else(|| Version::Tag("latest".to_string())); Self { has_schema: false, raw_input: "".into(), registry: reg, - repository: repository, + repository, version: ver, } } @@ -152,19 +152,19 @@ fn parse_url(s: &str) -> Result { if has_schema { rest = s.trim_left_matches("docker://"); }; - let (rest, ver) = match (rest.rfind('@'), rest.rfind(':')) { + let (rest, version) = match (rest.rfind('@'), rest.rfind(':')) { (Some(i), _) | (None, Some(i)) => { let s = rest.split_at(i); (s.0, Version::from_str(s.1)?) } (None, None) => (rest, Version::default()), }; - if rest.len() < 1 { + if rest.is_empty() { bail!("name too short"); } let mut reg = "registry-1.docker.io"; let split: Vec<&str> = rest.rsplitn(3, '/').collect(); - let image = match split.len() { + let repository = match split.len() { 1 => "library/".to_string() + rest, 2 => rest.to_string(), _ => { @@ -172,14 +172,14 @@ fn parse_url(s: &str) -> Result { split[1].to_string() + "/" + split[0] } }; - if image.len() > 127 { + if repository.len() > 127 { bail!("name too long"); } Ok(Reference { - has_schema: has_schema, + has_schema, raw_input: s.to_string(), registry: reg.to_string(), - repository: image, - version: ver, + repository, + version, }) } diff --git a/src/render.rs b/src/render.rs index 5ea68806..6a795e16 100644 --- a/src/render.rs +++ b/src/render.rs @@ -30,7 +30,7 @@ pub fn unpack(layers: &[Vec], target_dir: &path::Path) -> Result<()> { for entry in archive.entries()? { let file = entry?; let path = file.path()?; - let parent = path.parent().unwrap_or(path::Path::new("/")); + let parent = path.parent().unwrap_or_else(|| path::Path::new("/")); if let Some(fname) = path.file_name() { let wh_name = fname.to_string_lossy(); if wh_name == ".wh..wh..opq" { diff --git a/src/v2/auth.rs b/src/v2/auth.rs index b1afa154..82af8113 100644 --- a/src/v2/auth.rs +++ b/src/v2/auth.rs @@ -35,7 +35,7 @@ impl Client { let a = r .headers() .get(hyper::header::WWW_AUTHENTICATE) - .ok_or(Error::from("get_token: missing Auth header"))?; + .ok_or_else(|| Error::from("get_token: missing Auth header"))?; let chal = String::from_utf8(a.as_bytes().to_vec())?; Ok(chal) }).and_then(move |hdr| { @@ -45,7 +45,7 @@ impl Client { let kv: Vec<&str> = item.split('=').collect(); match (kv.get(0), kv.get(1)) { (Some(&"realm"), Some(v)) => auth_ep = v.trim_matches('"').to_owned(), - (Some(&"service"), Some(v)) => service = Some(v.trim_matches('"').clone()), + (Some(&"service"), Some(v)) => service = Some(v.trim_matches('"')), (Some(&"scope"), _) => {} (_, _) => return Err("unsupported key".to_owned().into()), }; @@ -71,7 +71,7 @@ impl Client { /// Perform registry authentication and return an authenticated token. /// /// On success, the returned token will be valid for all requested scopes. - pub fn login(&self, scopes: Vec<&str>) -> Result { + pub fn login(&self, scopes: &[&str]) -> Result { let subclient = self.hclient.clone(); let creds = self.credentials.clone(); let scope = scopes diff --git a/src/v2/config.rs b/src/v2/config.rs index 0ff52975..051f439d 100644 --- a/src/v2/config.rs +++ b/src/v2/config.rs @@ -70,9 +70,10 @@ impl Config { /// Return a `Client` to interact with a v2 registry. pub fn build(self) -> Result { - let base = match self.insecure_registry { - false => "https://".to_string() + &self.index, - true => "http://".to_string() + &self.index, + let base = if self.insecure_registry { + "http://".to_string() + &self.index + } else { + "https://".to_string() + &self.index }; trace!( "Built client for {:?}: endpoint {:?} - user {:?}", @@ -82,7 +83,10 @@ impl Config { ); let creds = match (self.username, self.password) { (None, None) => None, - (u, p) => Some((u.unwrap_or("".into()), p.unwrap_or("".into()))), + (u, p) => Some(( + u.unwrap_or_else(|| "".into()), + p.unwrap_or_else(|| "".into()), + )), }; let c = Client { base_url: base,