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

Be discouraging about unwrap() lints #1921

Closed
mulkieran opened this issue Aug 2, 2017 · 14 comments
Closed

Be discouraging about unwrap() lints #1921

mulkieran opened this issue Aug 2, 2017 · 14 comments
Labels
S-needs-discussion Status: Needs further discussion before merging or work can be started

Comments

@mulkieran
Copy link

Please see #1300 for previous discussion.

My position is in the comments, but it essentially means that programmers should use expect() to communicate verbally an invariant in their code which logically implies that the unwrap() should not fail.

Thus, the use of unwrap() should be discouraged. Programmers can use expect() to indicate logical impossibility of failure and return an error to handle the failure.

I would welcome suggestions about how to move this out of clippy into a more central repo like rust documentation. The examples of the use of expect() that I have seen in the docs are generally no more informative than a bare unwrap(), something like:

b.expect("This should work") which is identical in information content to b.unwrap() IMHO.

@oli-obk
Copy link
Contributor

oli-obk commented Aug 2, 2017

I often have used b.expect("checked above"), which isn't all that helpful either...

@oli-obk oli-obk added the S-needs-discussion Status: Needs further discussion before merging or work can be started label Aug 2, 2017
@Manishearth
Copy link
Member

Yeah, I'm for making the ones that suggest expect over unwrap warn.

@oli-obk
Copy link
Contributor

oli-obk commented Aug 2, 2017

@mulkieran You're probably going to get few counter arguments judging from the other issue. Do you want to open a PR changing the level from allow to warn?

@llogiq
Copy link
Contributor

llogiq commented Aug 2, 2017

I personally think that Result::unwrap() is ok, as it's often better than a generic message. I'm ok with warning on Option::unwrap() though, as long as it's not in test cases. At least for now. Once we can return T: Try in main() and tests (and I understand this work is underway), we should ask people to use ? instead for both Option and Result.

@mulkieran
Copy link
Author

@llogiq Currently, I unwrap() like its going out of style in test cases, so I understand where you're coming from. Maybe I should re-evaluate, though.

@llogiq
Copy link
Contributor

llogiq commented Aug 2, 2017

As I said, once we can use ? in tests like unwrap (well, perhaps with better output) I'm all for changing the code. For now, it'll only confuse or frustrate our users.

@mulkieran
Copy link
Author

@oli-obk I don't mind waiting for a bit more input. Part of my position is that the way expect() is generally used in examples in the docs makes its existence virtually pointless (and that that's a shame). And that clippy might be a helpful agent of change.

@llogiq Why does not using Result::unwrap() lead to a generic error message?

@llogiq
Copy link
Contributor

llogiq commented Aug 2, 2017

Let's say I have a Result<_, Box<Error>> (or any other type implementing the Error trait, but I'll box here for simplicity). Now if you .unwrap() you'll get the contained error description, whereas with .expect("this should not happen!") you just get "this should not happen!".

@oli-obk
Copy link
Contributor

oli-obk commented Aug 3, 2017

    Err::<(), &str>("foobar").expect("this should not happen");

yields

thread 'main' panicked at 'this should not happen: "foobar"',

@raindev
Copy link

raindev commented Jun 19, 2018

I was surprised to not get any warnings from clippy when using Option::unwrap. E.g. in IntelliJ I would get a warning every time I try to call Java's Optional::get without guarding it by if (optional.isPresent). Option should be mapped over, methods that unwrap Option are the last resort, in my opinion. I think making the developer explicitly acknowledge the case that is not always safe with Option::expect is a good idea. Unsafe APIs should be more clumsy to use.

Another point is that the documentation frequently overuses and misuses both unwrap and expect methods. A good expect message would be one that explains why the invariant holds true. E.g. c.to_digit(10).expect("input to not allow non-digits"). Panic message could include "expect" prefix to make the error message clearer:

thread 'main' panicked at 'expect input to not allow non-digits'

@Lokathor
Copy link

Lokathor commented Jan 6, 2019

So, the main reason that I sometimes don't use expect is that the text still shows up in the binary, and lately I've been working on the GBA, where you get 32MB total for everything, so spare text you don't really need is just junk data. So I just unwrap with a comment in the source.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 8, 2019

So, the main reason that I sometimes don't use expect is that the text still shows up in the binary, and lately I've been working on the GBA, where you get 32MB total for everything, so spare text you don't really need is just junk data. So I just unwrap with a comment in the source.

That seems like enough of an edge case to turn off the lint, not to change the defaults.

@Lokathor
Copy link

Lokathor commented Jan 8, 2019

Oh I'm all with you. I'm shocked that "don't use unwrap you dummy, at least use expect" wasn't the first lint that clippy ever implemented.

@camsteffen
Copy link
Contributor

I don't see anything changing here. unwrap is perfectly sensible in certain cases and blanket warning by default would be too noisy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-needs-discussion Status: Needs further discussion before merging or work can be started
Projects
None yet
Development

No branches or pull requests

7 participants