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

let_and_return mistake #1524

Closed
leonardo-m opened this issue Feb 8, 2017 · 3 comments
Closed

let_and_return mistake #1524

leonardo-m opened this issue Feb 8, 2017 · 3 comments
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. T-middle Type: Probably requires verifiying types

Comments

@leonardo-m
Copy link

This is reduced code:

#![feature(conservative_impl_trait)]

fn pairwise<'a, T>(items: &'a [T]) -> impl Iterator<Item=(&'a T, &'a T)> + 'a {
    items
    .iter()
    .enumerate()
    .flat_map(move |(i, x1)| items[i ..]
                             .iter()
                             .map(move |x2| (x1, x2)))
}

fn foo() -> usize {
    let bars: Vec<Vec<u8>> = vec![];
    let is_valid = |_: &[u8], _: &[u8]| { true };
    let result = pairwise(&bars).filter(|&(c1, c2)| is_valid(c1, c2)).count();
    result
}

fn main() {
    foo();
}

Clippy gives a warning:

warning: returning the result of a let binding from a block. Consider returning the expression directly.
  --> src\main.rs:16:5
   |
16 |     result
   |     ^^^^^^
   |
   = note: #[warn(let_and_return)] on by default
note: this expression can be directly returned
  --> src\main.rs:15:18
   |
15 |     let result = pairwise(&bars).filter(|&(c1, c2)| is_valid(c1, c2)).count();
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

But following that advice breaks the code:

error: `bars` does not live long enough
  --> ...\src\main.rs:16:1
   |
15 |     pairwise(&bars).filter(|&(c1, c2)| is_valid(c1, c2)).count()
   |               ---- borrow occurs here
16 | }
   | ^ `bars` dropped here while still borrowed
   |
   = note: values in a scope are dropped in the opposite order they are created

error: aborting due to previous error
@oli-obk
Copy link
Contributor

oli-obk commented Feb 9, 2017

related to #1322

@oli-obk oli-obk added E-medium Call for participation: Medium difficulty level problem and requires some initial experience. C-bug Category: Clippy is not doing the correct thing T-middle Type: Probably requires verifiying types labels Feb 9, 2017
@brson
Copy link

brson commented Sep 2, 2019

I've encountered this too. Annoying.

@ebroto
Copy link
Member

ebroto commented Jun 4, 2020

This can be closed as the suggestion builds in current stable, see here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. T-middle Type: Probably requires verifiying types
Projects
None yet
Development

No branches or pull requests

5 participants