diff --git a/crates/mdman/src/main.rs b/crates/mdman/src/main.rs index 2bdf96d7230..8fa912b2aae 100644 --- a/crates/mdman/src/main.rs +++ b/crates/mdman/src/main.rs @@ -98,15 +98,16 @@ fn process_args() -> Result { let man = args .next() .ok_or_else(|| format_err!("--man requires a value"))?; - let parts: Vec<_> = man.splitn(2, '=').collect(); - let key_parts: Vec<_> = parts[0].splitn(2, ':').collect(); - if parts.len() != 2 || key_parts.len() != 2 { - bail!("--man expected value with form name:1=link"); - } - let section: u8 = key_parts[1].parse().with_context(|| { - format!("expected unsigned integer for section, got `{}`", parts[1]) + let parts = man.split_once('=').ok_or_else(|| { + anyhow::format_err!("--man expected value with form name:1=link") + })?; + let key_parts = parts.0.split_once(':').ok_or_else(|| { + anyhow::format_err!("--man expected value with form name:1=link") + })?; + let section: u8 = key_parts.1.parse().with_context(|| { + format!("expected unsigned integer for section, got `{}`", parts.1) })?; - man_map.insert((key_parts[0].to_string(), section), parts[1].to_string()); + man_map.insert((key_parts.0.to_string(), section), parts.1.to_string()); } s => { sources.push(PathBuf::from(s)); diff --git a/src/cargo/core/compiler/custom_build.rs b/src/cargo/core/compiler/custom_build.rs index f862852169b..427656f8e84 100644 --- a/src/cargo/core/compiler/custom_build.rs +++ b/src/cargo/core/compiler/custom_build.rs @@ -681,32 +681,24 @@ impl BuildOutput { Ok(line) => line.trim(), Err(..) => continue, }; - let mut iter = line.splitn(2, ':'); - if iter.next() != Some("cargo") { - // skip this line since it doesn't start with "cargo:" - continue; - } - let data = match iter.next() { - Some(val) => { + let data = match line.split_once(':') { + Some(("cargo", val)) => { if val.starts_with(":") { // Line started with `cargo::`. - bail!("unsupported output in {}: `{}`\n\ + bail!("unsupported output in {whence}: `{line}`\n\ Found a `cargo::key=value` build directive which is reserved for future use.\n\ Either change the directive to `cargo:key=value` syntax (note the single `:`) or upgrade your version of Rust.\n\ See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \ - for more information about build script outputs.", whence, line); + for more information about build script outputs."); } val } - None => continue, + _ => continue, }; // getting the `key=value` part of the line - let mut iter = data.splitn(2, '='); - let key = iter.next(); - let value = iter.next(); - let (key, value) = match (key, value) { - (Some(a), Some(b)) => (a, b.trim_end()), + let (key, value) = match data.split_once('=') { + Some((a,b)) => (a, b.trim_end()), // Line started with `cargo:` but didn't match `key=value`. _ => bail!("invalid output in {}: `{}`\n\ Expected a line with `cargo:key=value` with an `=` character, \ @@ -765,9 +757,7 @@ impl BuildOutput { check_and_add_target!("bin", Target::is_bin, LinkArgTarget::Bin); } "rustc-link-arg-bin" => { - let mut parts = value.splitn(2, '='); - let bin_name = parts.next().unwrap().to_string(); - let arg = parts.next().ok_or_else(|| { + let (bin_name, arg) = value.split_once('=').ok_or_else(|| { anyhow::format_err!( "invalid instruction `cargo:{}={}` from {}\n\ The instruction should have the form cargo:{}=BIN=ARG", @@ -790,7 +780,10 @@ impl BuildOutput { bin_name ); } - linker_args.push((LinkArgTarget::SingleBin(bin_name), arg.to_string())); + linker_args.push(( + LinkArgTarget::SingleBin(bin_name.to_owned()), + arg.to_string(), + )); } "rustc-link-arg-tests" => { check_and_add_target!("test", Target::is_test, LinkArgTarget::Test); @@ -936,12 +929,9 @@ impl BuildOutput { /// /// [`cargo:rustc-env`]: https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-env pub fn parse_rustc_env(value: &str, whence: &str) -> CargoResult<(String, String)> { - let mut iter = value.splitn(2, '='); - let name = iter.next(); - let val = iter.next(); - match (name, val) { - (Some(n), Some(v)) => Ok((n.to_owned(), v.to_owned())), - _ => bail!("Variable rustc-env has no value in {}: {}", whence, value), + match value.split_once('=') { + Some((n, v)) => Ok((n.to_owned(), v.to_owned())), + _ => bail!("Variable rustc-env has no value in {whence}: {value}"), } } } diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 9bc80583ce6..ab49e65e4f6 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -634,19 +634,9 @@ where { let mut search_path = vec![]; for dir in paths { - let dir = match dir.to_str() { - Some(s) => { - let mut parts = s.splitn(2, '='); - match (parts.next(), parts.next()) { - (Some("native"), Some(path)) - | (Some("crate"), Some(path)) - | (Some("dependency"), Some(path)) - | (Some("framework"), Some(path)) - | (Some("all"), Some(path)) => path.into(), - _ => dir.clone(), - } - } - None => dir.clone(), + let dir = match dir.to_str().and_then(|s| s.split_once("=")) { + Some(("native" | "crate" | "dependency" | "framework" | "all", path)) => path.into(), + _ => dir.clone(), }; if dir.starts_with(&root_output) { search_path.push(dir); diff --git a/src/cargo/core/package_id_spec.rs b/src/cargo/core/package_id_spec.rs index 267ea3ffde2..68561aecc7d 100644 --- a/src/cargo/core/package_id_spec.rs +++ b/src/cargo/core/package_id_spec.rs @@ -123,24 +123,20 @@ impl PackageIdSpec { ) })?; match frag { - Some(fragment) => { - let mut parts = fragment.splitn(2, [':', '@']); - let name_or_version = parts.next().unwrap(); - match parts.next() { - Some(part) => { - let version = part.to_semver()?; - (InternedString::new(name_or_version), Some(version)) - } - None => { - if name_or_version.chars().next().unwrap().is_alphabetic() { - (InternedString::new(name_or_version), None) - } else { - let version = name_or_version.to_semver()?; - (InternedString::new(path_name), Some(version)) - } + Some(fragment) => match fragment.split_once([':', '@']) { + Some((name, part)) => { + let version = part.to_semver()?; + (InternedString::new(name), Some(version)) + } + None => { + if fragment.chars().next().unwrap().is_alphabetic() { + (InternedString::new(&fragment), None) + } else { + let version = fragment.to_semver()?; + (InternedString::new(path_name), Some(version)) } } - } + }, None => (InternedString::new(path_name), None), } }; diff --git a/src/cargo/core/source/source_id.rs b/src/cargo/core/source/source_id.rs index 6bbc07a5df3..e5fe4483f9f 100644 --- a/src/cargo/core/source/source_id.rs +++ b/src/cargo/core/source/source_id.rs @@ -148,10 +148,8 @@ impl SourceId { /// 656c58fb7c5ef5f12bc747f"); /// ``` pub fn from_url(string: &str) -> CargoResult { - let mut parts = string.splitn(2, '+'); - let kind = parts.next().unwrap(); - let url = parts - .next() + let (kind, url) = string + .split_once('+') .ok_or_else(|| anyhow::format_err!("invalid source `{}`", string))?; match kind { diff --git a/src/cargo/sources/registry/index.rs b/src/cargo/sources/registry/index.rs index f988ca15985..cc9e18735dc 100644 --- a/src/cargo/sources/registry/index.rs +++ b/src/cargo/sources/registry/index.rs @@ -587,15 +587,13 @@ impl<'cfg> RegistryIndex<'cfg> { // `=o->` where `` is the name of a crate on // this source, `` is the version installed and ` is the // version requested (argument to `--precise`). - let precise = match source_id.precise() { - Some(p) if p.starts_with(name) && p[name.len()..].starts_with('=') => { - let mut vers = p[name.len() + 1..].splitn(2, "->"); - let current_vers = vers.next().unwrap().to_semver().unwrap(); - let requested_vers = vers.next().unwrap().to_semver().unwrap(); - Some((current_vers, requested_vers)) - } - _ => None, - }; + let precise = source_id + .precise() + .filter(|p| p.starts_with(name) && p[name.len()..].starts_with('=')) + .map(|p| { + let (current, requested) = p[name.len() + 1..].split_once("->").unwrap(); + (current.to_semver().unwrap(), requested.to_semver().unwrap()) + }); let summaries = summaries.filter(|s| match &precise { Some((current, requested)) => { if req.matches(current) {