From 11377a47bb35fd1546a14666192b40313485bd5e Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Fri, 25 Oct 2019 18:04:37 +0200 Subject: [PATCH] avoid some clippy warnings --- src/api2/node/syslog.rs | 2 +- src/api2/node/tasks.rs | 2 +- src/bin/dynamic-files.rs | 2 +- src/bin/proxmox-backup-client.rs | 44 ++++++++++++++------------------ src/bin/pxar.rs | 8 +++--- src/bin/test_chunk_speed2.rs | 2 +- src/client/http_client.rs | 12 ++++----- 7 files changed, 33 insertions(+), 39 deletions(-) diff --git a/src/api2/node/syslog.rs b/src/api2/node/syslog.rs index 0940a30f..33e64140 100644 --- a/src/api2/node/syslog.rs +++ b/src/api2/node/syslog.rs @@ -43,7 +43,7 @@ fn dump_journal( Ok(line) => { count += 1; if count < start { continue }; - if limit <= 0 { continue }; + if limit == 0 { continue }; lines.push(json!({ "n": count, "t": line })); diff --git a/src/api2/node/tasks.rs b/src/api2/node/tasks.rs index 0b539780..450547d3 100644 --- a/src/api2/node/tasks.rs +++ b/src/api2/node/tasks.rs @@ -75,7 +75,7 @@ fn read_task_log( Ok(line) => { count += 1; if count < start { continue }; - if limit <= 0 { continue }; + if limit == 0 { continue }; lines.push(json!({ "n": count, "t": line })); diff --git a/src/bin/dynamic-files.rs b/src/bin/dynamic-files.rs index 1db16e8f..97ce9531 100644 --- a/src/bin/dynamic-files.rs +++ b/src/bin/dynamic-files.rs @@ -53,7 +53,7 @@ fn main() -> Result<(), Error> { } for h in handles { - if let Err(_) = h.join() { + if h.join().is_err() { bail!("join failed"); } } diff --git a/src/bin/proxmox-backup-client.rs b/src/bin/proxmox-backup-client.rs index ed63c51d..38bdb26e 100644 --- a/src/bin/proxmox-backup-client.rs +++ b/src/bin/proxmox-backup-client.rs @@ -91,7 +91,7 @@ fn record_repository(repo: &BackupRepository) { _ => return, }; - let mut data = file_get_json(&path, None).unwrap_or(json!({})); + let mut data = file_get_json(&path, None).unwrap_or_else(|_| json!({})); let repo = repo.to_string(); @@ -141,7 +141,7 @@ fn complete_repository(_arg: &str, _param: &HashMap) -> Vec return result, }; - let data = file_get_json(&path, None).unwrap_or(json!({})); + let data = file_get_json(&path, None).unwrap_or_else(|_| json!({})); if let Some(map) = data.as_object() { for (repo, _count) in map { @@ -239,14 +239,10 @@ async fn backup_image>( fn strip_server_file_expenstion(name: &str) -> String { - if name.ends_with(".didx") { - return name[..name.len()-5].to_owned(); - } else if name.ends_with(".fidx") { - return name[..name.len()-5].to_owned(); - } else if name.ends_with(".blob") { - return name[..name.len()-5].to_owned(); + if name.ends_with(".didx") || name.ends_with(".fidx") || name.ends_with(".blob") { + name[..name.len()-5].to_owned() } else { - return name.to_owned(); // should not happen + name.to_owned() // should not happen } } @@ -466,7 +462,7 @@ fn dump_catalog( let path = tools::required_string_param(¶m, "snapshot")?; let snapshot = BackupDir::parse(path)?; - let keyfile = param["keyfile"].as_str().map(|p| PathBuf::from(p)); + let keyfile = param["keyfile"].as_str().map(PathBuf::from); let crypt_config = match keyfile { None => None, @@ -612,7 +608,7 @@ fn create_backup( verify_chunk_size(size)?; } - let keyfile = param["keyfile"].as_str().map(|p| PathBuf::from(p)); + let keyfile = param["keyfile"].as_str().map(PathBuf::from); let backup_id = param["backup-id"].as_str().unwrap_or(&proxmox::tools::nodename()); @@ -651,7 +647,7 @@ fn create_backup( let file_type = metadata.file_type(); let extension = target.rsplit('.').next() - .ok_or(format_err!("missing target file extenion '{}'", target))?; + .ok_or_else(|| format_err!("missing target file extenion '{}'", target))?; match extension { "pxar" => { @@ -690,7 +686,7 @@ fn create_backup( } } - let backup_time = Utc.timestamp(backup_time_opt.unwrap_or(Utc::now().timestamp()), 0); + let backup_time = Utc.timestamp(backup_time_opt.unwrap_or_else(|| Utc::now().timestamp()), 0); let client = HttpClient::new(repo.host(), repo.user(), None)?; record_repository(&repo); @@ -947,7 +943,7 @@ async fn restore_do(param: Value) -> Result { }))).await?; let list = result["data"].as_array().unwrap(); - if list.len() == 0 { + if list.is_empty() { bail!("backup group '{}' does not contain any snapshots:", path); } @@ -962,7 +958,7 @@ async fn restore_do(param: Value) -> Result { let target = tools::required_string_param(¶m, "target")?; let target = if target == "-" { None } else { Some(target) }; - let keyfile = param["keyfile"].as_str().map(|p| PathBuf::from(p)); + let keyfile = param["keyfile"].as_str().map(PathBuf::from); let crypt_config = match keyfile { None => None, @@ -1117,7 +1113,7 @@ fn upload_log( let mut client = HttpClient::new(repo.host(), repo.user(), None)?; - let keyfile = param["keyfile"].as_str().map(|p| PathBuf::from(p)); + let keyfile = param["keyfile"].as_str().map(PathBuf::from); let crypt_config = match keyfile { None => None, @@ -1375,7 +1371,7 @@ fn complete_chunk_size(_arg: &str, _param: &HashMap) -> Vec 4096 { break; } } @@ -1615,13 +1611,11 @@ fn key_mgmt_cli() -> CliCommandMap { .arg_param(vec!["path"]) .completion_cb("path", tools::complete_file_name); - let cmd_def = CliCommandMap::new() + CliCommandMap::new() .insert("create".to_owned(), key_create_cmd_def.into()) .insert("create-master-key".to_owned(), key_create_master_key_cmd_def.into()) .insert("import-master-pubkey".to_owned(), key_import_master_pubkey_cmd_def.into()) - .insert("change-passphrase".to_owned(), key_change_passphrase_cmd_def.into()); - - cmd_def + .insert("change-passphrase".to_owned(), key_change_passphrase_cmd_def.into()) } @@ -1641,7 +1635,7 @@ fn mount( // Make sure to fork before the async runtime is instantiated to avoid troubles. let pipe = pipe()?; match fork() { - Ok(ForkResult::Parent { child: _, .. }) => { + Ok(ForkResult::Parent { .. }) => { nix::unistd::close(pipe.1).unwrap(); // Blocks the parent process until we are ready to go in the child let _res = nix::unistd::read(pipe.0, &mut [0]).unwrap(); @@ -1675,7 +1669,7 @@ async fn mount_do(param: Value, pipe: Option) -> Result { }))).await?; let list = result["data"].as_array().unwrap(); - if list.len() == 0 { + if list.is_empty() { bail!("backup group '{}' does not contain any snapshots:", path); } @@ -1687,7 +1681,7 @@ async fn mount_do(param: Value, pipe: Option) -> Result { (snapshot.group().backup_type().to_owned(), snapshot.group().backup_id().to_owned(), snapshot.backup_time()) }; - let keyfile = param["keyfile"].as_str().map(|p| PathBuf::from(p)); + let keyfile = param["keyfile"].as_str().map(PathBuf::from); let crypt_config = match keyfile { None => None, Some(path) => { @@ -1762,7 +1756,7 @@ async fn mount_do(param: Value, pipe: Option) -> Result { } // Signal the parent process that we are done with the setup and it can // terminate. - nix::unistd::write(pipe, &mut [0u8])?; + nix::unistd::write(pipe, &[0u8])?; nix::unistd::close(pipe).unwrap(); } diff --git a/src/bin/pxar.rs b/src/bin/pxar.rs index 8e04adb8..5e77b69c 100644 --- a/src/bin/pxar.rs +++ b/src/bin/pxar.rs @@ -77,7 +77,7 @@ fn extract_archive_from_reader( }); decoder.set_allow_existing_dirs(allow_existing_dirs); - let pattern = pattern.unwrap_or(Vec::new()); + let pattern = pattern.unwrap_or_else(Vec::new); decoder.restore(Path::new(target), &pattern)?; Ok(()) @@ -138,10 +138,10 @@ fn extract_archive( pattern_list.push(p); } - let pattern = if pattern_list.len() > 0 { - Some(pattern_list) - } else { + let pattern = if pattern_list.is_empty() { None + } else { + Some(pattern_list) }; if archive == "-" { diff --git a/src/bin/test_chunk_speed2.rs b/src/bin/test_chunk_speed2.rs index 28de6a95..3fe7024a 100644 --- a/src/bin/test_chunk_speed2.rs +++ b/src/bin/test_chunk_speed2.rs @@ -45,7 +45,7 @@ async fn run() -> Result<(), Error> { println!("Got chunk {}", chunk.len()); } - let speed = ((stream_len*1000000)/(1024*1024))/(start_time.elapsed().as_micros() as usize); + let speed = ((stream_len*1_000_000)/(1024*1024))/(start_time.elapsed().as_micros() as usize); println!("Uploaded {} chunks in {} seconds ({} MB/s).", repeat, start_time.elapsed().as_secs(), speed); println!("Average chunk size was {} bytes.", stream_len/repeat); println!("time per request: {} microseconds.", (start_time.elapsed().as_micros())/(repeat as u128)); diff --git a/src/client/http_client.rs b/src/client/http_client.rs index a480d6d5..1ff1994c 100644 --- a/src/client/http_client.rs +++ b/src/client/http_client.rs @@ -365,11 +365,11 @@ impl HttpClient { let text = String::from_utf8(data.to_vec()).unwrap(); if status.is_success() { - if text.len() > 0 { + if text.is_empty() { + Ok(Value::Null) + } else { let value: Value = serde_json::from_str(&text)?; Ok(value) - } else { - Ok(Value::Null) } } else { bail!("HTTP Error {}: {}", status, text); @@ -578,7 +578,9 @@ impl H2Client { let text = String::from_utf8(data.to_vec()).unwrap(); if status.is_success() { - if text.len() > 0 { + if text.is_empty() { + Ok(Value::Null) + } else { let mut value: Value = serde_json::from_str(&text)?; if let Some(map) = value.as_object_mut() { if let Some(data) = map.remove("data") { @@ -586,8 +588,6 @@ impl H2Client { } } bail!("got result without data property"); - } else { - Ok(Value::Null) } } else { bail!("HTTP Error {}: {}", status, text);