Skip to content

Check for lifetime uses in closures as well #14608

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

Merged
merged 1 commit into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions clippy_lints/src/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ fn could_use_elision<'tcx>(
return None;
}

let mut checker = BodyLifetimeChecker;
let mut checker = BodyLifetimeChecker::new(cx);
if checker.visit_expr(body.value).is_break() {
return None;
}
Expand Down Expand Up @@ -911,10 +911,23 @@ fn elision_suggestions(
Some(suggestions)
}

struct BodyLifetimeChecker;
struct BodyLifetimeChecker<'tcx> {
tcx: TyCtxt<'tcx>,
}

impl<'tcx> BodyLifetimeChecker<'tcx> {
fn new(cx: &LateContext<'tcx>) -> Self {
Self { tcx: cx.tcx }
}
}

impl<'tcx> Visitor<'tcx> for BodyLifetimeChecker {
impl<'tcx> Visitor<'tcx> for BodyLifetimeChecker<'tcx> {
type Result = ControlFlow<()>;
type NestedFilter = middle_nested_filter::OnlyBodies;

fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
self.tcx
}
// for lifetimes as parameters of generics
fn visit_lifetime(&mut self, lifetime: &'tcx Lifetime) -> ControlFlow<()> {
if !lifetime.is_anonymous() && lifetime.ident.name != kw::StaticLifetime {
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/needless_lifetimes.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,11 @@ mod issue13749bis {
impl<'a, T: 'a> Generic<T> {}
}

pub fn issue14607<'s>(x: &'s u8) {
#[expect(clippy::redundant_closure_call)]
(|| {
let _: &'s u8 = x;
})();
}

fn main() {}
7 changes: 7 additions & 0 deletions tests/ui/needless_lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,11 @@ mod issue13749bis {
impl<'a, T: 'a> Generic<T> {}
}

pub fn issue14607<'s>(x: &'s u8) {
#[expect(clippy::redundant_closure_call)]
(|| {
let _: &'s u8 = x;
})();
}

fn main() {}