Skip to content

Commit 3543133

Browse files
committed
Remove/refactor some .unwraps.
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`.
1 parent 2f0ac4a commit 3543133

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

src/bin/cargo-verify-project.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ fn main() {
2626
}
2727
};
2828

29-
if !matches.opt_present("m") {
30-
fail("missing-argument", "manifest");
31-
return;
32-
}
33-
34-
let manifest = matches.opt_str("m").unwrap();
29+
let manifest = match matches.opt_str("m") {
30+
Some(m) => m,
31+
None => {
32+
fail("missing-argument", "manifest");
33+
return;
34+
}
35+
};
3536
let file = Path::new(manifest);
3637
let contents = match File::open(&file).read_to_str() {
3738
Ok(s) => s,

src/cargo/core/package.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,13 @@ impl PackageSet {
155155
}
156156

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

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

166167
pub fn get_all<'a>(&'a self, names: &[&str]) -> Vec<&'a Package> {

src/cargo/ops/cargo_rustc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ fn compile_custom(pkg: &Package, cmd: &str, cx: &Context) -> CargoResult<()> {
168168
let mut cmd = cmd.split(' ');
169169
let mut p = util::process(cmd.next().unwrap())
170170
.cwd(pkg.get_root())
171-
.env("OUT_DIR", Some(cx.dest.as_str().unwrap()))
171+
.env("OUT_DIR", Some(cx.dest.as_str().expect("non-UTF8 dest path")))
172172
.env("DEPS_DIR", Some(cx.dest.join(cx.deps_dir)
173-
.as_str().unwrap()));
173+
.as_str().expect("non-UTF8 deps path")));
174174
for arg in cmd {
175175
p = p.arg(arg);
176176
}

src/cargo/util/config.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{io,fmt,os};
1+
use std::{io,fmt,os, result};
22
use std::collections::HashMap;
33
use serialize::{Encodable,Encoder};
44
use toml;
@@ -231,17 +231,15 @@ fn merge_array(existing: &mut ConfigValue, val: &[toml::Value],
231231
match existing.value {
232232
String(_) => Err(internal("should be an Array, but it was a String")),
233233
List(ref mut list) => {
234-
let new_list: Vec<CargoResult<String>> =
235-
val.iter().map(toml_string).collect();
236-
if new_list.iter().any(|v| v.is_err()) {
237-
return Err(internal("should be an Array of Strings, but \
238-
was an Array of other values"));
239-
} else {
240-
let new_list: Vec<String> =
241-
new_list.move_iter().map(|v| v.unwrap()).collect();
242-
list.push_all(new_list.as_slice());
243-
existing.path.push(path.clone());
244-
Ok(())
234+
let r: CargoResult<Vec<String>> = result::collect(val.iter().map(toml_string));
235+
match r {
236+
Err(_) => Err(internal("should be an Array of Strings, but \
237+
was an Array of other values")),
238+
Ok(new_list) => {
239+
list.push_all(new_list.as_slice());
240+
existing.path.push(path.clone());
241+
Ok(())
242+
}
245243
}
246244
}
247245
}

0 commit comments

Comments
 (0)