Skip to content

Commit c7dbc42

Browse files
committed
Auto merge of #10416 - Eh2406:less_mut, r=alexcrichton
Some small clean ups. Just some small changes I noticed while exploring the code base. The only thing nontrivial was noticing that get_registry_index already calls validate_package_name.
2 parents 50f1e11 + 6459ce0 commit c7dbc42

File tree

8 files changed

+27
-44
lines changed

8 files changed

+27
-44
lines changed

crates/mdman/src/hbs.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@ pub fn expand(file: &Path, formatter: FormatterRef) -> Result<String, Error> {
2424
handlebars.register_template_file("template", file)?;
2525
let includes = file.parent().unwrap().join("includes");
2626
handlebars.register_templates_directory(".md", includes)?;
27-
let mut data: HashMap<String, String> = HashMap::new();
2827
let man_name = file
2928
.file_stem()
3029
.expect("expected filename")
3130
.to_str()
3231
.expect("utf8 filename")
3332
.to_string();
34-
data.insert("man_name".to_string(), man_name);
33+
let data = HashMap::from([("man_name", man_name)]);
3534
let expanded = handlebars.render("template", &data)?;
3635
Ok(expanded)
3736
}

src/bin/cargo/commands/verify_project.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ pub fn cli() -> App {
1313

1414
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
1515
if let Err(e) = args.workspace(config) {
16-
let mut h = HashMap::new();
17-
h.insert("invalid".to_string(), e.to_string());
18-
config.shell().print_json(&h)?;
16+
config
17+
.shell()
18+
.print_json(&HashMap::from([("invalid", e.to_string())]))?;
1919
process::exit(1)
2020
}
2121

22-
let mut h = HashMap::new();
23-
h.insert("success".to_string(), "true".to_string());
24-
config.shell().print_json(&h)?;
22+
config
23+
.shell()
24+
.print_json(&HashMap::from([("success", "true")]))?;
2525
Ok(())
2626
}

src/cargo/core/compiler/rustdoc.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,8 @@ pub struct RustdocExternMap {
6464

6565
impl Default for RustdocExternMap {
6666
fn default() -> Self {
67-
let mut registries = HashMap::new();
68-
registries.insert(CRATES_IO_REGISTRY.into(), DOCS_RS_URL.into());
6967
Self {
70-
registries,
68+
registries: HashMap::from([(CRATES_IO_REGISTRY.into(), DOCS_RS_URL.into())]),
7169
std: None,
7270
}
7371
}

src/cargo/core/compiler/standard_lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ pub fn resolve_std<'cfg>(
5353
})
5454
.collect::<CargoResult<Vec<_>>>()?;
5555
let crates_io_url = crate::sources::CRATES_IO_INDEX.parse().unwrap();
56-
let mut patch = HashMap::new();
57-
patch.insert(crates_io_url, patches);
56+
let patch = HashMap::from([(crates_io_url, patches)]);
5857
let members = vec![
5958
String::from("library/std"),
6059
String::from("library/core"),

src/cargo/core/profiles.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,12 @@ impl Profiles {
139139

140140
/// Returns the hard-coded directory names for built-in profiles.
141141
fn predefined_dir_names() -> HashMap<InternedString, InternedString> {
142-
let mut dir_names = HashMap::new();
143-
dir_names.insert(InternedString::new("dev"), InternedString::new("debug"));
144-
dir_names.insert(InternedString::new("test"), InternedString::new("debug"));
145-
dir_names.insert(InternedString::new("bench"), InternedString::new("release"));
146-
dir_names
142+
[
143+
(InternedString::new("dev"), InternedString::new("debug")),
144+
(InternedString::new("test"), InternedString::new("debug")),
145+
(InternedString::new("bench"), InternedString::new("release")),
146+
]
147+
.into()
147148
}
148149

149150
/// Initialize `by_name` with the two "root" profiles, `dev`, and

src/cargo/ops/registry.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use crate::sources::{RegistrySource, SourceConfigMap, CRATES_IO_DOMAIN, CRATES_I
2424
use crate::util::config::{self, Config, SslVersionConfig, SslVersionConfigRange};
2525
use crate::util::errors::CargoResult;
2626
use crate::util::important_paths::find_root_manifest_for_wd;
27-
use crate::util::validate_package_name;
2827
use crate::util::IntoUrl;
2928
use crate::{drop_print, drop_println, version};
3029

@@ -344,7 +343,6 @@ pub fn registry_configuration(
344343
// `registry.default` is handled in command-line parsing.
345344
let (index, token, process) = match registry {
346345
Some(registry) => {
347-
validate_package_name(registry, "registry name", "")?;
348346
let index = Some(config.get_registry_index(registry)?.to_string());
349347
let token_key = format!("registries.{}.token", registry);
350348
let token = config.get_string(&token_key)?.map(|p| p.val);
@@ -418,8 +416,8 @@ fn registry(
418416
}
419417
// Parse all configuration options
420418
let reg_cfg = registry_configuration(config, registry.as_deref())?;
421-
let opt_index = reg_cfg.index.as_ref().or_else(|| index.as_ref());
422-
let sid = get_source_id(config, opt_index, registry.as_ref())?;
419+
let opt_index = reg_cfg.index.as_deref().or_else(|| index.as_deref());
420+
let sid = get_source_id(config, opt_index, registry.as_deref())?;
423421
if !sid.is_remote_registry() {
424422
bail!(
425423
"{} does not support API commands.\n\
@@ -892,11 +890,7 @@ pub fn yank(
892890
///
893891
/// The `index` and `reg` values are from the command-line or config settings.
894892
/// If both are None, returns the source for crates.io.
895-
fn get_source_id(
896-
config: &Config,
897-
index: Option<&String>,
898-
reg: Option<&String>,
899-
) -> CargoResult<SourceId> {
893+
fn get_source_id(config: &Config, index: Option<&str>, reg: Option<&str>) -> CargoResult<SourceId> {
900894
match (reg, index) {
901895
(Some(r), _) => SourceId::alt_registry(config, r),
902896
(_, Some(i)) => SourceId::for_registry(&i.into_url()?),

src/cargo/util/config/mod.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,8 +1053,7 @@ impl Config {
10531053
}
10541054

10551055
fn load_file(&self, path: &Path, includes: bool) -> CargoResult<ConfigValue> {
1056-
let mut seen = HashSet::new();
1057-
self._load_file(path, &mut seen, includes)
1056+
self._load_file(path, &mut HashSet::new(), includes)
10581057
}
10591058

10601059
fn _load_file(
@@ -1119,7 +1118,7 @@ impl Config {
11191118
cv: &mut CV,
11201119
remove: bool,
11211120
) -> CargoResult<Vec<(String, PathBuf, Definition)>> {
1122-
let abs = |path: &String, def: &Definition| -> (String, PathBuf, Definition) {
1121+
let abs = |path: &str, def: &Definition| -> (String, PathBuf, Definition) {
11231122
let abs_path = match def {
11241123
Definition::Path(p) => p.parent().unwrap().join(&path),
11251124
Definition::Environment(_) | Definition::Cli => self.cwd().join(&path),
@@ -1171,9 +1170,8 @@ impl Config {
11711170
anyhow::format_err!("config path {:?} is not utf-8", arg_as_path)
11721171
})?
11731172
.to_string();
1174-
let mut map = HashMap::new();
11751173
let value = CV::String(str_path, Definition::Cli);
1176-
map.insert("include".to_string(), value);
1174+
let map = HashMap::from([("include".to_string(), value)]);
11771175
CV::Table(map, Definition::Cli)
11781176
} else {
11791177
// We only want to allow "dotted key" (see https://toml.io/en/v1.0.0#keys)
@@ -1253,9 +1251,8 @@ impl Config {
12531251
CV::from_toml(Definition::Cli, toml_v)
12541252
.with_context(|| format!("failed to convert --config argument `{arg}`"))?
12551253
};
1256-
let mut seen = HashSet::new();
12571254
let tmp_table = self
1258-
.load_includes(tmp_table, &mut seen)
1255+
.load_includes(tmp_table, &mut HashSet::new())
12591256
.with_context(|| "failed to load --config include".to_string())?;
12601257
loaded_args
12611258
.merge(tmp_table, true)
@@ -1419,8 +1416,7 @@ impl Config {
14191416

14201417
if let Some(token) = value_map.remove("token") {
14211418
if let Vacant(entry) = value_map.entry("registry".into()) {
1422-
let mut map = HashMap::new();
1423-
map.insert("token".into(), token);
1419+
let map = HashMap::from([("token".into(), token)]);
14241420
let table = CV::Table(map, def.clone());
14251421
entry.insert(table);
14261422
}
@@ -1994,8 +1990,7 @@ pub fn save_credentials(
19941990

19951991
// Move the old token location to the new one.
19961992
if let Some(token) = toml.as_table_mut().unwrap().remove("token") {
1997-
let mut map = HashMap::new();
1998-
map.insert("token".to_string(), token);
1993+
let map = HashMap::from([("token".to_string(), token)]);
19991994
toml.as_table_mut()
20001995
.unwrap()
20011996
.insert("registry".into(), map.into());
@@ -2006,13 +2001,11 @@ pub fn save_credentials(
20062001
let (key, mut value) = {
20072002
let key = "token".to_string();
20082003
let value = ConfigValue::String(token, Definition::Path(file.path().to_path_buf()));
2009-
let mut map = HashMap::new();
2010-
map.insert(key, value);
2004+
let map = HashMap::from([(key, value)]);
20112005
let table = CV::Table(map, Definition::Path(file.path().to_path_buf()));
20122006

20132007
if let Some(registry) = registry {
2014-
let mut map = HashMap::new();
2015-
map.insert(registry.to_string(), table);
2008+
let map = HashMap::from([(registry.to_string(), table)]);
20162009
(
20172010
"registries".into(),
20182011
CV::Table(map, Definition::Path(file.path().to_path_buf())),

src/cargo/util/diagnostic_server.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ impl Message {
7575
.shutdown(Shutdown::Write)
7676
.context("failed to shutdown")?;
7777

78-
let mut tmp = Vec::new();
7978
client
80-
.read_to_end(&mut tmp)
79+
.read_to_end(&mut Vec::new())
8180
.context("failed to receive a disconnect")?;
8281

8382
Ok(())

0 commit comments

Comments
 (0)