Skip to content

[FP] extra_unused_lifetimes can be triggered by a derive macro #9014

Closed
@JohnTitor

Description

@JohnTitor

Summary

Consider this code:

/// Represents a new user record insertable to the `users` table
#[derive(Insertable, Debug, Default)]
#[table_name = "users"]
pub struct NewUser<'a> {
    pub gh_id: i32,
    pub gh_login: &'a str,
    pub name: Option<&'a str>,
    pub gh_avatar: Option<&'a str>,
    pub gh_access_token: Cow<'a, str>,
}

(The code is from the crates.io source code)

The struct itself needs the 'a lifetime, but notice we use the Insertable derive macro from the Diesel.
Insertable will trigger extra_unused_lifetimes and cause the below warning:

warning: this lifetime isn't used in the impl
  --> src/models/user.rs:26:10
   |
26 | #[derive(Insertable, Debug, Default)]
   |          ^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#extra_unused_lifetimes
   = note: this warning originates in the derive macro `Insertable` (in Nightly builds, run with -Z macro-backtrace for more info)

But you cannot fix it as the warning comes from the outside of your code.

Lint Name

extra_unused_lifetimes

Reproducer

I tried this code:

/// Represents a new user record insertable to the `users` table
#[derive(Insertable, Debug, Default)]
#[table_name = "users"]
pub struct NewUser<'a> {
    pub gh_id: i32,
    pub gh_login: &'a str,
    pub name: Option<&'a str>,
    pub gh_avatar: Option<&'a str>,
    pub gh_access_token: Cow<'a, str>,
}

I saw this happen:

warning: this lifetime isn't used in the impl
  --> src/models/user.rs:26:10
   |
26 | #[derive(Insertable, Debug, Default)]
   |          ^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#extra_unused_lifetimes
   = note: this warning originates in the derive macro `Insertable` (in Nightly builds, run with -Z macro-backtrace for more info)

I expected to see this happen:

No warnings.

Version

rustc 1.63.0-nightly (c3b7d7b49 2022-06-17)
binary: rustc
commit-hash: c3b7d7b496b5536bb0a3d501222d2d0a8b54a69e
commit-date: 2022-06-17
host: x86_64-unknown-linux-gnu
release: 1.63.0-nightly
LLVM version: 14.0.5

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions