-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
unwrap()
of Option<_> or Result<_>
#24
Comments
|
Also you can use So, after having used Option more and getting the hang of the methods, I think warning on |
The latter is set to Allow by default. Issue rust-lang#24
The latter is set to Allow by default (fixes rust-lang#24)
Update to rust style guidelines
I believe we should warn about usage of
unwrap()
of optional types.unwrap()
is essentially a pattern match with_ => panic!()
, but there are often ways to continue execution of program w/o failing.Today, it's common to find code like
As one might learn pretty quickly,
unwrap()
ing a None returned byterm::stdout()
will lead to failure of task. This is, of course, specified in docs. But it's certainly not obvious that this is not a "safe" operation — it may lead to "crash". Not "hard" crash, because it will be handled by Rust runtime. It produces error about unwrapping None. But it's a crash nevertheless.Warnings from compiler that
.unwrap()
is essentially a pattern match w/o "catch-all" case are missing here. We do get warnings when not all variants of enum are handled, so why skip it here?unwrap()
ofOption<_>
is a hack. It's convenient, I know — but it's the sort of convenience we disdain JavaScript and PHP for. I also understand that one is extremely likely to useunwrap()
in closures because "heck, I only need to filter/map/reduce stuff, I don't want my closure to spend 5 lines!". Programmer uses it there and then forgets about it. Voila — safety is needlessly violated.I think a programmer should be forced to actively discard any failure condition. It makes for explicit decision making about not handling an error.
In Rust, in it's present state,
unwrap()
is the easiest and simplest way to Get Things Done. This shortcut will be taken by many and many people who will then later complain about Rust not being secure despite claiming itself "preventing almost all crashes".The text was updated successfully, but these errors were encountered: