Skip to content

unused_lifetimes lint has false positives with GATs #94307

Closed
@kahomayo

Description

@kahomayo

The unused_lifetimes lint can be incorrectly triggered both by declaring and by implementing GATs.

Declaring a GAT with an unconstrained lifetime causes the lint to fire (Playground):

#![feature(generic_associated_types)]
#![warn(unused_lifetimes)]

trait Foo {
    // This GAT triggers the lint:
    type X<'x>;
    // Using the lifetime in an associated function does not avoid the lint.
    fn make_x<'a>(_x: &'a()) -> Self::X<'a>;
    fn make_x_elide(_x: &()) -> Self::X<'_>;
}

This code should be accepted (or maybe produce a lint about a lack of constraints on 'x). Right now, it produces warnings about unused lifetimes. Note that there is no way to elide the lifetime (as the lint suggests) and that removing the lifetime breaks the API for implementors and users.

warning: lifetime parameter `'x` never used
 --> src/lib.rs:6:12
  |
6 |     type X<'x>;
  |           -^^- help: elide the unused lifetime
  |

Implementing a GAT without using a lifetime causes the lint to fire:

impl Foo for () {
    type X<'x> = u8;
    fn make_x<'a>(_x: &'a ()) -> u8 { 0 }
    fn make_x_elide(_x: &()) -> u8 { 0 }
}

This code should be accepted, as there is no way for the implementor to elide the lifetime here ('_ is reserved and omitting the <'x> is a lifetime mismatch). Using the lifetime in the type would alter the meaning of the code and may break consumers.

warning: lifetime parameter `'x` never used
  --> src/lib.rs:13:12
   |
13 |     type X<'x> = u8;
   |           -^^- help: elide the unused lifetime

Meta

rustc --version --verbose:

rustc 1.61.0-nightly (68369a041 2022-02-22)
binary: rustc
commit-hash: 68369a041cea809a87e5bd80701da90e0e0a4799
commit-date: 2022-02-22
host: x86_64-pc-windows-msvc
release: 1.61.0-nightly
LLVM version: 14.0.0

Metadata

Metadata

Assignees

Labels

A-GATsArea: Generic associated types (GATs)A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.F-generic_associated_types`#![feature(generic_associated_types)]` a.k.a. GATsF-lint-single_use_lifetimes`single_use_lifetimes` lintGATs-triagedIssues using the `generic_associated_types` feature that have been triaged

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions