# Clippy warnings to remove \`return\` a bit too much?

**URL:** <https://users.rust-lang.org/t/clippy-warnings-to-remove-return-a-bit-too-much/52999>\
**Category:** code review\
**Created:** [December 19, 2020, 10:47am UTC](https://users.rust-lang.org/t/clippy-warnings-to-remove-return-a-bit-too-much/52999 "2020-12-19T10:47:39Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![alper](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alper/32/18268_2.png) [@alper](https://users.rust-lang.org/u/alper)\
**Post date:** [December 19, 2020, 10:47am UTC](https://users.rust-lang.org/t/clippy-warnings-to-remove-return-a-bit-too-much/52999/1 "2020-12-19T10:47:39Z")

</div>

I'm curious about the reasoning behind this.

Clippy tells me to remove `return` statements but if I'm doing early returns in a kludge of code, I would prefer these to be marked explicitly.

Wouldn't it be better to limit enforcing `return`less returns to the last statement in a function?

---

<div class="post-metadata">

**Author:** ![johannesvollmer](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/johannesvollmer/32/10473_2.png) [@johannesvollmer](https://users.rust-lang.org/u/johannesvollmer)\
**Post date:** [December 19, 2020, 11:58am UTC](https://users.rust-lang.org/t/clippy-warnings-to-remove-return-a-bit-too-much/52999/2 "2020-12-19T11:58:03Z")

</div>

Do you have an example? I can't remember having clippy suggest me to remove a return keyword except in the last statement 🙂

---

<div class="post-metadata">

**Author:** ![carlomilanesi](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/carlomilanesi/32/24581_2.png) [@carlomilanesi](https://users.rust-lang.org/u/carlomilanesi)\
**Post date:** [December 19, 2020, 12:01pm UTC](https://users.rust-lang.org/t/clippy-warnings-to-remove-return-a-bit-too-much/52999/3 "2020-12-19T12:01:09Z")

</div>

Early returns must use the `return` keyword, so Clippy cannot ask to remove them. Could you post a relevant code snippet?

---

<div class="post-metadata">

**Author:** ![H2CO3](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@H2CO3](https://users.rust-lang.org/u/H2CO3)\
**Post date:** [December 19, 2020, 12:07pm UTC](https://users.rust-lang.org/t/clippy-warnings-to-remove-return-a-bit-too-much/52999/4 "2020-12-19T12:07:01Z")

</div>

> [@alper](#):
>
> Clippy tells me to remove `return` statements but if I'm doing early returns in a kludge of code, I would prefer these to be marked explicitly.

I bet you have something like this:

```rust
fn foo() -> Option<u32> {
    if early_return_condition {
        return None;
    } else {
        return some_other_calculation();
    }
}

```

which can be simplified to

```rust
fn foo() -> Option<u32> {
    if early_return_condition {
        None
    } else {
        some_other_calculation()
    }
}

```

The "last expression of the function" does not literally mean the last line only. If the last expression happens to be a structured expression like `if` or `match`, it has a value too, so you can omit the `returns` there as well.

---

<div class="post-metadata">

**Author:** ![alper](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alper/32/18268_2.png) [@alper](https://users.rust-lang.org/u/alper)\
**Post date:** [December 19, 2020, 12:36pm UTC](https://users.rust-lang.org/t/clippy-warnings-to-remove-return-a-bit-too-much/52999/5 "2020-12-19T12:36:02Z")

</div>

I think it’s fine that you can omit them but should clippy enforce this?

I leave them in because I think it’s clearer if there is code in between a bunch of other code that does a return, to mark it explicitly with the keyword. Especially if you have a complicated nested setup (which of course you shouldn’t have).

---

<div class="post-metadata">

**Author:** ![mbrubeck](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mbrubeck/32/648_2.png) [@mbrubeck](https://users.rust-lang.org/u/mbrubeck)\
**Post date:** [December 19, 2020, 12:41pm UTC](https://users.rust-lang.org/t/clippy-warnings-to-remove-return-a-bit-too-much/52999/6 "2020-12-19T12:41:16Z")

</div>

You can also transform code like this into an "actual" early return, which will make Clippy happy, and reduce nesting:

```rust
fn foo() -> Option<u32> {
    if early_return_condition {
        return None;
    }
    some_other_calculation()
}

```

---

<div class="post-metadata">

**Author:** ![cliff](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cliff/32/689_2.png) [@cliff](https://users.rust-lang.org/u/cliff)\
**Post date:** [December 19, 2020, 12:53pm UTC](https://users.rust-lang.org/t/clippy-warnings-to-remove-return-a-bit-too-much/52999/7 "2020-12-19T12:53:17Z")

</div>

Regarding your direct question, I do think clippy should enforce it. Rust is an expression based language, which is different from what a lot of people are used to. Every block evaluates to the value of its last statement. Every function returns the value our evaluates to. It's a powerful pattern, and worth teaching through clippy.

If you have a strong desire to write non idiomatic rust, feel free to disable the lint, but idiomatic rust only uses return when necessary.

---

<div class="post-metadata">

**Author:** ![cliff](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cliff/32/689_2.png) [@cliff](https://users.rust-lang.org/u/cliff)\
**Post date:** [December 19, 2020, 1:01pm UTC](https://users.rust-lang.org/t/clippy-warnings-to-remove-return-a-bit-too-much/52999/8 "2020-12-19T13:01:00Z")

</div>

Note, it's almost wrong in terms of rust semantics to even think about it as "omitting" a return statement. Explicitly adding the return is as strange in rust as writing

```rust
def main():
    import sys
    sys.exit(0)
    return

```

in Python. It's redundant, and leads the reader to suspect something weird is happening when it's not.

---

<div class="post-metadata">

**Author:** ![alper](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alper/32/18268_2.png) [@alper](https://users.rust-lang.org/u/alper)\
**Post date:** [December 19, 2020, 1:20pm UTC](https://users.rust-lang.org/t/clippy-warnings-to-remove-return-a-bit-too-much/52999/9 "2020-12-19T13:20:16Z")

</div>

> [@cliff](#):
>
> Rust is an expression based language, which is different from what a lot of people are used to. Every block evaluates to the value of its last statement. Every function returns the value our evaluates to. It's a powerful pattern, and worth teaching through clippy.

OK. That’s what I wanted to know.

---

<div class="post-metadata">

**Author:** ![johannesvollmer](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/johannesvollmer/32/10473_2.png) [@johannesvollmer](https://users.rust-lang.org/u/johannesvollmer)\
**Post date:** [December 25, 2020, 12:25pm UTC](https://users.rust-lang.org/t/clippy-warnings-to-remove-return-a-bit-too-much/52999/10 "2020-12-25T12:25:59Z")

</div>

Here's why explicit return statements might be considered unidiomatic: [The forgotten art of structured programming (YouTube)](https://youtu.be/SFv8Wm2HdNM) (skip to 14m0s for actual arguments)

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/scottmcm/32/4286_2.png) [@scottmcm](https://users.rust-lang.org/u/scottmcm)\
**Post date:** [January 17, 2021, 7:48pm UTC](https://users.rust-lang.org/t/clippy-warnings-to-remove-return-a-bit-too-much/52999/11 "2021-01-17T19:48:26Z")

</div>

Note that if you look at the core of _Go To Statement Considered Harmful_, it's about

> The unbridled use of the **go to** statement has an immediate consequence that it becomes terribly hard to find a meaningful set of coordinates in which to describe the process progress.

The one use of goto that doesn't have this problem is using it to exit a block -- and that's exactly what `break` from loops (including labeled loops), and `return` do.

That's why clippy doesn't complain about all early returns, [as was mentioned above](https://users.rust-lang.org/t/clippy-warnings-to-remove-return-a-bit-too-much/52999/6) -- only the ones that are unneeded, the same way the compiler complains about unnecessary parentheses.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [April 17, 2021, 7:48pm UTC](https://users.rust-lang.org/t/clippy-warnings-to-remove-return-a-bit-too-much/52999/12 "2021-04-17T19:48:51Z")

</div>

Closed on April 17, 2021, 7:48pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
