Skip to content

Replace unwrap calls in examples by expect #51668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/libstd/collections/hash/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,9 +678,9 @@ impl<T, S> HashSet<T, S>
/// let mut set = HashSet::new();
/// set.insert(Vec::<i32>::new());
///
/// assert_eq!(set.get(&[][..]).unwrap().capacity(), 0);
/// assert_eq!(set.get(&[][..]).expect("get() failed").capacity(), 0);
/// set.replace(Vec::with_capacity(10));
/// assert_eq!(set.get(&[][..]).unwrap().capacity(), 10);
/// assert_eq!(set.get(&[][..]).expect("get() failed").capacity(), 10);
/// ```
#[stable(feature = "set_recovery", since = "1.9.0")]
pub fn replace(&mut self, value: T) -> Option<T> {
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/collections/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,10 @@
//! map.insert(Foo { a: 1, b: "xyz" }, 100);
//!
//! // The value has been updated...
//! assert_eq!(map.values().next().unwrap(), &100);
//! assert_eq!(map.values().next().expect("empty value: we reached the end"), &100);
//!
//! // ...but the key hasn't changed. b is still "baz", not "xyz".
//! assert_eq!(map.keys().next().unwrap().b, "baz");
//! assert_eq!(map.keys().next().expect("empty value: we reached the end").b, "baz");
//! ```
//!
//! [`Vec`]: ../../std/vec/struct.Vec.html
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub trait Error: Debug + Display {
/// match get_super_error() {
/// Err(e) => {
/// println!("Error: {}", e.description());
/// println!("Caused by: {}", e.cause().unwrap());
/// println!("Caused by: {}", e.cause().expect("no cause provided"));
/// }
/// _ => println!("No error"),
/// }
Expand Down
66 changes: 35 additions & 31 deletions src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ use sys;
/// }
///
/// // We are certain that our string doesn't have 0 bytes in the middle,
/// // so we can .unwrap()
/// let c_to_print = CString::new("Hello, world!").unwrap();
/// // so we can .expect()
/// let c_to_print = CString::new("Hello, world!").expect("CString::new failed");
/// unsafe {
/// my_printer(c_to_print.as_ptr());
/// }
Expand Down Expand Up @@ -174,7 +174,7 @@ pub struct CString {
/// unsafe { work_with(data.as_ptr()) }
/// }
///
/// let s = CString::new("data data data data").unwrap();
/// let s = CString::new("data data data data").expect("CString::new failed");
/// work(&s);
/// ```
///
Expand Down Expand Up @@ -313,7 +313,7 @@ impl CString {
///
/// extern { fn puts(s: *const c_char); }
///
/// let to_print = CString::new("Hello!").unwrap();
/// let to_print = CString::new("Hello!").expect("CString::new failed");
/// unsafe {
/// puts(to_print.as_ptr());
/// }
Expand Down Expand Up @@ -397,7 +397,7 @@ impl CString {
/// fn some_extern_function(s: *mut c_char);
/// }
///
/// let c_string = CString::new("Hello!").unwrap();
/// let c_string = CString::new("Hello!").expect("CString::new failed");
/// let raw = c_string.into_raw();
/// unsafe {
/// some_extern_function(raw);
Expand Down Expand Up @@ -427,7 +427,7 @@ impl CString {
/// ```
/// use std::ffi::CString;
///
/// let c_string = CString::new("foo").unwrap();
/// let c_string = CString::new("foo").expect("CString::new failed");
///
/// let ptr = c_string.into_raw();
///
Expand Down Expand Up @@ -459,12 +459,12 @@ impl CString {
/// use std::ffi::CString;
///
/// let valid_utf8 = vec![b'f', b'o', b'o'];
/// let cstring = CString::new(valid_utf8).unwrap();
/// assert_eq!(cstring.into_string().unwrap(), "foo");
/// let cstring = CString::new(valid_utf8).expect("CString::new failed");
/// assert_eq!(cstring.into_string().expect("into_string() call failed"), "foo");
///
/// let invalid_utf8 = vec![b'f', 0xff, b'o', b'o'];
/// let cstring = CString::new(invalid_utf8).unwrap();
/// let err = cstring.into_string().err().unwrap();
/// let cstring = CString::new(invalid_utf8).expect("CString::new failed");
/// let err = cstring.into_string().err().expect("into_string().err() failed");
/// assert_eq!(err.utf8_error().valid_up_to(), 1);
/// ```

Expand All @@ -488,7 +488,7 @@ impl CString {
/// ```
/// use std::ffi::CString;
///
/// let c_string = CString::new("foo").unwrap();
/// let c_string = CString::new("foo").expect("CString::new failed");
/// let bytes = c_string.into_bytes();
/// assert_eq!(bytes, vec![b'f', b'o', b'o']);
/// ```
Expand All @@ -510,7 +510,7 @@ impl CString {
/// ```
/// use std::ffi::CString;
///
/// let c_string = CString::new("foo").unwrap();
/// let c_string = CString::new("foo").expect("CString::new failed");
/// let bytes = c_string.into_bytes_with_nul();
/// assert_eq!(bytes, vec![b'f', b'o', b'o', b'\0']);
/// ```
Expand All @@ -533,7 +533,7 @@ impl CString {
/// ```
/// use std::ffi::CString;
///
/// let c_string = CString::new("foo").unwrap();
/// let c_string = CString::new("foo").expect("CString::new failed");
/// let bytes = c_string.as_bytes();
/// assert_eq!(bytes, &[b'f', b'o', b'o']);
/// ```
Expand All @@ -553,7 +553,7 @@ impl CString {
/// ```
/// use std::ffi::CString;
///
/// let c_string = CString::new("foo").unwrap();
/// let c_string = CString::new("foo").expect("CString::new failed");
/// let bytes = c_string.as_bytes_with_nul();
/// assert_eq!(bytes, &[b'f', b'o', b'o', b'\0']);
/// ```
Expand All @@ -572,9 +572,10 @@ impl CString {
/// ```
/// use std::ffi::{CString, CStr};
///
/// let c_string = CString::new(b"foo".to_vec()).unwrap();
/// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
/// let c_str = c_string.as_c_str();
/// assert_eq!(c_str, CStr::from_bytes_with_nul(b"foo\0").unwrap());
/// assert_eq!(c_str,
/// CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"));
/// ```
#[inline]
#[stable(feature = "as_c_str", since = "1.20.0")]
Expand All @@ -591,16 +592,17 @@ impl CString {
/// ```
/// use std::ffi::{CString, CStr};
///
/// let c_string = CString::new(b"foo".to_vec()).unwrap();
/// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
/// let boxed = c_string.into_boxed_c_str();
/// assert_eq!(&*boxed, CStr::from_bytes_with_nul(b"foo\0").unwrap());
/// assert_eq!(&*boxed,
/// CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed"));
/// ```
#[stable(feature = "into_boxed_c_str", since = "1.20.0")]
pub fn into_boxed_c_str(self) -> Box<CStr> {
unsafe { Box::from_raw(Box::into_raw(self.into_inner()) as *mut CStr) }
}

// Bypass "move out of struct which implements [`Drop`] trait" restriction.
/// Bypass "move out of struct which implements [`Drop`] trait" restriction.
///
/// [`Drop`]: ../ops/trait.Drop.html
fn into_inner(self) -> Box<[u8]> {
Expand Down Expand Up @@ -961,7 +963,7 @@ impl CStr {
///
/// unsafe {
/// let slice = CStr::from_ptr(my_string());
/// println!("string returned: {}", slice.to_str().unwrap());
/// println!("string returned: {}", slice.to_str().expect("to_str() call failed"));
/// }
/// # }
/// ```
Expand Down Expand Up @@ -1030,7 +1032,7 @@ impl CStr {
/// use std::ffi::{CStr, CString};
///
/// unsafe {
/// let cstring = CString::new("hello").unwrap();
/// let cstring = CString::new("hello").expect("CString::new failed");
/// let cstr = CStr::from_bytes_with_nul_unchecked(cstring.to_bytes_with_nul());
/// assert_eq!(cstr, &*cstring);
/// }
Expand All @@ -1057,7 +1059,7 @@ impl CStr {
/// # #![allow(unused_must_use)]
/// use std::ffi::{CString};
///
/// let ptr = CString::new("Hello").unwrap().as_ptr();
/// let ptr = CString::new("Hello").expect("CString::new failed").as_ptr();
/// unsafe {
/// // `ptr` is dangling
/// *ptr;
Expand All @@ -1066,14 +1068,14 @@ impl CStr {
///
/// This happens because the pointer returned by `as_ptr` does not carry any
/// lifetime information and the [`CString`] is deallocated immediately after
/// the `CString::new("Hello").unwrap().as_ptr()` expression is evaluated.
/// the `CString::new("Hello").expect("CString::new failed").as_ptr()` expression is evaluated.
/// To fix the problem, bind the `CString` to a local variable:
///
/// ```no_run
/// # #![allow(unused_must_use)]
/// use std::ffi::{CString};
///
/// let hello = CString::new("Hello").unwrap();
/// let hello = CString::new("Hello").expect("CString::new failed");
/// let ptr = hello.as_ptr();
/// unsafe {
/// // `ptr` is valid because `hello` is in scope
Expand Down Expand Up @@ -1105,7 +1107,7 @@ impl CStr {
/// ```
/// use std::ffi::CStr;
///
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap();
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
/// assert_eq!(c_str.to_bytes(), b"foo");
/// ```
#[inline]
Expand All @@ -1131,7 +1133,7 @@ impl CStr {
/// ```
/// use std::ffi::CStr;
///
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap();
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
/// assert_eq!(c_str.to_bytes_with_nul(), b"foo\0");
/// ```
#[inline]
Expand All @@ -1158,7 +1160,7 @@ impl CStr {
/// ```
/// use std::ffi::CStr;
///
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").unwrap();
/// let c_str = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul");
/// assert_eq!(c_str.to_str(), Ok("foo"));
/// ```
#[stable(feature = "cstr_to_str", since = "1.4.0")]
Expand Down Expand Up @@ -1198,7 +1200,8 @@ impl CStr {
/// use std::borrow::Cow;
/// use std::ffi::CStr;
///
/// let c_str = CStr::from_bytes_with_nul(b"Hello World\0").unwrap();
/// let c_str = CStr::from_bytes_with_nul(b"Hello World\0")
/// .expect("CStr::from_bytes_with_nul failed");
/// assert_eq!(c_str.to_string_lossy(), Cow::Borrowed("Hello World"));
/// ```
///
Expand All @@ -1208,7 +1211,8 @@ impl CStr {
/// use std::borrow::Cow;
/// use std::ffi::CStr;
///
/// let c_str = CStr::from_bytes_with_nul(b"Hello \xF0\x90\x80World\0").unwrap();
/// let c_str = CStr::from_bytes_with_nul(b"Hello \xF0\x90\x80World\0")
/// .expect("CStr::from_bytes_with_nul failed");
/// assert_eq!(
/// c_str.to_string_lossy(),
/// Cow::Owned(String::from("Hello �World")) as Cow<str>
Expand All @@ -1229,9 +1233,9 @@ impl CStr {
/// ```
/// use std::ffi::CString;
///
/// let c_string = CString::new(b"foo".to_vec()).unwrap();
/// let c_string = CString::new(b"foo".to_vec()).expect("CString::new failed");
/// let boxed = c_string.into_boxed_c_str();
/// assert_eq!(boxed.into_c_string(), CString::new("foo").unwrap());
/// assert_eq!(boxed.into_c_string(), CString::new("foo").expect("CString::new failed"));
/// ```
#[stable(feature = "into_boxed_c_str", since = "1.20.0")]
pub fn into_c_string(self: Box<CStr>) -> CString {
Expand Down
5 changes: 3 additions & 2 deletions src/libstd/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2023,9 +2023,10 @@ impl DirBuilder {
/// let path = "/tmp/foo/bar/baz";
/// DirBuilder::new()
/// .recursive(true)
/// .create(path).unwrap();
/// .create(path)
/// .expect("DirBuilder creation failed");
///
/// assert!(fs::metadata(path).unwrap().is_dir());
/// assert!(fs::metadata(path).expect("fs::metadata call failed").is_dir());
/// ```
#[stable(feature = "dir_builder", since = "1.6.0")]
pub fn create<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
Expand Down
Loading