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

Detect named lifetimes that are effectively 'static #10108

Open
estebank opened this issue Dec 22, 2022 · 2 comments
Open

Detect named lifetimes that are effectively 'static #10108

estebank opened this issue Dec 22, 2022 · 2 comments
Labels
A-lint Area: New lints

Comments

@estebank
Copy link
Contributor

estebank commented Dec 22, 2022

What it does

Given a function that returns a borrow to a lifetime that isn't used in any parameter, give and explanation and suggest removing the named lifetime and making it 'static, as it is the only effective lifetime that will work:

fn foo<'a>() -> &'a str { todo!() }

Lint Name

hidden_static_lifetime

Category

suspicious, style, pedantic

Advantage

It makes the code easier to follow and is a good thing to teach users about.

Drawbacks

No response

Example

fn foo<'a>() -> &'a str { todo!() }

Could be written as:

fn foo() -> &'static str { todo!() }
@estebank estebank added the A-lint Area: New lints label Dec 22, 2022
@lukaslueg
Copy link
Contributor

Notice Box::leak():

/// Consumes and leaks the Box, returning a mutable reference, &'a mut T.
/// Note that the type T must outlive the chosen lifetime 'a. If the type
/// has only static references, or none at all, then this may be chosen to be 'static.
pub fn leak<'a>(b: Box<T, A>) -> &'a mut T where A: 'a

That is, in the face of generics, 'a is not necessarily 'static.

@blyxyas
Copy link
Member

blyxyas commented Dec 24, 2022

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants