From 69b82bfe4c45750ec5130d43d66139d807e18025 Mon Sep 17 00:00:00 2001 From: Jacob Finkelman Date: Wed, 23 Feb 2022 19:43:29 +0000 Subject: [PATCH 1/5] &String -> &str --- src/cargo/ops/registry.rs | 10 +++------- src/cargo/util/config/mod.rs | 2 +- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index b8e47d921..0044415c1 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -418,8 +418,8 @@ fn registry( } // Parse all configuration options let reg_cfg = registry_configuration(config, registry.as_deref())?; - let opt_index = reg_cfg.index.as_ref().or_else(|| index.as_ref()); - let sid = get_source_id(config, opt_index, registry.as_ref())?; + let opt_index = reg_cfg.index.as_deref().or_else(|| index.as_deref()); + let sid = get_source_id(config, opt_index, registry.as_deref())?; if !sid.is_remote_registry() { bail!( "{} does not support API commands.\n\ @@ -892,11 +892,7 @@ pub fn yank( /// /// The `index` and `reg` values are from the command-line or config settings. /// If both are None, returns the source for crates.io. -fn get_source_id( - config: &Config, - index: Option<&String>, - reg: Option<&String>, -) -> CargoResult { +fn get_source_id(config: &Config, index: Option<&str>, reg: Option<&str>) -> CargoResult { match (reg, index) { (Some(r), _) => SourceId::alt_registry(config, r), (_, Some(i)) => SourceId::for_registry(&i.into_url()?), diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index 0f183cdf5..2a18621dd 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -1119,7 +1119,7 @@ impl Config { cv: &mut CV, remove: bool, ) -> CargoResult> { - let abs = |path: &String, def: &Definition| -> (String, PathBuf, Definition) { + let abs = |path: &str, def: &Definition| -> (String, PathBuf, Definition) { let abs_path = match def { Definition::Path(p) => p.parent().unwrap().join(&path), Definition::Environment(_) | Definition::Cli => self.cwd().join(&path), From 7a3ec1d27441306d33d68ee394d96952db97c046 Mon Sep 17 00:00:00 2001 From: Jacob Finkelman Date: Wed, 23 Feb 2022 20:46:13 +0000 Subject: [PATCH 2/5] dont call validate_package_name when get_registry_index alredy does it. --- src/cargo/ops/registry.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index 0044415c1..2c67091ec 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -24,7 +24,6 @@ use crate::sources::{RegistrySource, SourceConfigMap, CRATES_IO_DOMAIN, CRATES_I use crate::util::config::{self, Config, SslVersionConfig, SslVersionConfigRange}; use crate::util::errors::CargoResult; use crate::util::important_paths::find_root_manifest_for_wd; -use crate::util::validate_package_name; use crate::util::IntoUrl; use crate::{drop_print, drop_println, version}; @@ -344,7 +343,6 @@ pub fn registry_configuration( // `registry.default` is handled in command-line parsing. let (index, token, process) = match registry { Some(registry) => { - validate_package_name(registry, "registry name", "")?; let index = Some(config.get_registry_index(registry)?.to_string()); let token_key = format!("registries.{}.token", registry); let token = config.get_string(&token_key)?.map(|p| p.val); From 4bfed401782d1ad61ba7c5433df9a51fc01bbce1 Mon Sep 17 00:00:00 2001 From: Jacob Finkelman Date: Wed, 23 Feb 2022 20:46:51 +0000 Subject: [PATCH 3/5] don't need mut --- crates/mdman/src/hbs.rs | 3 +-- src/bin/cargo/commands/verify_project.rs | 6 ++---- src/cargo/core/compiler/rustdoc.rs | 3 +-- src/cargo/core/compiler/standard_lib.rs | 3 +-- src/cargo/core/profiles.rs | 11 ++++++----- src/cargo/util/config/mod.rs | 21 +++++++-------------- src/cargo/util/diagnostic_server.rs | 3 +-- 7 files changed, 19 insertions(+), 31 deletions(-) diff --git a/crates/mdman/src/hbs.rs b/crates/mdman/src/hbs.rs index a8cbae94a..eefeb3c1a 100644 --- a/crates/mdman/src/hbs.rs +++ b/crates/mdman/src/hbs.rs @@ -24,14 +24,13 @@ pub fn expand(file: &Path, formatter: FormatterRef) -> Result { handlebars.register_template_file("template", file)?; let includes = file.parent().unwrap().join("includes"); handlebars.register_templates_directory(".md", includes)?; - let mut data: HashMap = HashMap::new(); let man_name = file .file_stem() .expect("expected filename") .to_str() .expect("utf8 filename") .to_string(); - data.insert("man_name".to_string(), man_name); + let data: HashMap = [("man_name", man_name)].into(); let expanded = handlebars.render("template", &data)?; Ok(expanded) } diff --git a/src/bin/cargo/commands/verify_project.rs b/src/bin/cargo/commands/verify_project.rs index e3779d895..c67bb6b80 100644 --- a/src/bin/cargo/commands/verify_project.rs +++ b/src/bin/cargo/commands/verify_project.rs @@ -13,14 +13,12 @@ pub fn cli() -> App { pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { if let Err(e) = args.workspace(config) { - let mut h = HashMap::new(); - h.insert("invalid".to_string(), e.to_string()); + let h: HashMap<_, _> = [("invalid", e.to_string())].into(); config.shell().print_json(&h)?; process::exit(1) } - let mut h = HashMap::new(); - h.insert("success".to_string(), "true".to_string()); + let h: HashMap<_, _> = [("success", "true")].into(); config.shell().print_json(&h)?; Ok(()) } diff --git a/src/cargo/core/compiler/rustdoc.rs b/src/cargo/core/compiler/rustdoc.rs index d9244404a..bbc8488ca 100644 --- a/src/cargo/core/compiler/rustdoc.rs +++ b/src/cargo/core/compiler/rustdoc.rs @@ -64,8 +64,7 @@ pub struct RustdocExternMap { impl Default for RustdocExternMap { fn default() -> Self { - let mut registries = HashMap::new(); - registries.insert(CRATES_IO_REGISTRY.into(), DOCS_RS_URL.into()); + let registries = [(CRATES_IO_REGISTRY.into(), DOCS_RS_URL.into())].into(); Self { registries, std: None, diff --git a/src/cargo/core/compiler/standard_lib.rs b/src/cargo/core/compiler/standard_lib.rs index bf159e178..4de305cda 100644 --- a/src/cargo/core/compiler/standard_lib.rs +++ b/src/cargo/core/compiler/standard_lib.rs @@ -53,8 +53,7 @@ pub fn resolve_std<'cfg>( }) .collect::>>()?; let crates_io_url = crate::sources::CRATES_IO_INDEX.parse().unwrap(); - let mut patch = HashMap::new(); - patch.insert(crates_io_url, patches); + let patch = [(crates_io_url, patches)].into(); let members = vec![ String::from("library/std"), String::from("library/core"), diff --git a/src/cargo/core/profiles.rs b/src/cargo/core/profiles.rs index f15f48e33..bb354abdc 100644 --- a/src/cargo/core/profiles.rs +++ b/src/cargo/core/profiles.rs @@ -139,11 +139,12 @@ impl Profiles { /// Returns the hard-coded directory names for built-in profiles. fn predefined_dir_names() -> HashMap { - let mut dir_names = HashMap::new(); - dir_names.insert(InternedString::new("dev"), InternedString::new("debug")); - dir_names.insert(InternedString::new("test"), InternedString::new("debug")); - dir_names.insert(InternedString::new("bench"), InternedString::new("release")); - dir_names + [ + (InternedString::new("dev"), InternedString::new("debug")), + (InternedString::new("test"), InternedString::new("debug")), + (InternedString::new("bench"), InternedString::new("release")), + ] + .into() } /// Initialize `by_name` with the two "root" profiles, `dev`, and diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index 2a18621dd..24c32e817 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -1053,8 +1053,7 @@ impl Config { } fn load_file(&self, path: &Path, includes: bool) -> CargoResult { - let mut seen = HashSet::new(); - self._load_file(path, &mut seen, includes) + self._load_file(path, &mut HashSet::new(), includes) } fn _load_file( @@ -1171,9 +1170,8 @@ impl Config { anyhow::format_err!("config path {:?} is not utf-8", arg_as_path) })? .to_string(); - let mut map = HashMap::new(); let value = CV::String(str_path, Definition::Cli); - map.insert("include".to_string(), value); + let map = [("include".to_string(), value)].into(); CV::Table(map, Definition::Cli) } else { // We only want to allow "dotted key" (see https://toml.io/en/v1.0.0#keys) @@ -1253,9 +1251,8 @@ impl Config { CV::from_toml(Definition::Cli, toml_v) .with_context(|| format!("failed to convert --config argument `{arg}`"))? }; - let mut seen = HashSet::new(); let tmp_table = self - .load_includes(tmp_table, &mut seen) + .load_includes(tmp_table, &mut HashSet::new()) .with_context(|| "failed to load --config include".to_string())?; loaded_args .merge(tmp_table, true) @@ -1419,8 +1416,7 @@ impl Config { if let Some(token) = value_map.remove("token") { if let Vacant(entry) = value_map.entry("registry".into()) { - let mut map = HashMap::new(); - map.insert("token".into(), token); + let map = [("token".into(), token)].into(); let table = CV::Table(map, def.clone()); entry.insert(table); } @@ -1994,8 +1990,7 @@ pub fn save_credentials( // Move the old token location to the new one. if let Some(token) = toml.as_table_mut().unwrap().remove("token") { - let mut map = HashMap::new(); - map.insert("token".to_string(), token); + let map: HashMap<_, _> = [("token".to_string(), token)].into(); toml.as_table_mut() .unwrap() .insert("registry".into(), map.into()); @@ -2006,13 +2001,11 @@ pub fn save_credentials( let (key, mut value) = { let key = "token".to_string(); let value = ConfigValue::String(token, Definition::Path(file.path().to_path_buf())); - let mut map = HashMap::new(); - map.insert(key, value); + let map = [(key, value)].into(); let table = CV::Table(map, Definition::Path(file.path().to_path_buf())); if let Some(registry) = registry { - let mut map = HashMap::new(); - map.insert(registry.to_string(), table); + let map = [(registry.to_string(), table)].into(); ( "registries".into(), CV::Table(map, Definition::Path(file.path().to_path_buf())), diff --git a/src/cargo/util/diagnostic_server.rs b/src/cargo/util/diagnostic_server.rs index 22bbf0ca2..4a99836b2 100644 --- a/src/cargo/util/diagnostic_server.rs +++ b/src/cargo/util/diagnostic_server.rs @@ -75,9 +75,8 @@ impl Message { .shutdown(Shutdown::Write) .context("failed to shutdown")?; - let mut tmp = Vec::new(); client - .read_to_end(&mut tmp) + .read_to_end(&mut Vec::new()) .context("failed to receive a disconnect")?; Ok(()) From 96dc595eaf81312eace669b72d568d4e4cc01703 Mon Sep 17 00:00:00 2001 From: Jacob Finkelman Date: Thu, 24 Feb 2022 18:24:33 +0000 Subject: [PATCH 4/5] HashMap::from not into --- crates/mdman/src/hbs.rs | 2 +- src/bin/cargo/commands/verify_project.rs | 6 ++---- src/cargo/core/compiler/rustdoc.rs | 3 +-- src/cargo/core/compiler/standard_lib.rs | 2 +- src/cargo/util/config/mod.rs | 10 +++++----- 5 files changed, 10 insertions(+), 13 deletions(-) diff --git a/crates/mdman/src/hbs.rs b/crates/mdman/src/hbs.rs index eefeb3c1a..022e696b8 100644 --- a/crates/mdman/src/hbs.rs +++ b/crates/mdman/src/hbs.rs @@ -30,7 +30,7 @@ pub fn expand(file: &Path, formatter: FormatterRef) -> Result { .to_str() .expect("utf8 filename") .to_string(); - let data: HashMap = [("man_name", man_name)].into(); + let data = HashMap::from([("man_name", man_name)]); let expanded = handlebars.render("template", &data)?; Ok(expanded) } diff --git a/src/bin/cargo/commands/verify_project.rs b/src/bin/cargo/commands/verify_project.rs index c67bb6b80..46e3eb885 100644 --- a/src/bin/cargo/commands/verify_project.rs +++ b/src/bin/cargo/commands/verify_project.rs @@ -13,12 +13,10 @@ pub fn cli() -> App { pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { if let Err(e) = args.workspace(config) { - let h: HashMap<_, _> = [("invalid", e.to_string())].into(); - config.shell().print_json(&h)?; + config.shell().print_json(&HashMap::from([("invalid", e.to_string())]))?; process::exit(1) } - let h: HashMap<_, _> = [("success", "true")].into(); - config.shell().print_json(&h)?; + config.shell().print_json(&HashMap::from([("success", "true")]))?; Ok(()) } diff --git a/src/cargo/core/compiler/rustdoc.rs b/src/cargo/core/compiler/rustdoc.rs index bbc8488ca..ff7f00f3c 100644 --- a/src/cargo/core/compiler/rustdoc.rs +++ b/src/cargo/core/compiler/rustdoc.rs @@ -64,9 +64,8 @@ pub struct RustdocExternMap { impl Default for RustdocExternMap { fn default() -> Self { - let registries = [(CRATES_IO_REGISTRY.into(), DOCS_RS_URL.into())].into(); Self { - registries, + registries: HashMap::from([(CRATES_IO_REGISTRY.into(), DOCS_RS_URL.into())]), std: None, } } diff --git a/src/cargo/core/compiler/standard_lib.rs b/src/cargo/core/compiler/standard_lib.rs index 4de305cda..94242fdb8 100644 --- a/src/cargo/core/compiler/standard_lib.rs +++ b/src/cargo/core/compiler/standard_lib.rs @@ -53,7 +53,7 @@ pub fn resolve_std<'cfg>( }) .collect::>>()?; let crates_io_url = crate::sources::CRATES_IO_INDEX.parse().unwrap(); - let patch = [(crates_io_url, patches)].into(); + let patch = HashMap::from([(crates_io_url, patches)]); let members = vec![ String::from("library/std"), String::from("library/core"), diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index 24c32e817..0adfcb249 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -1171,7 +1171,7 @@ impl Config { })? .to_string(); let value = CV::String(str_path, Definition::Cli); - let map = [("include".to_string(), value)].into(); + let map = HashMap::from([("include".to_string(), value)]); CV::Table(map, Definition::Cli) } else { // We only want to allow "dotted key" (see https://toml.io/en/v1.0.0#keys) @@ -1416,7 +1416,7 @@ impl Config { if let Some(token) = value_map.remove("token") { if let Vacant(entry) = value_map.entry("registry".into()) { - let map = [("token".into(), token)].into(); + let map = HashMap::from([("token".into(), token)]); let table = CV::Table(map, def.clone()); entry.insert(table); } @@ -1990,7 +1990,7 @@ pub fn save_credentials( // Move the old token location to the new one. if let Some(token) = toml.as_table_mut().unwrap().remove("token") { - let map: HashMap<_, _> = [("token".to_string(), token)].into(); + let map = HashMap::from([("token".to_string(), token)]); toml.as_table_mut() .unwrap() .insert("registry".into(), map.into()); @@ -2001,11 +2001,11 @@ pub fn save_credentials( let (key, mut value) = { let key = "token".to_string(); let value = ConfigValue::String(token, Definition::Path(file.path().to_path_buf())); - let map = [(key, value)].into(); + let map = HashMap::from([(key, value)]); let table = CV::Table(map, Definition::Path(file.path().to_path_buf())); if let Some(registry) = registry { - let map = [(registry.to_string(), table)].into(); + let map = HashMap::from([(registry.to_string(), table)]); ( "registries".into(), CV::Table(map, Definition::Path(file.path().to_path_buf())), From 6459ce0314b0f3160d2e4211ce6bea1a94eb486c Mon Sep 17 00:00:00 2001 From: Jacob Finkelman Date: Thu, 24 Feb 2022 18:28:15 +0000 Subject: [PATCH 5/5] fmt --- src/bin/cargo/commands/verify_project.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/bin/cargo/commands/verify_project.rs b/src/bin/cargo/commands/verify_project.rs index 46e3eb885..4153da878 100644 --- a/src/bin/cargo/commands/verify_project.rs +++ b/src/bin/cargo/commands/verify_project.rs @@ -13,10 +13,14 @@ pub fn cli() -> App { pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { if let Err(e) = args.workspace(config) { - config.shell().print_json(&HashMap::from([("invalid", e.to_string())]))?; + config + .shell() + .print_json(&HashMap::from([("invalid", e.to_string())]))?; process::exit(1) } - config.shell().print_json(&HashMap::from([("success", "true")]))?; + config + .shell() + .print_json(&HashMap::from([("success", "true")]))?; Ok(()) }