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

unwrap() of Option<_> or Result<_> #24

Closed
mkpankov opened this issue Dec 14, 2014 · 3 comments
Closed

unwrap() of Option<_> or Result<_> #24

mkpankov opened this issue Dec 14, 2014 · 3 comments
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy T-middle Type: Probably requires verifiying types

Comments

@mkpankov
Copy link

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

extern crate term;

fn main() {
    let mut t = term::stdout().unwrap();

    t.fg(term::color::GREEN).unwrap();
    (write!(t, "hello, ")).unwrap();

    t.fg(term::color::RED).unwrap();
    (writeln!(t, "world!")).unwrap();

    t.reset().unwrap();
}

As one might learn pretty quickly, unwrap()ing a None returned by term::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() of Option<_> 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 use unwrap() 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".

@kmcallister
Copy link

Option::unwrap() is usually an anti-pattern, but Result::unwrap() is often perfectly reasonable, because it carries along an error message. It's the simplest way to say "panic the task if IO has failed", which is often what you want.

@llogiq
Copy link
Contributor

llogiq commented May 4, 2015

Also you can use if myopt.is_some() { ... myopt.unwrap() ... } else { ... }, which can be a bit more composable than a match. On the other hand, in those cases my_opt.map(|...| ...).unwrap_or_else(|| ...) is probably a better solution.

So, after having used Option more and getting the hang of the methods, I think warning on Option::unwrap() may be a good idea, if only to suggest using expect("some useful information here") instead.

@llogiq
Copy link
Contributor

llogiq commented Jun 2, 2015

Also see issues #6, #10, #19.

@Manishearth Manishearth added good-first-issue These issues are a good way to get started with Clippy T-middle Type: Probably requires verifiying types A-lint Area: New lints labels Aug 11, 2015
birkenfeld added a commit to birkenfeld/rust-clippy that referenced this issue Aug 11, 2015
The latter is set to Allow by default.  Issue rust-lang#24
birkenfeld added a commit to birkenfeld/rust-clippy that referenced this issue Aug 11, 2015
The latter is set to Allow by default (fixes rust-lang#24)
@sebinsua sebinsua mentioned this issue Dec 13, 2015
44 tasks
@sebinsua sebinsua mentioned this issue Dec 23, 2015
15 tasks
yaahc pushed a commit to yaahc/rust-clippy that referenced this issue Mar 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy T-middle Type: Probably requires verifiying types
Projects
None yet
Development

No branches or pull requests

4 participants