Skip to content

Commit b4a1e59

Browse files
author
Oliver Schneider
committed
don't use Result::ok just to be able to use unwrap/unwrap_or
1 parent 0084f92 commit b4a1e59

File tree

21 files changed

+62
-53
lines changed

21 files changed

+62
-53
lines changed

src/libcore/result.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
//! let bad_result: Result<int, int> = bad_result.or_else(|i| Ok(11));
7979
//!
8080
//! // Consume the result and return the contents with `unwrap`.
81-
//! let final_awesome_result = good_result.ok().unwrap();
81+
//! let final_awesome_result = good_result.unwrap();
8282
//! ```
8383
//!
8484
//! # Results must be used
@@ -460,7 +460,7 @@ impl<T, E> Result<T, E> {
460460
/// line.trim_right().parse::<int>().unwrap_or(0)
461461
/// });
462462
/// // Add the value if there were no errors, otherwise add 0
463-
/// sum += val.ok().unwrap_or(0);
463+
/// sum += val.unwrap_or(0);
464464
/// }
465465
///
466466
/// assert!(sum == 10);

src/librustc/middle/astencode.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use std::io::SeekFrom;
4343
use std::io::prelude::*;
4444
use std::num::FromPrimitive;
4545
use std::rc::Rc;
46+
use std::fmt::Debug;
4647

4748
use rbml::reader;
4849
use rbml::writer::Encoder;
@@ -298,9 +299,11 @@ trait def_id_encoder_helpers {
298299
fn emit_def_id(&mut self, did: ast::DefId);
299300
}
300301

301-
impl<S:serialize::Encoder> def_id_encoder_helpers for S {
302+
impl<S:serialize::Encoder> def_id_encoder_helpers for S
303+
where <S as serialize::serialize::Encoder>::Error: Debug
304+
{
302305
fn emit_def_id(&mut self, did: ast::DefId) {
303-
did.encode(self).ok().unwrap()
306+
did.encode(self).unwrap()
304307
}
305308
}
306309

@@ -310,15 +313,18 @@ trait def_id_decoder_helpers {
310313
cdata: &cstore::crate_metadata) -> ast::DefId;
311314
}
312315

313-
impl<D:serialize::Decoder> def_id_decoder_helpers for D {
316+
impl<D:serialize::Decoder> def_id_decoder_helpers for D
317+
where <D as serialize::serialize::Decoder>::Error: Debug
318+
{
314319
fn read_def_id(&mut self, dcx: &DecodeContext) -> ast::DefId {
315-
let did: ast::DefId = Decodable::decode(self).ok().unwrap();
320+
let did: ast::DefId = Decodable::decode(self).unwrap();
316321
did.tr(dcx)
317322
}
318323

319324
fn read_def_id_nodcx(&mut self,
320-
cdata: &cstore::crate_metadata) -> ast::DefId {
321-
let did: ast::DefId = Decodable::decode(self).ok().unwrap();
325+
cdata: &cstore::crate_metadata)
326+
-> ast::DefId {
327+
let did: ast::DefId = Decodable::decode(self).unwrap();
322328
decoder::translate_def_id(cdata, did)
323329
}
324330
}
@@ -1769,7 +1775,7 @@ impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> {
17691775
fn read_closure_kind<'b, 'c>(&mut self, _dcx: &DecodeContext<'b, 'c, 'tcx>)
17701776
-> ty::ClosureKind
17711777
{
1772-
Decodable::decode(self).ok().unwrap()
1778+
Decodable::decode(self).unwrap()
17731779
}
17741780

17751781
fn read_closure_ty<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)

src/librustc/middle/cfg/graphviz.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ fn replace_newline_with_backslash_l(s: String) -> String {
5454
}
5555

5656
impl<'a, 'ast> dot::Labeller<'a, Node<'a>, Edge<'a>> for LabelledCFG<'a, 'ast> {
57-
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new(&self.name[..]).ok().unwrap() }
57+
fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new(&self.name[..]).unwrap() }
5858

5959
fn node_id(&'a self, &(i,_): &Node<'a>) -> dot::Id<'a> {
60-
dot::Id::new(format!("N{}", i.node_id())).ok().unwrap()
60+
dot::Id::new(format!("N{}", i.node_id())).unwrap()
6161
}
6262

6363
fn node_label(&'a self, &(i, n): &Node<'a>) -> dot::LabelText<'a> {

src/librustc/middle/infer/region_inference/graphviz.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ pub fn maybe_print_constraints_for<'a, 'tcx>(region_vars: &RegionVarBindings<'a,
6969
return;
7070
}
7171

72-
let requested_output = env::var("RUST_REGION_GRAPH").ok();
72+
let requested_output = env::var("RUST_REGION_GRAPH");
7373
debug!("requested_output: {:?} requested_node: {:?}",
7474
requested_output, requested_node);
7575

7676
let output_path = {
7777
let output_template = match requested_output {
78-
Some(ref s) if &**s == "help" => {
78+
Ok(ref s) if &**s == "help" => {
7979
static PRINTED_YET: AtomicBool = ATOMIC_BOOL_INIT;
8080
if !PRINTED_YET.load(Ordering::SeqCst) {
8181
print_help_message();
@@ -84,8 +84,8 @@ pub fn maybe_print_constraints_for<'a, 'tcx>(region_vars: &RegionVarBindings<'a,
8484
return;
8585
}
8686

87-
Some(other_path) => other_path,
88-
None => "/tmp/constraints.node%.dot".to_string(),
87+
Ok(other_path) => other_path,
88+
Err(_) => "/tmp/constraints.node%.dot".to_string(),
8989
};
9090

9191
if output_template.len() == 0 {
@@ -171,7 +171,7 @@ impl<'a, 'tcx> ConstraintGraph<'a, 'tcx> {
171171

172172
impl<'a, 'tcx> dot::Labeller<'a, Node, Edge> for ConstraintGraph<'a, 'tcx> {
173173
fn graph_id(&self) -> dot::Id {
174-
dot::Id::new(&*self.graph_name).ok().unwrap()
174+
dot::Id::new(&*self.graph_name).unwrap()
175175
}
176176
fn node_id(&self, n: &Node) -> dot::Id {
177177
let node_id = match self.node_ids.get(n) {

src/librustc_borrowck/borrowck/gather_loans/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for StaticInitializerCtxt<'a, 'tcx> {
491491
if let ast::ExprAddrOf(mutbl, ref base) = ex.node {
492492
let param_env = ty::empty_parameter_environment(self.bccx.tcx);
493493
let mc = mc::MemCategorizationContext::new(&param_env);
494-
let base_cmt = mc.cat_expr(&**base).ok().unwrap();
494+
let base_cmt = mc.cat_expr(&**base).unwrap();
495495
let borrow_kind = ty::BorrowKind::from_mutbl(mutbl);
496496
// Check that we don't allow borrows of unsafe static items.
497497
if check_aliasability(self.bccx, ex.span, euv::AddrOf,

src/libstd/fs/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,7 @@ mod tests {
15321532
#[test]
15331533
fn binary_file() {
15341534
let mut bytes = [0; 1024];
1535-
StdRng::new().ok().unwrap().fill_bytes(&mut bytes);
1535+
StdRng::new().unwrap().fill_bytes(&mut bytes);
15361536

15371537
let tmpdir = tmpdir();
15381538

src/libstd/io/cursor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ mod tests {
277277
fn read_to_end() {
278278
let mut reader = Cursor::new(vec!(0, 1, 2, 3, 4, 5, 6, 7));
279279
let mut v = Vec::new();
280-
reader.read_to_end(&mut v).ok().unwrap();
280+
reader.read_to_end(&mut v).unwrap();
281281
assert_eq!(v, [0, 1, 2, 3, 4, 5, 6, 7]);
282282
}
283283

src/libstd/net/addr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ mod tests {
617617
unique_local: bool, global: bool,
618618
u_link_local: bool, u_site_local: bool, u_global: bool,
619619
m_scope: Option<Ipv6MulticastScope>) {
620-
let ip: Ipv6Addr = str_addr.parse().ok().unwrap();
620+
let ip: Ipv6Addr = str_addr.parse().unwrap();
621621
assert_eq!(str_addr, ip.to_string());
622622

623623
assert_eq!(ip.is_unspecified(), unspec);

src/libstd/old_io/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1608,7 +1608,7 @@ mod test {
16081608
use rand::{StdRng, Rng};
16091609

16101610
let mut bytes = [0; 1024];
1611-
StdRng::new().ok().unwrap().fill_bytes(&mut bytes);
1611+
StdRng::new().unwrap().fill_bytes(&mut bytes);
16121612

16131613
let tmpdir = tmpdir();
16141614

src/libstd/sync/mpsc/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ impl<T: Send> Sender<T> {
592592
// asleep (we're looking at it), so the receiver
593593
// can't go away.
594594
(*a.get()).send(t).ok().unwrap();
595-
token.signal();
595+
token.signal();
596596
(a, Ok(()))
597597
}
598598
}

src/libstd/sys/unix/backtrace.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ fn output(w: &mut Write, idx: int, addr: *mut libc::c_void,
454454
#[allow(dead_code)]
455455
fn output_fileline(w: &mut Write, file: &[u8], line: libc::c_int,
456456
more: bool) -> io::Result<()> {
457-
let file = str::from_utf8(file).ok().unwrap_or("<unknown>");
457+
let file = str::from_utf8(file).unwrap_or("<unknown>");
458458
// prior line: " ##: {:2$} - func"
459459
try!(write!(w, " {:3$}at {}:{}", "", file, line, HEX_WIDTH));
460460
if more {

src/libstd/sys/unix/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ mod tests {
391391
let mut reader = FileDesc::new(reader, true);
392392
let mut writer = FileDesc::new(writer, true);
393393

394-
writer.write(b"test").ok().unwrap();
394+
writer.write(b"test").unwrap();
395395
let mut buf = [0; 4];
396396
match reader.read(&mut buf) {
397397
Ok(4) => {

src/libstd/sys/unix/helper_signal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn new() -> (signal, signal) {
2323
}
2424

2525
pub fn signal(fd: libc::c_int) {
26-
FileDesc::new(fd, false).write(&[0]).ok().unwrap();
26+
FileDesc::new(fd, false).write(&[0]).unwrap();
2727
}
2828

2929
pub fn close(fd: libc::c_int) {

src/libstd/sys/unix/timer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ fn helper(input: libc::c_int, messages: Receiver<Req>, _: ()) {
197197

198198
// drain the file descriptor
199199
let mut buf = [0];
200-
assert_eq!(fd.read(&mut buf).ok().unwrap(), 1);
200+
assert_eq!(fd.read(&mut buf).unwrap(), 1);
201201
}
202202

203203
-1 if os::errno() == libc::EINTR as i32 => {}

src/libstd/sys/windows/pipe.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -757,10 +757,10 @@ impl UnixAcceptor {
757757

758758
impl Clone for UnixAcceptor {
759759
fn clone(&self) -> UnixAcceptor {
760-
let name = to_utf16(&self.listener.name).ok().unwrap();
760+
let name = to_utf16(&self.listener.name).unwrap();
761761
UnixAcceptor {
762762
inner: self.inner.clone(),
763-
event: Event::new(true, false).ok().unwrap(),
763+
event: Event::new(true, false).unwrap(),
764764
deadline: 0,
765765
listener: UnixListener {
766766
name: self.listener.name.clone(),

src/libstd/thread.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ mod test {
869869
Err(e) => {
870870
type T = &'static str;
871871
assert!(e.is::<T>());
872-
assert_eq!(*e.downcast::<T>().ok().unwrap(), "static string");
872+
assert_eq!(*e.downcast::<T>().unwrap(), "static string");
873873
}
874874
Ok(()) => panic!()
875875
}
@@ -883,7 +883,7 @@ mod test {
883883
Err(e) => {
884884
type T = String;
885885
assert!(e.is::<T>());
886-
assert_eq!(*e.downcast::<T>().ok().unwrap(), "owned string".to_string());
886+
assert_eq!(*e.downcast::<T>().unwrap(), "owned string".to_string());
887887
}
888888
Ok(()) => panic!()
889889
}
@@ -897,9 +897,9 @@ mod test {
897897
Err(e) => {
898898
type T = Box<Any + Send>;
899899
assert!(e.is::<T>());
900-
let any = e.downcast::<T>().ok().unwrap();
900+
let any = e.downcast::<T>().unwrap();
901901
assert!(any.is::<u16>());
902-
assert_eq!(*any.downcast::<u16>().ok().unwrap(), 413);
902+
assert_eq!(*any.downcast::<u16>().unwrap(), 413);
903903
}
904904
Ok(()) => panic!()
905905
}

src/libsyntax/ext/deriving/encodable.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
//! fn encode(&self, s: &mut S) -> Result<(), E> {
6565
//! s.emit_struct("Spanned", 2, |this| {
6666
//! this.emit_struct_field("node", 0, |this| self.node.encode(this))
67-
//! .ok().unwrap();
67+
//! .unwrap();
6868
//! this.emit_struct_field("span", 1, |this| self.span.encode(this))
6969
//! })
7070
//! }
@@ -79,9 +79,9 @@
7979
//! d.read_struct("Spanned", 2, |this| {
8080
//! Ok(Spanned {
8181
//! node: this.read_struct_field("node", 0, |this| Decodable::decode(this))
82-
//! .ok().unwrap(),
82+
//! .unwrap(),
8383
//! span: this.read_struct_field("span", 1, |this| Decodable::decode(this))
84-
//! .ok().unwrap(),
84+
//! .unwrap(),
8585
//! })
8686
//! })
8787
//! }

src/libsyntax/parse/lexer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ impl<'a> StringReader<'a> {
623623
// find the integer representing the name
624624
self.scan_digits(base);
625625
let encoded_name : u32 = self.with_str_from(start_bpos, |s| {
626-
num::from_str_radix(s, 10).ok().unwrap_or_else(|| {
626+
num::from_str_radix(s, 10).unwrap_or_else(|_| {
627627
panic!("expected digits representing a name, got {:?}, {}, range [{:?},{:?}]",
628628
s, whence, start_bpos, self.last_pos);
629629
})
@@ -641,7 +641,7 @@ impl<'a> StringReader<'a> {
641641
let start_bpos = self.last_pos;
642642
self.scan_digits(base);
643643
let encoded_ctxt : ast::SyntaxContext = self.with_str_from(start_bpos, |s| {
644-
num::from_str_radix(s, 10).ok().unwrap_or_else(|| {
644+
num::from_str_radix(s, 10).unwrap_or_else(|_| {
645645
panic!("expected digits representing a ctxt, got {:?}, {}", s, whence);
646646
})
647647
});

src/libterm/terminfo/mod.rs

+17-14
Original file line numberDiff line numberDiff line change
@@ -181,21 +181,24 @@ impl<T: Write+Send+'static> TerminfoTerminal<T> {
181181
}
182182
};
183183

184-
let entry = open(&term[..]);
185-
if entry.is_err() {
186-
if env::var("MSYSCON").ok().map_or(false, |s| {
187-
"mintty.exe" == s
188-
}) {
189-
// msys terminal
190-
return Some(box TerminfoTerminal {out: out,
191-
ti: msys_terminfo(),
192-
num_colors: 8} as Box<Terminal<T>+Send>);
193-
}
194-
debug!("error finding terminfo entry: {:?}", entry.err().unwrap());
195-
return None;
196-
}
184+
let mut file = match open(&term[..]) {
185+
Ok(f) => f,
186+
Err(err) => return match env::var("MSYSCON") {
187+
Ok(ref val) if &val[..] == "mintty.exe" => {
188+
// msys terminal
189+
Some(box TerminfoTerminal{
190+
out: out,
191+
ti: msys_terminfo(),
192+
num_colors: 8,
193+
} as Box<Terminal<T>+Send>)
194+
},
195+
_ => {
196+
debug!("error finding terminfo entry: {:?}", err);
197+
None
198+
},
199+
},
200+
};
197201

198-
let mut file = entry.unwrap();
199202
let ti = parse(&mut file, false);
200203
if ti.is_err() {
201204
debug!("error parsing terminfo entry: {:?}", ti.err().unwrap());

src/test/run-pass/spawn2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::thread;
1212

1313
pub fn main() {
1414
let t = thread::spawn(move|| child((10, 20, 30, 40, 50, 60, 70, 80, 90)) );
15-
t.join().ok().unwrap();
15+
t.join().ok().unwrap(); // forget Err value, since it doesn't implement Debug
1616
}
1717

1818
fn child(args: (int, int, int, int, int, int, int, int, int)) {

src/test/run-pass/unit-like-struct-drop-run.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ pub fn main() {
2626
let _b = Foo;
2727
}).join();
2828

29-
let s = x.err().unwrap().downcast::<&'static str>().ok().unwrap();
29+
let s = x.err().unwrap().downcast::<&'static str>().unwrap();
3030
assert_eq!(&**s, "This panic should happen.");
3131
}

0 commit comments

Comments
 (0)