Skip to content

unused_lifetimes warning misses unused lifetime #74165

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

Open
Tracked by #94038
bobbobbio opened this issue Jul 8, 2020 · 2 comments
Open
Tracked by #94038

unused_lifetimes warning misses unused lifetime #74165

bobbobbio opened this issue Jul 8, 2020 · 2 comments
Labels
A-lifetimes Area: Lifetimes / regions A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@bobbobbio
Copy link

bobbobbio commented Jul 8, 2020

I discovered an unused lifetime in my code that the compiler didn't tell me about. I was able to reduce the problem to the following snippet.

#![warn(unused_lifetimes)]

struct Foo<'a>(for<'r> fn(&'r mut Foo<'a>));

impl<'a> Foo<'a> {
    pub fn new() -> Self {
        Self(Self::a)
    }
    
    pub fn a(&mut self) {}
}

fn main() {
    let f = Foo::new();
    drop(f);
}

The lifetime 'a is unused, but the compiler doesn't complain at all. I confirmed that it still exists on stable 1.44.1 and on nightly 1.46.0.

@bobbobbio bobbobbio added the C-bug Category: This is a bug. label Jul 8, 2020
@bobbobbio bobbobbio changed the title Compiler misses unused lifetime unused_lifetimes warning misses unused lifetime Jul 8, 2020
@jonas-schievink jonas-schievink added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Jul 8, 2020
@SNCPlay42
Copy link
Contributor

Removing the mut in &'r mut causes the code to fail to compile as expected:

error[E0392]: parameter `'a` is never used
 --> src/main.rs:3:12
  |
3 | struct Foo<'a>(for<'r> fn(&'r Foo<'a>));
  |            ^^ unused parameter
  |
  = help: consider removing `'a`, referring to it in a field, or using a marker such as `std::marker::PhantomData`

The mut causes Foo<'a> to be invariant which is apparently enough to satisfy E0392 - it just checks that there are no unbounded lifetimes. I guess the unused_lifetimes lint assumes E0392 handled any situations like this.

Reduced:

use std::cell::Cell;
pub struct Foo<'a>(Box<Cell<Foo<'a>>>);

@Enselic
Copy link
Member

Enselic commented Jul 24, 2023

@rustbot label A-lifetimes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants