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

[ER] Unused lifetime warning #91588

Open
leonardo-m opened this issue Dec 6, 2021 · 3 comments
Open

[ER] Unused lifetime warning #91588

leonardo-m opened this issue Dec 6, 2021 · 3 comments
Labels
A-lifetimes Area: Lifetimes / regions C-feature-request Category: A feature request, i.e: not implemented / a PR.

Comments

@leonardo-m
Copy link

In code like this rustc could warn that the 'b lifetime is not needed:

fn select<'a, 'b, const N: usize>(data: &'a mut [u32; N],
                                  keep: &'b mut [bool; N]) -> &'a [u32] {
    let mut pos = 0;
    for (i, &b) in keep.iter().enumerate() {
        if b {
            data[pos] = data[i];
            pos += 1;
        }
    }
    &data[.. pos]
}

fn main() {
    let mut data = [1, 2, 3, 4, 5];
    let mut keep = [true, false, true, false, true];
    println!("{:?}", select(&mut data, &mut keep));
}

(Currently rustc also doesn't spot that the 'keep' function argument doesn't need to be &mut).

@pro465
Copy link
Contributor

pro465 commented Dec 6, 2021

i think 'b is needed.

consider the following case:

fn main() {
    let mut data = [1, 2, 3, 4, 5];
    let lives_longer_than_keep = {
           let mut keep = [true, false, true, false, true];
           select(&mut data, &mut keep)
    };
    println!("{:?}", lives_longer_than_keep);
}

if you simply make keep use the same lifetime as data and the return type, i.e. 'a, then i think this will not compile.

however, i agree with you on the unnecessary &mut thing

@leonardo-m
Copy link
Author

leonardo-m commented Dec 6, 2021

The code I meant rustc to suggest is without 'b, like this (using your main function):

fn select<'a, const N: usize>(data: &'a mut [u32; N],
                              keep: &mut [bool; N]) -> &'a [u32] {
    let mut pos = 0;
    for (i, &b) in keep.iter().enumerate() {
        if b {
            data[pos] = data[i];
            pos += 1;
        }
    }
    &data[.. pos]
}

fn main() {
    let mut data = [1, 2, 3, 4, 5];
    let lives_longer_than_keep = {
           let mut keep = [true, false, true, false, true];
           select(&mut data, &mut keep)
    };
    println!("{:?}", lives_longer_than_keep);
}

In this code the keep doesn't have implicitly the same lifetime as 'a, it has a different lifetime, that's anonymous. So that code compiles and works correctly.

This ER was suggested by both my coding experience with rust, and the code changes in: #91580

There functions like this one have lifetimes that aren't necessary, but rustc isn't spotting them:

pub fn iter(&'a self) -> impl Iterator<Item = &'a T> {

@pro465
Copy link
Contributor

pro465 commented Dec 6, 2021

oh well, then i certainly agree with you

@Dylan-DPC Dylan-DPC added A-lifetimes Area: Lifetimes / regions C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Feb 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions C-feature-request Category: A feature request, i.e: not implemented / a PR.
Projects
None yet
Development

No branches or pull requests

3 participants