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 false positive #1669

Closed
leonardo-m opened this issue Apr 9, 2017 · 1 comment
Closed

let_and_return false positive #1669

leonardo-m opened this issue Apr 9, 2017 · 1 comment

Comments

@leonardo-m
Copy link

#![feature(conservative_impl_trait)]
fn pairs<'a, T>(items: &'a [T]) -> impl Iterator<Item=(&'a T, &'a T)> + 'a {
    items.iter().map(|i| (i, i))
}
fn foo() -> usize {
    let data = vec![0, 1, 2, 3];
    let result = pairs(&data).count();
    result
}
fn main() {
    foo();
}
warning: returning the result of a let binding from a block. Consider returning the expression directly.
 --> src\main.rs:8:5
  |
8 |     result
  |     ^^^^^^
  |
  = note: #[warn(let_and_return)] on by default
note: this expression can be directly returned
 --> src\main.rs:7:18
  |
7 |     let result = pairs(&data).count();
  |                  ^^^^^^^^^^^^^^^^^^^^
  = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#let_and_return

If I modify the code as suggested, it stops compiling:

#![feature(conservative_impl_trait)]
fn pairs<'a, T>(items: &'a [T]) -> impl Iterator<Item=(&'a T, &'a T)> + 'a {
    items.iter().map(|i| (i, i))
}
fn foo() -> usize {
    let data = vec![0, 1, 2, 3];
    pairs(&data).count()
}
fn main() {
    foo();
}

Rustc gives:

error: `data` does not live long enough
 --> ...\main.rs:8:1
  |
7 |     pairs(&data).count()
  |            ---- borrow occurs here
8 | }
  | ^ `data` dropped here while still borrowed
  |
  = note: values in a scope are dropped in the opposite order they are created
@ebroto
Copy link
Member

ebroto commented Jun 5, 2020

Duplicate of #1524 (I missed it yesterday), also builds on stable (playground)

@flip1995 flip1995 closed this as completed Jun 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants