Skip to content

Commit

Permalink
Ues strip_prefix for cleaner code
Browse files Browse the repository at this point in the history
  • Loading branch information
Eh2406 committed Sep 6, 2023
1 parent 84544d4 commit 1c6e57e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
7 changes: 5 additions & 2 deletions crates/cargo-test-support/src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ pub fn validate_crate_contents(
(name, contents)
})
.collect();
assert!(expected_crate_name.ends_with(".crate"));
let base_crate_name = Path::new(&expected_crate_name[..expected_crate_name.len() - 6]);
let base_crate_name = Path::new(
expected_crate_name
.strip_suffix(".crate")
.expect("must end with .crate"),
);
let actual_files: HashSet<PathBuf> = files.keys().cloned().collect();
let expected_files: HashSet<PathBuf> = expected_files
.iter()
Expand Down
15 changes: 8 additions & 7 deletions src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,18 @@ fn list_commands(config: &Config) -> BTreeMap<String, CommandInfo> {
};
for entry in entries.filter_map(|e| e.ok()) {
let path = entry.path();
let filename = match path.file_name().and_then(|s| s.to_str()) {
Some(filename) => filename,
_ => continue,
let Some(filename) = path.file_name().and_then(|s| s.to_str()) else {
continue;
};
if !filename.starts_with(prefix) || !filename.ends_with(suffix) {
let Some(name) = filename
.strip_prefix(prefix)
.and_then(|s| s.strip_suffix(suffix))
else {
continue;
}
};
if is_executable(entry.path()) {
let end = filename.len() - suffix.len();
commands.insert(
filename[prefix.len()..end].to_string(),
name.to_string(),
CommandInfo::External { path: path.clone() },
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/resolver/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ impl EncodableResolve {
let mut to_remove = Vec::new();
for (k, v) in metadata.iter().filter(|p| p.0.starts_with(prefix)) {
to_remove.push(k.to_string());
let k = &k[prefix.len()..];
let k = k.strip_prefix(prefix).unwrap();
let enc_id: EncodablePackageId = k
.parse()
.with_context(|| internal("invalid encoding of checksum in lockfile"))?;
Expand Down
5 changes: 2 additions & 3 deletions src/cargo/util/config/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,8 @@ impl<'config> ConfigMapAccess<'config> {
// `CARGO_PROFILE_DEV_PACKAGE_`
let env_prefix = format!("{}_", de.key.as_env_key());
for env_key in de.config.env_keys() {
if env_key.starts_with(&env_prefix) {
// `CARGO_PROFILE_DEV_PACKAGE_bar_OPT_LEVEL = 3`
let rest = &env_key[env_prefix.len()..];
// `CARGO_PROFILE_DEV_PACKAGE_bar_OPT_LEVEL = 3`
if let Some(rest) = env_key.strip_prefix(&env_prefix) {
// `rest = bar_OPT_LEVEL`
let part = rest.splitn(2, '_').next().unwrap();
// `part = "bar"`
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/util/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ impl Rustc {
let extract = |field: &str| -> CargoResult<&str> {
verbose_version
.lines()
.find(|l| l.starts_with(field))
.map(|l| &l[field.len()..])
.filter_map(|l| l.strip_prefix(field))
.next()
.ok_or_else(|| {
anyhow::format_err!(
"`rustc -vV` didn't have a line for `{}`, got:\n{}",
Expand Down

0 comments on commit 1c6e57e

Please sign in to comment.