Skip to content

Consider splitting needless_lifetimes into cases that do and do not require '_ #13514

Description

@dtolnay

Description

I think the suggested code changes from needless_lifetimes are consistently good improvements when they look like this, involving only deleting characters:

warning: the following explicit lifetimes could be elided: 'a
   --> src/de.rs:339:11
    |
339 | impl<'de, 'a> EnumAccess<'de> for &'a mut Deserializer<'de> {
    |           ^^                       ^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes
help: elide the lifetimes
    |
339 - impl<'de, 'a> EnumAccess<'de> for &'a mut Deserializer<'de> {
339 + impl<'de> EnumAccess<'de> for &mut Deserializer<'de> {

and a mixed bag when they look like this:

warning: the following explicit lifetimes could be elided: 'a
   --> src/de.rs:197:19
    |
197 |         impl<'de, 'a> de::SeqAccess<'de> for SeqAccess<'a, 'de> {
    |                   ^^                                   ^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes
help: elide the lifetimes
    |
197 -         impl<'de, 'a> de::SeqAccess<'de> for SeqAccess<'a, 'de> {
197 +         impl<'de> de::SeqAccess<'de> for SeqAccess<'_, 'de> {
    |

I would love to be able to deny the first category and allow the second category.

Another possible way to draw a distinction is whether the lint pertains to an already-named lifetime. In SeqAccess<'a, 'de> a name has already been chosen for that 'a lifetime in the location where the type is defined, so the impl isn't responsible for inventing a whole new name. Whereas in &'a mut Deserializer<'de>, the impl is needlessly inventing a brand new name for this 'a lifetime. I think this "already-named" framing is slightly different than the "does it require '_" framing, considering cases like impl Trait for dyn Trait + '_. I am not sure which of the two would make the best split.

Version

rustc 1.83.0-nightly (9096f4f 2024-10-05)
binary: rustc
commit-hash: 9096f4f
commit-date: 2024-10-05
host: aarch64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions