Skip to content

Commit

Permalink
refactor: remove unnecessary anyhow macro calls
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed Apr 16, 2021
1 parent ebca519 commit 9099b49
Show file tree
Hide file tree
Showing 15 changed files with 31 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/compile_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl CompileTarget {
// with different paths always produce the same result.
let path = Path::new(name)
.canonicalize()
.with_context(|| anyhow::format_err!("target path {:?} is not a valid file", name))?;
.with_context(|| format!("target path {:?} is not a valid file", name))?;

let name = path
.into_os_string()
Expand Down
6 changes: 2 additions & 4 deletions src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,10 +498,8 @@ impl Manifest {
self.unstable_features
.require(Feature::test_dummy_unstable())
.with_context(|| {
anyhow::format_err!(
"the `im-a-teapot` manifest key is unstable and may \
not work properly in England"
)
"the `im-a-teapot` manifest key is unstable and may \
not work properly in England"
})?;
}

Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ impl<'a, 'cfg> Downloads<'a, 'cfg> {
.ok_or_else(|| internal(format!("couldn't find source for `{}`", id)))?;
let pkg = source
.download(id)
.with_context(|| anyhow::format_err!("unable to get packages from source"))?;
.with_context(|| "unable to get packages from source")?;
let (url, descriptor) = match pkg {
MaybePackage::Ready(pkg) => {
debug!("{} doesn't need a download", id);
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/package_id_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl PackageIdSpec {
let i: Vec<_> = i.into_iter().collect();
let spec = PackageIdSpec::parse(spec).with_context(|| {
let suggestion = lev_distance::closest_msg(spec, i.iter(), |id| id.name().as_str());
anyhow::format_err!("invalid package ID specification: `{}`{}", spec, suggestion)
format!("invalid package ID specification: `{}`{}", spec, suggestion)
})?;
spec.query(i)
}
Expand Down
5 changes: 2 additions & 3 deletions src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,10 +1142,9 @@ fn get_config_profile(ws: &Workspace<'_>, name: &str) -> CargoResult<Option<Toml
.val
.validate(name, ws.unstable_features(), &mut warnings)
.with_context(|| {
anyhow::format_err!(
format!(
"config profile `{}` is not valid (defined in `{}`)",
name,
profile.definition
name, profile.definition
)
})?;
for warning in warnings {
Expand Down
8 changes: 4 additions & 4 deletions src/cargo/core/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ impl<'cfg> PackageRegistry<'cfg> {
// corresponding to this `dep`.
self.ensure_loaded(dep.source_id(), Kind::Normal)
.with_context(|| {
anyhow::format_err!(
format!(
"failed to load source for dependency `{}`",
dep.package_name()
)
Expand Down Expand Up @@ -322,7 +322,7 @@ impl<'cfg> PackageRegistry<'cfg> {
Ok(summary)
})
.collect::<CargoResult<Vec<_>>>()
.with_context(|| anyhow::format_err!("failed to resolve patches for `{}`", url))?;
.with_context(|| format!("failed to resolve patches for `{}`", url))?;

let mut name_and_version = HashSet::new();
for summary in unlocked_summaries.iter() {
Expand Down Expand Up @@ -390,7 +390,7 @@ impl<'cfg> PackageRegistry<'cfg> {
let _p = profile::start(format!("updating: {}", source_id));
self.sources.get_mut(source_id).unwrap().update()
})()
.with_context(|| anyhow::format_err!("Unable to update {}", source_id))?;
.with_context(|| format!("Unable to update {}", source_id))?;
Ok(())
}

Expand Down Expand Up @@ -542,7 +542,7 @@ impl<'cfg> Registry for PackageRegistry<'cfg> {
// Ensure the requested source_id is loaded
self.ensure_loaded(dep.source_id(), Kind::Normal)
.with_context(|| {
anyhow::format_err!(
format!(
"failed to load source for dependency `{}`",
dep.package_name()
)
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/resolver/dep_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl<'a> RegistryQueryer<'a> {
.into_iter()
.map(|(dep, features)| {
let candidates = self.query(&dep).with_context(|| {
anyhow::format_err!(
format!(
"failed to get `{}` as a dependency of {}",
dep.package_name(),
describe_path(&cx.parents.path_to_bottom(&candidate.package_id())),
Expand Down
9 changes: 2 additions & 7 deletions src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1415,14 +1415,9 @@ impl WorkspaceRootConfig {
Some(p) => p,
None => return Ok(Vec::new()),
};
let res = glob(path)
.with_context(|| anyhow::format_err!("could not parse pattern `{}`", &path))?;
let res = glob(path).with_context(|| format!("could not parse pattern `{}`", &path))?;
let res = res
.map(|p| {
p.with_context(|| {
anyhow::format_err!("unable to match path to pattern `{}`", &path)
})
})
.map(|p| p.with_context(|| format!("unable to match path to pattern `{}`", &path)))
.collect::<Result<Vec<_>, _>>()?;
Ok(res)
}
Expand Down
6 changes: 2 additions & 4 deletions src/cargo/ops/cargo_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,12 @@ fn rm_rf(path: &Path, config: &Config) -> CargoResult<()> {
config
.shell()
.verbose(|shell| shell.status("Removing", path.display()))?;
paths::remove_dir_all(path)
.with_context(|| anyhow::format_err!("could not remove build directory"))?;
paths::remove_dir_all(path).with_context(|| "could not remove build directory")?;
} else if m.is_ok() {
config
.shell()
.verbose(|shell| shell.status("Removing", path.display()))?;
paths::remove_file(path)
.with_context(|| anyhow::format_err!("failed to remove build artifact"))?;
paths::remove_file(path).with_context(|| "failed to remove build artifact")?;
}
Ok(())
}
6 changes: 3 additions & 3 deletions src/cargo/ops/cargo_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ fn install_one(
td.into_path();
}

format_err!(
format!(
"failed to compile `{}`, intermediate artifacts can be \
found at `{}`",
pkg,
Expand Down Expand Up @@ -422,7 +422,7 @@ fn install_one(
let dst = dst.join(bin);
config.shell().status("Installing", dst.display())?;
fs::rename(&src, &dst).with_context(|| {
format_err!("failed to move `{}` to `{}`", src.display(), dst.display())
format!("failed to move `{}` to `{}`", src.display(), dst.display())
})?;
installed.bins.push(dst);
successful_bins.insert(bin.to_string());
Expand All @@ -437,7 +437,7 @@ fn install_one(
let dst = dst.join(bin);
config.shell().status("Replacing", dst.display())?;
fs::rename(&src, &dst).with_context(|| {
format_err!("failed to move `{}` to `{}`", src.display(), dst.display())
format!("failed to move `{}` to `{}`", src.display(), dst.display())
})?;
successful_bins.insert(bin.to_string());
}
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ pub fn new(opts: &NewOptions, config: &Config) -> CargoResult<()> {
};

mk(config, &mkopts).with_context(|| {
anyhow::format_err!(
format!(
"Failed to create package `{}` at `{}`",
name,
path.display()
Expand Down Expand Up @@ -502,7 +502,7 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> {
};

mk(config, &mkopts).with_context(|| {
anyhow::format_err!(
format!(
"Failed to create package `{}` at `{}`",
name,
path.display()
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Option
.status("Packaging", pkg.package_id().to_string())?;
dst.file().set_len(0)?;
tar(ws, ar_files, dst.file(), &filename)
.with_context(|| anyhow::format_err!("failed to prepare local package for uploading"))?;
.with_context(|| "failed to prepare local package for uploading")?;
if opts.verify {
dst.seek(SeekFrom::Start(0))?;
run_verify(ws, &dst, opts).with_context(|| "failed to verify package tarball")?
Expand Down
13 changes: 6 additions & 7 deletions src/cargo/ops/common_for_install_and_uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,11 @@ impl InstallTracker {
if contents.is_empty() {
Ok(CrateListingV1::default())
} else {
Ok(toml::from_str(&contents)
.with_context(|| format_err!("invalid TOML found for metadata"))?)
Ok(toml::from_str(&contents).with_context(|| "invalid TOML found for metadata")?)
}
})()
.with_context(|| {
format_err!(
format!(
"failed to parse crate metadata at `{}`",
v1_lock.path().to_string_lossy()
)
Expand All @@ -119,13 +118,13 @@ impl InstallTracker {
CrateListingV2::default()
} else {
serde_json::from_str(&contents)
.with_context(|| format_err!("invalid JSON found for metadata"))?
.with_context(|| "invalid JSON found for metadata")?
};
v2.sync_v1(&v1);
Ok(v2)
})()
.with_context(|| {
format_err!(
format!(
"failed to parse crate metadata at `{}`",
v2_lock.path().to_string_lossy()
)
Expand Down Expand Up @@ -279,14 +278,14 @@ impl InstallTracker {
/// Save tracking information to disk.
pub fn save(&self) -> CargoResult<()> {
self.v1.save(&self.v1_lock).with_context(|| {
format_err!(
format!(
"failed to write crate metadata at `{}`",
self.v1_lock.path().to_string_lossy()
)
})?;

self.v2.save(&self.v2_lock).with_context(|| {
format_err!(
format!(
"failed to write crate metadata at `{}`",
self.v2_lock.path().to_string_lossy()
)
Expand Down
3 changes: 1 addition & 2 deletions src/cargo/ops/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ pub fn vendor(ws: &Workspace<'_>, opts: &VendorOptions<'_>) -> CargoResult<()> {
extra_workspaces.push(ws);
}
let workspaces = extra_workspaces.iter().chain(Some(ws)).collect::<Vec<_>>();
let vendor_config =
sync(config, &workspaces, opts).with_context(|| "failed to sync".to_string())?;
let vendor_config = sync(config, &workspaces, opts).with_context(|| "failed to sync")?;

if config.shell().verbosity() != Verbosity::Quiet {
crate::drop_eprint!(
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 @@ -222,10 +222,10 @@ impl Cache {
.with_context(|| format!("could not execute process {} (never executed)", cmd))?;
let stdout = String::from_utf8(output.stdout)
.map_err(|e| anyhow::anyhow!("{}: {:?}", e, e.as_bytes()))
.with_context(|| anyhow::anyhow!("`{}` didn't return utf8 output", cmd))?;
.with_context(|| format!("`{}` didn't return utf8 output", cmd))?;
let stderr = String::from_utf8(output.stderr)
.map_err(|e| anyhow::anyhow!("{}: {:?}", e, e.as_bytes()))
.with_context(|| anyhow::anyhow!("`{}` didn't return utf8 output", cmd))?;
.with_context(|| format!("`{}` didn't return utf8 output", cmd))?;
self.data.outputs.insert(
key,
Output {
Expand Down

0 comments on commit 9099b49

Please sign in to comment.