Skip to content
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

Add messages to Option's and Result's must_use annotation for is_* #62431

Merged
merged 2 commits into from
Jul 13, 2019
Merged
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
5 changes: 3 additions & 2 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl<T> Option<T> {
/// ```
///
/// [`Some`]: #variant.Some
#[must_use]
#[must_use = "if you intended to assert that this has a value, consider `.unwrap()` instead"]
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn is_some(&self) -> bool {
Expand All @@ -201,7 +201,8 @@ impl<T> Option<T> {
/// ```
///
/// [`None`]: #variant.None
#[must_use]
#[must_use = "if you intended to assert that this doesn't have a value, consider \
`.and_then(|| panic!(\"`Option` had a value when expected `None`\"))` instead"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this message is too long. How about the suggestion is .ok_or(()).unwrap_err()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong opinion; I just wanted to give a message as nice as the .unwrap() ones do.

Maybe there's a way to combine them? .ok_or("Option::None").unwrap_err() or similar?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just tell the user to wrap it in an assert!()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really we should add an expect_none.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #62596.

#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn is_none(&self) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ impl<T, E> Result<T, E> {
/// let x: Result<i32, &str> = Err("Some error message");
/// assert_eq!(x.is_ok(), false);
/// ```
#[must_use]
#[must_use = "if you intended to assert that this is ok, consider `.unwrap()` instead"]
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn is_ok(&self) -> bool {
Expand All @@ -302,7 +302,7 @@ impl<T, E> Result<T, E> {
/// let x: Result<i32, &str> = Err("Some error message");
/// assert_eq!(x.is_err(), true);
/// ```
#[must_use]
#[must_use = "if you intended to assert that this is err, consider `.unwrap_err()` instead"]
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn is_err(&self) -> bool {
Expand Down