From ee1a7cd68b16614ce254f9bdbdbd0dd00219a8e6 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Thu, 5 Dec 2019 12:28:05 +0100 Subject: [PATCH] more clippy fixups Signed-off-by: Wolfgang Bumiller --- proxmox-api/src/cli.rs | 5 ++- proxmox-api/src/cli/command.rs | 6 ++-- proxmox-api/src/cli/completion.rs | 27 ++++++++-------- proxmox-api/src/cli/environment.rs | 6 ++-- proxmox-api/src/cli/format.rs | 18 +++++++---- proxmox-api/src/cli/getopts.rs | 50 ++++++++++++++---------------- proxmox-api/src/cli/readline.rs | 2 +- proxmox-api/src/cli/shellword.rs | 2 +- proxmox-api/src/format.rs | 2 +- proxmox-api/src/schema.rs | 2 +- 10 files changed, 60 insertions(+), 60 deletions(-) diff --git a/proxmox-api/src/cli.rs b/proxmox-api/src/cli.rs index 9a004f99..7f576ed0 100644 --- a/proxmox-api/src/cli.rs +++ b/proxmox-api/src/cli.rs @@ -93,6 +93,7 @@ impl CliCommand { } /// Define nested CLI commands. +#[derive(Default)] pub struct CliCommandMap { /// Each command has an unique name. The map associates names with /// command definitions. @@ -102,9 +103,7 @@ pub struct CliCommandMap { impl CliCommandMap { /// Create a new instance. pub fn new() -> Self { - Self { - commands: HashMap::new(), - } + Default::default() } /// Insert another command. diff --git a/proxmox-api/src/cli/command.rs b/proxmox-api/src/cli/command.rs index da6bec43..73bfdfb9 100644 --- a/proxmox-api/src/cli/command.rs +++ b/proxmox-api/src/cli/command.rs @@ -73,7 +73,7 @@ fn handle_nested_command( def: &CliCommandMap, mut args: Vec, ) -> Result<(), Error> { - if args.len() < 1 { + if args.is_empty() { let mut cmds: Vec<&String> = def.commands.keys().collect(); cmds.sort(); @@ -214,7 +214,7 @@ pub fn handle_command( pub fn run_cli_command(def: CommandLineInterface) { let def = match def { CommandLineInterface::Simple(cli_cmd) => CommandLineInterface::Simple(cli_cmd), - CommandLineInterface::Nested(map) => CommandLineInterface::Nested(map.insert_help().into()), + CommandLineInterface::Nested(map) => CommandLineInterface::Nested(map.insert_help()), }; let mut args = std::env::args(); @@ -244,7 +244,7 @@ pub fn run_cli_command(def: CommandLineInterface) { } } - if let Err(_) = handle_command(Arc::new(def), &prefix, args) { + if handle_command(Arc::new(def), &prefix, args).is_err() { std::process::exit(-1); } } diff --git a/proxmox-api/src/cli/completion.rs b/proxmox-api/src/cli/completion.rs index 956d5ea3..aaf21560 100644 --- a/proxmox-api/src/cli/completion.rs +++ b/proxmox-api/src/cli/completion.rs @@ -45,7 +45,7 @@ fn get_property_completion( let mut completions = Vec::new(); for value in list.iter() { if value.starts_with(arg) { - completions.push(value.to_string()); + completions.push((*value).to_string()); } } return completions; @@ -58,13 +58,13 @@ fn get_property_completion( lowercase_arg.make_ascii_lowercase(); for value in ["0", "1", "yes", "no", "true", "false", "on", "off"].iter() { if value.starts_with(&lowercase_arg) { - completions.push(value.to_string()); + completions.push((*value).to_string()); } } return completions; } - return Vec::new(); + Vec::new() } fn get_simple_completion( @@ -81,7 +81,9 @@ fn get_simple_completion( if args.len() > 1 { record_done_argument(done, cli_cmd.info.parameters, prop_name, &args[0]); return get_simple_completion(cli_cmd, done, &arg_param[1..], &args[1..]); - } else if args.len() == 1 { + } + + if args.len() == 1 { record_done_argument(done, cli_cmd.info.parameters, prop_name, &args[0]); if let Some((_, schema)) = cli_cmd.info.parameters.lookup(prop_name) { return get_property_completion( @@ -93,6 +95,7 @@ fn get_simple_completion( ); } } + return Vec::new(); } if args.is_empty() { @@ -115,7 +118,7 @@ fn get_simple_completion( let prefix = &args[args.len() - 1]; // match on last arg // complete option-name or option-value ? - if !prefix.starts_with("-") && args.len() > 1 { + if !prefix.starts_with('-') && args.len() > 1 { let last = &args[args.len() - 2]; if last.starts_with("--") && last.len() > 2 { let prop_name = &last[2..]; @@ -156,9 +159,7 @@ fn get_help_completion( let mut done = HashMap::new(); match def { - CommandLineInterface::Simple(_) => { - return get_simple_completion(help_cmd, &mut done, &[], args); - } + CommandLineInterface::Simple(_) => get_simple_completion(help_cmd, &mut done, &[], args), CommandLineInterface::Nested(map) => { if args.is_empty() { let mut completions = Vec::new(); @@ -177,7 +178,7 @@ fn get_help_completion( return Vec::new(); } - if first.starts_with("-") { + if first.starts_with('-') { return get_simple_completion(help_cmd, &mut done, &[], args); } @@ -187,7 +188,7 @@ fn get_help_completion( completions.push(cmd.to_string()); } } - return completions; + completions } } } @@ -199,7 +200,7 @@ fn get_nested_completion(def: &CommandLineInterface, args: &[String]) -> Vec { if args.is_empty() { @@ -222,7 +223,7 @@ fn get_nested_completion(def: &CommandLineInterface, args: &[String]) -> Vec, user: Option, @@ -11,10 +12,7 @@ pub struct CliEnvironment { impl CliEnvironment { pub fn new() -> Self { - Self { - result_attributes: HashMap::new(), - user: None, - } + Default::default() } } diff --git a/proxmox-api/src/cli/format.rs b/proxmox-api/src/cli/format.rs index 79c11506..11df87f5 100644 --- a/proxmox-api/src/cli/format.rs +++ b/proxmox-api/src/cli/format.rs @@ -1,3 +1,5 @@ +#![allow(clippy::match_bool)] // just no... + use serde_json::Value; use std::collections::HashSet; @@ -93,7 +95,7 @@ pub fn generate_usage_str( let type_text = get_schema_type_text(param_schema, ParameterDisplayStyle::Arg); if *optional { - if options.len() > 0 { + if !options.is_empty() { options.push('\n'); } options.push_str(&get_property_description( @@ -112,7 +114,11 @@ pub fn generate_usage_str( done_hash.insert(prop); } - let option_indicator = if options.len() > 0 { " [OPTIONS]" } else { "" }; + let option_indicator = if !options.is_empty() { + " [OPTIONS]" + } else { + "" + }; let mut text = match format { DocumentationFormat::Short => { @@ -131,11 +137,11 @@ pub fn generate_usage_str( ), }; - if arg_descr.len() > 0 { + if !arg_descr.is_empty() { text.push_str(&arg_descr); text.push('\n'); } - if options.len() > 0 { + if !options.is_empty() { text.push_str(&options); text.push('\n'); } @@ -170,7 +176,7 @@ pub fn generate_nested_usage( match def.commands.get(cmd).unwrap() { CommandLineInterface::Simple(cli_cmd) => { - if usage.len() > 0 && format == DocumentationFormat::ReST { + if !usage.is_empty() && format == DocumentationFormat::ReST { usage.push_str("----\n\n"); } usage.push_str(&generate_usage_str(&new_prefix, cli_cmd, format, "")); @@ -188,7 +194,7 @@ pub fn generate_nested_usage( pub fn print_help( top_def: &CommandLineInterface, mut prefix: String, - args: &Vec, + args: &[String], verbose: Option, ) { let mut iface = top_def; diff --git a/proxmox-api/src/cli/getopts.rs b/proxmox-api/src/cli/getopts.rs index 30cca95c..3f80eb91 100644 --- a/proxmox-api/src/cli/getopts.rs +++ b/proxmox-api/src/cli/getopts.rs @@ -21,28 +21,27 @@ fn parse_argument(arg: &str) -> RawArgument { }; } - let mut first = 1; - - if bytes[1] == b'-' { + let first = if bytes[1] == b'-' { if length == 2 { return RawArgument::Separator; } - first = 2; - } + 2 + } else { + 1 + }; - for start in first..length { - if bytes[start] == b'=' { - // Since we take a &str, we know the contents of it are valid utf8. - // Since bytes[start] == b'=', we know the byte beginning at start is a single-byte - // code pointer. We also know that 'first' points exactly after a single-byte code - // point as it points to the first byte after a hyphen. - // Therefore we know arg[first..start] is valid utf-8, therefore it is safe to use - // get_unchecked() to speed things up. - return RawArgument::Option { - name: unsafe { arg.get_unchecked(first..start).to_string() }, - value: Some(unsafe { arg.get_unchecked((start + 1)..).to_string() }), - }; - } + if let Some(i) = bytes[first..length].iter().position(|b| *b == b'=') { + let start = i + first; + // Since we take a &str, we know the contents of it are valid utf8. + // Since bytes[start] == b'=', we know the byte beginning at start is a single-byte + // code pointer. We also know that 'first' points exactly after a single-byte code + // point as it points to the first byte after a hyphen. + // Therefore we know arg[first..start] is valid utf-8, therefore it is safe to use + // get_unchecked() to speed things up. + return RawArgument::Option { + name: unsafe { arg.get_unchecked(first..start).to_string() }, + value: Some(unsafe { arg.get_unchecked((start + 1)..).to_string() }), + }; } RawArgument::Option { @@ -76,12 +75,9 @@ pub(crate) fn parse_argument_list>( if let Some((_optional, param_schema)) = schema.lookup(&name) { if let Schema::Boolean(boolean_schema) = param_schema { want_bool = true; - if let Some(default) = boolean_schema.default { - if default == false { - can_default = true; - } - } else { - can_default = true; + match boolean_schema.default { + Some(false) | None => can_default = true, + Some(true) => (), } } } @@ -93,7 +89,7 @@ pub(crate) fn parse_argument_list>( let next = args[pos + 1].as_ref(); if let RawArgument::Argument { .. } = parse_argument(next) { next_is_argument = true; - if let Ok(_) = parse_boolean(next) { + if parse_boolean(next).is_ok() { next_is_bool = true; } } @@ -181,7 +177,7 @@ pub fn parse_arguments>( let name = arg_param[i]; let is_last_arg_param = i == (arg_param.len() - 1); - if rest.len() == 0 { + if rest.is_empty() { if !(is_last_arg_param && last_arg_param_is_optional) { errors.push(format_err!("missing argument '{}'", name)); } @@ -195,7 +191,7 @@ pub fn parse_arguments>( } } - if errors.len() > 0 { + if !errors.is_empty() { return Err(errors); } diff --git a/proxmox-api/src/cli/readline.rs b/proxmox-api/src/cli/readline.rs index 65ea04b7..ce4ea54f 100644 --- a/proxmox-api/src/cli/readline.rs +++ b/proxmox-api/src/cli/readline.rs @@ -36,7 +36,7 @@ impl rustyline::completion::Completer for CliHelper { let (start, completions) = super::get_completions(&*self.cmd_def, line, false); - return Ok((start, completions)); + Ok((start, completions)) } } diff --git a/proxmox-api/src/cli/shellword.rs b/proxmox-api/src/cli/shellword.rs index 89fa0b65..16eaf04f 100644 --- a/proxmox-api/src/cli/shellword.rs +++ b/proxmox-api/src/cli/shellword.rs @@ -112,7 +112,7 @@ pub fn shellword_split_unclosed( /// Return words unescaped and without quotes. pub fn shellword_split(s: &str) -> Result, Error> { let (args, unclosed_field) = shellword_split_unclosed(s, true); - if !unclosed_field.is_none() { + if unclosed_field.is_some() { bail!("shellword split failed - found unclosed quote."); } Ok(args) diff --git a/proxmox-api/src/format.rs b/proxmox-api/src/format.rs index 4efd834e..3abe9120 100644 --- a/proxmox-api/src/format.rs +++ b/proxmox-api/src/format.rs @@ -291,7 +291,7 @@ pub fn dump_api( //keys.sort_unstable_by(|a, b| a.cmp(b)); for (key, sub_router) in dirmap.iter() { let sub_path = if path == "." { - key.to_string() + (*key).to_string() } else { format!("{}/{}", path, key) }; diff --git a/proxmox-api/src/schema.rs b/proxmox-api/src/schema.rs index e38e54af..5ad20fe3 100644 --- a/proxmox-api/src/schema.rs +++ b/proxmox-api/src/schema.rs @@ -634,7 +634,7 @@ pub fn parse_parameter_strings( } } - if test_required && errors.len() == 0 { + if test_required && errors.is_empty() { for (name, optional, _prop_schema) in schema.properties { if !(*optional) && params[name] == Value::Null { errors.push(format_err!(