From b4a1e59146c70a12d8c4c1f85c08a2ecf9fb3d2e Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 20 Mar 2015 08:19:13 +0100 Subject: [PATCH] don't use Result::ok just to be able to use unwrap/unwrap_or --- src/libcore/result.rs | 4 +-- src/librustc/middle/astencode.rs | 20 +++++++----- src/librustc/middle/cfg/graphviz.rs | 4 +-- .../middle/infer/region_inference/graphviz.rs | 10 +++--- .../borrowck/gather_loans/mod.rs | 2 +- src/libstd/fs/mod.rs | 2 +- src/libstd/io/cursor.rs | 2 +- src/libstd/net/addr.rs | 2 +- src/libstd/old_io/fs.rs | 2 +- src/libstd/sync/mpsc/mod.rs | 2 +- src/libstd/sys/unix/backtrace.rs | 2 +- src/libstd/sys/unix/fs.rs | 2 +- src/libstd/sys/unix/helper_signal.rs | 2 +- src/libstd/sys/unix/timer.rs | 2 +- src/libstd/sys/windows/pipe.rs | 4 +-- src/libstd/thread.rs | 8 ++--- src/libsyntax/ext/deriving/encodable.rs | 6 ++-- src/libsyntax/parse/lexer/mod.rs | 4 +-- src/libterm/terminfo/mod.rs | 31 ++++++++++--------- src/test/run-pass/spawn2.rs | 2 +- .../run-pass/unit-like-struct-drop-run.rs | 2 +- 21 files changed, 62 insertions(+), 53 deletions(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index a5b2fddfd5d..8dd91fd6500 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -78,7 +78,7 @@ //! let bad_result: Result = bad_result.or_else(|i| Ok(11)); //! //! // Consume the result and return the contents with `unwrap`. -//! let final_awesome_result = good_result.ok().unwrap(); +//! let final_awesome_result = good_result.unwrap(); //! ``` //! //! # Results must be used @@ -460,7 +460,7 @@ impl Result { /// line.trim_right().parse::().unwrap_or(0) /// }); /// // Add the value if there were no errors, otherwise add 0 - /// sum += val.ok().unwrap_or(0); + /// sum += val.unwrap_or(0); /// } /// /// assert!(sum == 10); diff --git a/src/librustc/middle/astencode.rs b/src/librustc/middle/astencode.rs index f7210728bb4..faadf669a43 100644 --- a/src/librustc/middle/astencode.rs +++ b/src/librustc/middle/astencode.rs @@ -43,6 +43,7 @@ use std::io::SeekFrom; use std::io::prelude::*; use std::num::FromPrimitive; use std::rc::Rc; +use std::fmt::Debug; use rbml::reader; use rbml::writer::Encoder; @@ -298,9 +299,11 @@ trait def_id_encoder_helpers { fn emit_def_id(&mut self, did: ast::DefId); } -impl def_id_encoder_helpers for S { +impl def_id_encoder_helpers for S + where ::Error: Debug +{ fn emit_def_id(&mut self, did: ast::DefId) { - did.encode(self).ok().unwrap() + did.encode(self).unwrap() } } @@ -310,15 +313,18 @@ trait def_id_decoder_helpers { cdata: &cstore::crate_metadata) -> ast::DefId; } -impl def_id_decoder_helpers for D { +impl def_id_decoder_helpers for D + where ::Error: Debug +{ fn read_def_id(&mut self, dcx: &DecodeContext) -> ast::DefId { - let did: ast::DefId = Decodable::decode(self).ok().unwrap(); + let did: ast::DefId = Decodable::decode(self).unwrap(); did.tr(dcx) } fn read_def_id_nodcx(&mut self, - cdata: &cstore::crate_metadata) -> ast::DefId { - let did: ast::DefId = Decodable::decode(self).ok().unwrap(); + cdata: &cstore::crate_metadata) + -> ast::DefId { + let did: ast::DefId = Decodable::decode(self).unwrap(); decoder::translate_def_id(cdata, did) } } @@ -1769,7 +1775,7 @@ impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> { fn read_closure_kind<'b, 'c>(&mut self, _dcx: &DecodeContext<'b, 'c, 'tcx>) -> ty::ClosureKind { - Decodable::decode(self).ok().unwrap() + Decodable::decode(self).unwrap() } fn read_closure_ty<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>) diff --git a/src/librustc/middle/cfg/graphviz.rs b/src/librustc/middle/cfg/graphviz.rs index ac6524dad80..3a6966ab112 100644 --- a/src/librustc/middle/cfg/graphviz.rs +++ b/src/librustc/middle/cfg/graphviz.rs @@ -54,10 +54,10 @@ fn replace_newline_with_backslash_l(s: String) -> String { } impl<'a, 'ast> dot::Labeller<'a, Node<'a>, Edge<'a>> for LabelledCFG<'a, 'ast> { - fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new(&self.name[..]).ok().unwrap() } + fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new(&self.name[..]).unwrap() } fn node_id(&'a self, &(i,_): &Node<'a>) -> dot::Id<'a> { - dot::Id::new(format!("N{}", i.node_id())).ok().unwrap() + dot::Id::new(format!("N{}", i.node_id())).unwrap() } fn node_label(&'a self, &(i, n): &Node<'a>) -> dot::LabelText<'a> { diff --git a/src/librustc/middle/infer/region_inference/graphviz.rs b/src/librustc/middle/infer/region_inference/graphviz.rs index 6211fde7919..5be3310926c 100644 --- a/src/librustc/middle/infer/region_inference/graphviz.rs +++ b/src/librustc/middle/infer/region_inference/graphviz.rs @@ -69,13 +69,13 @@ pub fn maybe_print_constraints_for<'a, 'tcx>(region_vars: &RegionVarBindings<'a, return; } - let requested_output = env::var("RUST_REGION_GRAPH").ok(); + let requested_output = env::var("RUST_REGION_GRAPH"); debug!("requested_output: {:?} requested_node: {:?}", requested_output, requested_node); let output_path = { let output_template = match requested_output { - Some(ref s) if &**s == "help" => { + Ok(ref s) if &**s == "help" => { static PRINTED_YET: AtomicBool = ATOMIC_BOOL_INIT; if !PRINTED_YET.load(Ordering::SeqCst) { print_help_message(); @@ -84,8 +84,8 @@ pub fn maybe_print_constraints_for<'a, 'tcx>(region_vars: &RegionVarBindings<'a, return; } - Some(other_path) => other_path, - None => "/tmp/constraints.node%.dot".to_string(), + Ok(other_path) => other_path, + Err(_) => "/tmp/constraints.node%.dot".to_string(), }; if output_template.len() == 0 { @@ -171,7 +171,7 @@ impl<'a, 'tcx> ConstraintGraph<'a, 'tcx> { impl<'a, 'tcx> dot::Labeller<'a, Node, Edge> for ConstraintGraph<'a, 'tcx> { fn graph_id(&self) -> dot::Id { - dot::Id::new(&*self.graph_name).ok().unwrap() + dot::Id::new(&*self.graph_name).unwrap() } fn node_id(&self, n: &Node) -> dot::Id { let node_id = match self.node_ids.get(n) { diff --git a/src/librustc_borrowck/borrowck/gather_loans/mod.rs b/src/librustc_borrowck/borrowck/gather_loans/mod.rs index d51ad6e0c97..7d77eb23b6e 100644 --- a/src/librustc_borrowck/borrowck/gather_loans/mod.rs +++ b/src/librustc_borrowck/borrowck/gather_loans/mod.rs @@ -491,7 +491,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for StaticInitializerCtxt<'a, 'tcx> { if let ast::ExprAddrOf(mutbl, ref base) = ex.node { let param_env = ty::empty_parameter_environment(self.bccx.tcx); let mc = mc::MemCategorizationContext::new(¶m_env); - let base_cmt = mc.cat_expr(&**base).ok().unwrap(); + let base_cmt = mc.cat_expr(&**base).unwrap(); let borrow_kind = ty::BorrowKind::from_mutbl(mutbl); // Check that we don't allow borrows of unsafe static items. if check_aliasability(self.bccx, ex.span, euv::AddrOf, diff --git a/src/libstd/fs/mod.rs b/src/libstd/fs/mod.rs index ba89b3a0ea6..b02f68c1630 100644 --- a/src/libstd/fs/mod.rs +++ b/src/libstd/fs/mod.rs @@ -1532,7 +1532,7 @@ mod tests { #[test] fn binary_file() { let mut bytes = [0; 1024]; - StdRng::new().ok().unwrap().fill_bytes(&mut bytes); + StdRng::new().unwrap().fill_bytes(&mut bytes); let tmpdir = tmpdir(); diff --git a/src/libstd/io/cursor.rs b/src/libstd/io/cursor.rs index 365f5e37b0b..87e5a2a4488 100644 --- a/src/libstd/io/cursor.rs +++ b/src/libstd/io/cursor.rs @@ -277,7 +277,7 @@ mod tests { fn read_to_end() { let mut reader = Cursor::new(vec!(0, 1, 2, 3, 4, 5, 6, 7)); let mut v = Vec::new(); - reader.read_to_end(&mut v).ok().unwrap(); + reader.read_to_end(&mut v).unwrap(); assert_eq!(v, [0, 1, 2, 3, 4, 5, 6, 7]); } diff --git a/src/libstd/net/addr.rs b/src/libstd/net/addr.rs index 702f81db8e2..87241117043 100644 --- a/src/libstd/net/addr.rs +++ b/src/libstd/net/addr.rs @@ -617,7 +617,7 @@ mod tests { unique_local: bool, global: bool, u_link_local: bool, u_site_local: bool, u_global: bool, m_scope: Option) { - let ip: Ipv6Addr = str_addr.parse().ok().unwrap(); + let ip: Ipv6Addr = str_addr.parse().unwrap(); assert_eq!(str_addr, ip.to_string()); assert_eq!(ip.is_unspecified(), unspec); diff --git a/src/libstd/old_io/fs.rs b/src/libstd/old_io/fs.rs index 2df925beb36..980406b1ce9 100644 --- a/src/libstd/old_io/fs.rs +++ b/src/libstd/old_io/fs.rs @@ -1608,7 +1608,7 @@ mod test { use rand::{StdRng, Rng}; let mut bytes = [0; 1024]; - StdRng::new().ok().unwrap().fill_bytes(&mut bytes); + StdRng::new().unwrap().fill_bytes(&mut bytes); let tmpdir = tmpdir(); diff --git a/src/libstd/sync/mpsc/mod.rs b/src/libstd/sync/mpsc/mod.rs index 01eeed4fb54..123dad978f5 100644 --- a/src/libstd/sync/mpsc/mod.rs +++ b/src/libstd/sync/mpsc/mod.rs @@ -592,7 +592,7 @@ impl Sender { // asleep (we're looking at it), so the receiver // can't go away. (*a.get()).send(t).ok().unwrap(); - token.signal(); + token.signal(); (a, Ok(())) } } diff --git a/src/libstd/sys/unix/backtrace.rs b/src/libstd/sys/unix/backtrace.rs index 74ab04978cb..7db64cfb936 100644 --- a/src/libstd/sys/unix/backtrace.rs +++ b/src/libstd/sys/unix/backtrace.rs @@ -454,7 +454,7 @@ fn output(w: &mut Write, idx: int, addr: *mut libc::c_void, #[allow(dead_code)] fn output_fileline(w: &mut Write, file: &[u8], line: libc::c_int, more: bool) -> io::Result<()> { - let file = str::from_utf8(file).ok().unwrap_or(""); + let file = str::from_utf8(file).unwrap_or(""); // prior line: " ##: {:2$} - func" try!(write!(w, " {:3$}at {}:{}", "", file, line, HEX_WIDTH)); if more { diff --git a/src/libstd/sys/unix/fs.rs b/src/libstd/sys/unix/fs.rs index c839ce65298..a3d495f82ef 100644 --- a/src/libstd/sys/unix/fs.rs +++ b/src/libstd/sys/unix/fs.rs @@ -391,7 +391,7 @@ mod tests { let mut reader = FileDesc::new(reader, true); let mut writer = FileDesc::new(writer, true); - writer.write(b"test").ok().unwrap(); + writer.write(b"test").unwrap(); let mut buf = [0; 4]; match reader.read(&mut buf) { Ok(4) => { diff --git a/src/libstd/sys/unix/helper_signal.rs b/src/libstd/sys/unix/helper_signal.rs index ff29dea254f..17c8b21f8b3 100644 --- a/src/libstd/sys/unix/helper_signal.rs +++ b/src/libstd/sys/unix/helper_signal.rs @@ -23,7 +23,7 @@ pub fn new() -> (signal, signal) { } pub fn signal(fd: libc::c_int) { - FileDesc::new(fd, false).write(&[0]).ok().unwrap(); + FileDesc::new(fd, false).write(&[0]).unwrap(); } pub fn close(fd: libc::c_int) { diff --git a/src/libstd/sys/unix/timer.rs b/src/libstd/sys/unix/timer.rs index ef0274fdda9..c830eb91068 100644 --- a/src/libstd/sys/unix/timer.rs +++ b/src/libstd/sys/unix/timer.rs @@ -197,7 +197,7 @@ fn helper(input: libc::c_int, messages: Receiver, _: ()) { // drain the file descriptor let mut buf = [0]; - assert_eq!(fd.read(&mut buf).ok().unwrap(), 1); + assert_eq!(fd.read(&mut buf).unwrap(), 1); } -1 if os::errno() == libc::EINTR as i32 => {} diff --git a/src/libstd/sys/windows/pipe.rs b/src/libstd/sys/windows/pipe.rs index 2b03e9e7431..17fdd6755c6 100644 --- a/src/libstd/sys/windows/pipe.rs +++ b/src/libstd/sys/windows/pipe.rs @@ -757,10 +757,10 @@ impl UnixAcceptor { impl Clone for UnixAcceptor { fn clone(&self) -> UnixAcceptor { - let name = to_utf16(&self.listener.name).ok().unwrap(); + let name = to_utf16(&self.listener.name).unwrap(); UnixAcceptor { inner: self.inner.clone(), - event: Event::new(true, false).ok().unwrap(), + event: Event::new(true, false).unwrap(), deadline: 0, listener: UnixListener { name: self.listener.name.clone(), diff --git a/src/libstd/thread.rs b/src/libstd/thread.rs index d2742d4b45a..99414c771f0 100644 --- a/src/libstd/thread.rs +++ b/src/libstd/thread.rs @@ -869,7 +869,7 @@ mod test { Err(e) => { type T = &'static str; assert!(e.is::()); - assert_eq!(*e.downcast::().ok().unwrap(), "static string"); + assert_eq!(*e.downcast::().unwrap(), "static string"); } Ok(()) => panic!() } @@ -883,7 +883,7 @@ mod test { Err(e) => { type T = String; assert!(e.is::()); - assert_eq!(*e.downcast::().ok().unwrap(), "owned string".to_string()); + assert_eq!(*e.downcast::().unwrap(), "owned string".to_string()); } Ok(()) => panic!() } @@ -897,9 +897,9 @@ mod test { Err(e) => { type T = Box; assert!(e.is::()); - let any = e.downcast::().ok().unwrap(); + let any = e.downcast::().unwrap(); assert!(any.is::()); - assert_eq!(*any.downcast::().ok().unwrap(), 413); + assert_eq!(*any.downcast::().unwrap(), 413); } Ok(()) => panic!() } diff --git a/src/libsyntax/ext/deriving/encodable.rs b/src/libsyntax/ext/deriving/encodable.rs index d7961d7da00..06255f46779 100644 --- a/src/libsyntax/ext/deriving/encodable.rs +++ b/src/libsyntax/ext/deriving/encodable.rs @@ -64,7 +64,7 @@ //! fn encode(&self, s: &mut S) -> Result<(), E> { //! s.emit_struct("Spanned", 2, |this| { //! this.emit_struct_field("node", 0, |this| self.node.encode(this)) -//! .ok().unwrap(); +//! .unwrap(); //! this.emit_struct_field("span", 1, |this| self.span.encode(this)) //! }) //! } @@ -79,9 +79,9 @@ //! d.read_struct("Spanned", 2, |this| { //! Ok(Spanned { //! node: this.read_struct_field("node", 0, |this| Decodable::decode(this)) -//! .ok().unwrap(), +//! .unwrap(), //! span: this.read_struct_field("span", 1, |this| Decodable::decode(this)) -//! .ok().unwrap(), +//! .unwrap(), //! }) //! }) //! } diff --git a/src/libsyntax/parse/lexer/mod.rs b/src/libsyntax/parse/lexer/mod.rs index bb8f9da8917..e0953a8ace6 100644 --- a/src/libsyntax/parse/lexer/mod.rs +++ b/src/libsyntax/parse/lexer/mod.rs @@ -623,7 +623,7 @@ impl<'a> StringReader<'a> { // find the integer representing the name self.scan_digits(base); let encoded_name : u32 = self.with_str_from(start_bpos, |s| { - num::from_str_radix(s, 10).ok().unwrap_or_else(|| { + num::from_str_radix(s, 10).unwrap_or_else(|_| { panic!("expected digits representing a name, got {:?}, {}, range [{:?},{:?}]", s, whence, start_bpos, self.last_pos); }) @@ -641,7 +641,7 @@ impl<'a> StringReader<'a> { let start_bpos = self.last_pos; self.scan_digits(base); let encoded_ctxt : ast::SyntaxContext = self.with_str_from(start_bpos, |s| { - num::from_str_radix(s, 10).ok().unwrap_or_else(|| { + num::from_str_radix(s, 10).unwrap_or_else(|_| { panic!("expected digits representing a ctxt, got {:?}, {}", s, whence); }) }); diff --git a/src/libterm/terminfo/mod.rs b/src/libterm/terminfo/mod.rs index 3c269cc485d..309320b52ff 100644 --- a/src/libterm/terminfo/mod.rs +++ b/src/libterm/terminfo/mod.rs @@ -181,21 +181,24 @@ impl TerminfoTerminal { } }; - let entry = open(&term[..]); - if entry.is_err() { - if env::var("MSYSCON").ok().map_or(false, |s| { - "mintty.exe" == s - }) { - // msys terminal - return Some(box TerminfoTerminal {out: out, - ti: msys_terminfo(), - num_colors: 8} as Box+Send>); - } - debug!("error finding terminfo entry: {:?}", entry.err().unwrap()); - return None; - } + let mut file = match open(&term[..]) { + Ok(f) => f, + Err(err) => return match env::var("MSYSCON") { + Ok(ref val) if &val[..] == "mintty.exe" => { + // msys terminal + Some(box TerminfoTerminal{ + out: out, + ti: msys_terminfo(), + num_colors: 8, + } as Box+Send>) + }, + _ => { + debug!("error finding terminfo entry: {:?}", err); + None + }, + }, + }; - let mut file = entry.unwrap(); let ti = parse(&mut file, false); if ti.is_err() { debug!("error parsing terminfo entry: {:?}", ti.err().unwrap()); diff --git a/src/test/run-pass/spawn2.rs b/src/test/run-pass/spawn2.rs index 91edb5fd9c1..b808ea472a2 100644 --- a/src/test/run-pass/spawn2.rs +++ b/src/test/run-pass/spawn2.rs @@ -12,7 +12,7 @@ use std::thread; pub fn main() { let t = thread::spawn(move|| child((10, 20, 30, 40, 50, 60, 70, 80, 90)) ); - t.join().ok().unwrap(); + t.join().ok().unwrap(); // forget Err value, since it doesn't implement Debug } fn child(args: (int, int, int, int, int, int, int, int, int)) { diff --git a/src/test/run-pass/unit-like-struct-drop-run.rs b/src/test/run-pass/unit-like-struct-drop-run.rs index ac46187f03a..66cc8d658c0 100644 --- a/src/test/run-pass/unit-like-struct-drop-run.rs +++ b/src/test/run-pass/unit-like-struct-drop-run.rs @@ -26,6 +26,6 @@ pub fn main() { let _b = Foo; }).join(); - let s = x.err().unwrap().downcast::<&'static str>().ok().unwrap(); + let s = x.err().unwrap().downcast::<&'static str>().unwrap(); assert_eq!(&**s, "This panic should happen."); }