Skip to content

Commit

Permalink
Remove/refactor some .unwraps.
Browse files Browse the repository at this point in the history
Many of these had some precondition being changed that guaranteed they'd
never happen, but could be expressed to just avoid the `.unwrap`
entirely (easier to check).

Other `Option` unwraps were changed to have a mildly more informative
error message by using `.expect`.
  • Loading branch information
huonw committed Jun 27, 2014
1 parent 2f0ac4a commit 3543133
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
13 changes: 7 additions & 6 deletions src/bin/cargo-verify-project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ fn main() {
}
};

if !matches.opt_present("m") {
fail("missing-argument", "manifest");
return;
}

let manifest = matches.opt_str("m").unwrap();
let manifest = match matches.opt_str("m") {
Some(m) => m,
None => {
fail("missing-argument", "manifest");
return;
}
};
let file = Path::new(manifest);
let contents = match File::open(&file).read_to_str() {
Ok(s) => s,
Expand Down
5 changes: 3 additions & 2 deletions src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,13 @@ impl PackageSet {
}

pub fn pop(&mut self) -> Package {
self.packages.pop().unwrap()
self.packages.pop().expect("PackageSet.pop: empty set")
}

/// Get a package by name out of the set
pub fn get<'a>(&'a self, name: &str) -> &'a Package {
self.packages.iter().find(|pkg| name == pkg.get_name()).unwrap()
self.packages.iter().find(|pkg| name == pkg.get_name())
.expect("PackageSet.get: empty set")
}

pub fn get_all<'a>(&'a self, names: &[&str]) -> Vec<&'a Package> {
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/ops/cargo_rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ fn compile_custom(pkg: &Package, cmd: &str, cx: &Context) -> CargoResult<()> {
let mut cmd = cmd.split(' ');
let mut p = util::process(cmd.next().unwrap())
.cwd(pkg.get_root())
.env("OUT_DIR", Some(cx.dest.as_str().unwrap()))
.env("OUT_DIR", Some(cx.dest.as_str().expect("non-UTF8 dest path")))
.env("DEPS_DIR", Some(cx.dest.join(cx.deps_dir)
.as_str().unwrap()));
.as_str().expect("non-UTF8 deps path")));
for arg in cmd {
p = p.arg(arg);
}
Expand Down
22 changes: 10 additions & 12 deletions src/cargo/util/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io,fmt,os};
use std::{io,fmt,os, result};
use std::collections::HashMap;
use serialize::{Encodable,Encoder};
use toml;
Expand Down Expand Up @@ -231,17 +231,15 @@ fn merge_array(existing: &mut ConfigValue, val: &[toml::Value],
match existing.value {
String(_) => Err(internal("should be an Array, but it was a String")),
List(ref mut list) => {
let new_list: Vec<CargoResult<String>> =
val.iter().map(toml_string).collect();
if new_list.iter().any(|v| v.is_err()) {
return Err(internal("should be an Array of Strings, but \
was an Array of other values"));
} else {
let new_list: Vec<String> =
new_list.move_iter().map(|v| v.unwrap()).collect();
list.push_all(new_list.as_slice());
existing.path.push(path.clone());
Ok(())
let r: CargoResult<Vec<String>> = result::collect(val.iter().map(toml_string));
match r {
Err(_) => Err(internal("should be an Array of Strings, but \
was an Array of other values")),
Ok(new_list) => {
list.push_all(new_list.as_slice());
existing.path.push(path.clone());
Ok(())
}
}
}
}
Expand Down

0 comments on commit 3543133

Please sign in to comment.