Skip to content

#[expect(redundant_lifetimes)] doesn't work when #[derive(Debug)] is present #150553

Description

@luisfi-dev

View all comments

Consider the following code:

#![deny(redundant_lifetimes)]

use std::fmt::Debug;

#[derive(Debug)]
pub struct RefWrapper<'a, T>
where
    'a: 'static,
    T: Debug,
{
    pub t_ref: &'a T
}

As expected, the compiler throws an error because the 'a lifetime is redundant:

➜  rust-playground git:(master) ✗ cargo check
    Checking rust-playground v0.1.0 (/Users/luisferlcc/rust-playground)
error: unnecessary lifetime parameter `'a`
 --> src/lib.rs:6:23
  |
6 | pub struct RefWrapper<'a, T>
  |                       ^^
  |
  = note: you can use the `'static` lifetime directly, in place of `'a`
note: the lint level is defined here
 --> src/lib.rs:1:9
  |
1 | #![deny(redundant_lifetimes)]
  |         ^^^^^^^^^^^^^^^^^^^

error: unnecessary lifetime parameter `'a`
 --> src/lib.rs:6:23
  |
6 | pub struct RefWrapper<'a, T>
  |                       ^^
  |
  = note: you can use the `'static` lifetime directly, in place of `'a`

error: could not compile `rust-playground` (lib) due to 2 previous errors

Adding #[allow(redundant_lifetimes)] to the type definition...

#![deny(redundant_lifetimes)]

use std::fmt::Debug;

#[derive(Debug)]
#[allow(redundant_lifetimes)]
pub struct RefWrapper<'a, T>
where
    'a: 'static,
    T: Debug,
{
    pub t_ref: &'a T
}

...predictably eliminates the error:

➜  rust-playground git:(master) ✗ cargo check
    Checking rust-playground v0.1.0 (/Users/luisferlcc/rust-playground)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.02s

However, changing the allow attribute to expect...

#![deny(redundant_lifetimes)]

use std::fmt::Debug;

#[derive(Debug)]
#[expect(redundant_lifetimes)]
pub struct RefWrapper<'a, T>
where
    'a: 'static,
    T: Debug,
{
    pub t_ref: &'a T
}

...inexplicably fails to make the linter stand down:

➜  rust-playground git:(master) ✗ cargo check
    Checking rust-playground v0.1.0 (/Users/luisferlcc/rust-playground)
error: unnecessary lifetime parameter `'a`
 --> src/lib.rs:7:23
  |
7 | pub struct RefWrapper<'a, T>
  |                       ^^
  |
  = note: you can use the `'static` lifetime directly, in place of `'a`
note: the lint level is defined here
 --> src/lib.rs:1:9
  |
1 | #![deny(redundant_lifetimes)]
  |         ^^^^^^^^^^^^^^^^^^^

error: could not compile `rust-playground` (lib) due to 1 previous error

But removing #[derive(Debug)] allows the expect attribute to take effect:

#![deny(redundant_lifetimes)]

use std::fmt::Debug;

#[expect(redundant_lifetimes)]
pub struct RefWrapper<'a, T>
where
    'a: 'static,
    T: Debug,
{
    pub t_ref: &'a T
}
➜  rust-playground git:(master) ✗ cargo check
    Checking rust-playground v0.1.0 (/Users/luisferlcc/rust-playground)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s

Meta

This issue occurs on the latest versions of all three channels:

➜  rust-playground git:(master) ✗ rustc --version --verbose
rustc 1.92.0 (ded5c06cf 2025-12-08)
binary: rustc
commit-hash: ded5c06cf21d2b93bffd5d884aa6e96934ee4234
commit-date: 2025-12-08
host: aarch64-apple-darwin
release: 1.92.0
LLVM version: 21.1.3
➜  rust-playground git:(master) ✗ rustc +beta --version --verbose
rustc 1.93.0-beta.5 (72b6488ba 2025-12-21)
binary: rustc
commit-hash: 72b6488ba485fd08cd96f194ffdd83ba8aa0b057
commit-date: 2025-12-21
host: aarch64-apple-darwin
release: 1.93.0-beta.5
LLVM version: 21.1.8
➜  rust-playground git:(master) ✗ rustc +nightly --version --verbose
rustc 1.94.0-nightly (0e8999942 2025-12-30)
binary: rustc
commit-hash: 0e8999942552691afc20495af6227eca8ab0af05
commit-date: 2025-12-30
host: aarch64-apple-darwin
release: 1.94.0-nightly
LLVM version: 21.1.8
Backtrace

➜  rust-playground git:(master) ✗ RUST_BACKTRACE=1 cargo check
    Checking rust-playground v0.1.0 (/Users/luisferlcc/rust-playground)
error: unnecessary lifetime parameter `'a`
 --> src/lib.rs:7:54
  |
7 | pub struct RefWrapper<#[expect(redundant_lifetimes)] 'a, T>
  |                                                      ^^
  |
  = note: you can use the `'static` lifetime directly, in place of `'a`
note: the lint level is defined here
 --> src/lib.rs:1:9
  |
1 | #![deny(redundant_lifetimes)]
  |         ^^^^^^^^^^^^^^^^^^^

error: could not compile `rust-playground` (lib) due to 1 previous error

Activity

  1. added
    C-bug
    on Dec 31, 2025
  2. added
    needs-triage
    on Dec 31, 2025
  3. Urgau commented on Jan 1, 2026

    @Urgau
    Member

    The difference between the #[allow] case and #[expect] lies in the keeping of the attribute in the generated derive code.

    We see that #[expect] is not kept when expending the derive(Debug) macro.

    #[expect(redundant_lifetimes)]
    pub struct RefWrapper<'a, T> where 'a: 'static, T: Debug {
        pub t_ref: &'a T,
    }
    #[automatically_derived]
    impl<'a, T: ::core::fmt::Debug> ::core::fmt::Debug for RefWrapper<'a, T> where
        'a: 'static, T: Debug {
        #[inline]
        fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
            ::core::fmt::Formatter::debug_struct_field1_finish(f, "RefWrapper",
                "t_ref", &&self.t_ref)
        }
    }
  4. Urgau commented on Jan 1, 2026

    @Urgau
    Member

    We are missing the expect lint attribute when filtering the attributes to keep:

    // Keep the lint attributes of the previous item to control how the
    // generated implementations are linted
    let mut attrs = newitem.attrs.clone();
    attrs.extend(
    item.attrs
    .iter()
    .filter(|a| {
    a.has_any_name(&[
    sym::allow,
    sym::warn,
    sym::deny,
    sym::forbid,
    sym::stable,
    sym::unstable,
    ])
    })
    .cloned(),
    );

  5. Urgau commented on Jan 1, 2026

    @Urgau
    Member

    However adding the #[expect] attribute to the generated code could cause some code to start having a warning for unfilled expectations. Example below.

    #[expect(non_camel_case_types)]  // currently only applied to the struct, so no unfilled expectation
    #[derive(Debug)]
    pub struct SCREAMING_CASE {
        pub t_ref: i64,
    }
    
    #[expect(non_camel_case_types)]  // applied to the struct and derived code
    pub struct SCREAMING_CASE_2 {
        pub t_ref: i64,
    }
    #[expect(non_camel_case_types)]  // unfilled expectation
    #[automatically_derived]
    impl ::core::fmt::Debug for SCREAMING_CASE_2 {
        #[inline]
        fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
            todo!()
        }
    }

    One way I could see us fix this mismatch could be to downgrade the #[expect] to #[allow] in the derived code. That would avoid having unfilled expectations while also hiding any warnings (as asked in this issue).

    In any-case, this is a language question. cc @rust-lang/lang-ops
    @rustbot labels +T-lang -needs-triage

  6. added
    T-lang
    and removed
    needs-triage
    on Jan 1, 2026
  7. added
    I-lang-nominated
    I-lang-radar
    P-lang-drag-1
    on Jan 1, 2026
  8. luisfi-dev commented on Jan 2, 2026

    @luisfi-dev
    ContributorAuthor

    One way I could see us fix this mismatch could be to downgrade the #[expect] to #[allow] in the derived code. That would avoid having unfilled expectations while also hiding any warnings (as asked in this issue).

    That seems like a great solution, and actually one that would work for all other lints, not just for redundant_lifetimes. Hopefully we can implement it that way.

  9. joshtriplett commented on Jan 21, 2026

    @joshtriplett
    Member

    (@tmandry is planning to write a lang comment here.)

  10. tmandry commented on Jan 21, 2026

    @tmandry
    Member

    We discussed this on the lang team and our preferred model (articulated by @joshtriplett) is one where every time you write #[expect] it acts as an allow, and warns if there was not at least one suppression of the lint for that attribute.

    Duplicating expect attributes on derives and changing them to allows on derives both deviate from this model, which would be unfortunate.

    Question: Is it feasible to change the implementation of the expect check to group suppressed lints based on the span of the expect attribute? That would preserve the model from the user's perspective.

  11. traviscross commented on Jan 21, 2026

    @traviscross
    Contributor

    We talked as well about how this is a good use case for having some syntax to group items. If we had that, then the expect could be applied to that in the desugaring.

  12. removed
    I-lang-nominated
    on Jan 28, 2026
  13. 29 remaining items

  14. Urgau commented on Jul 25, 2026

    @Urgau
    Member

    Renominating the issue for T-lang, specifically regarding #150553 (comment) and #150553 (comment).

  15. added
    P-lang-drag-1
    on Jul 29, 2026
  16. added
    I-lang-radar
    on Oct 7, 2026
  17. tmandry commented on Oct 28, 2026

    @tmandry
    Member

    We talked about this in the lang meeting today. Those present all thought that no matter what the underlying model is, the behavior should ultimately be the one I described in #150553 (comment). That is: An #[expect] on an item with a #[derive] is satisfied by a single lint, on either the item itself or a derive-generated item.

    Any model we accept in the language ought to have this behavior, whether it is with grouped items or some other mechanism. We don't think the current behavior is correct, and couldn't see our way to accepting a model that deems it correct.

    While the behavior should hold for both builtin and user-defined derives, everyone present was comfortable with taking half-measures that only work for builtin derives in the meantime, given our level of certainty about what the final behavior should be.

  18. petrochenkov commented on Nov 10, 2026

    @petrochenkov
    Contributor

    Wait a second, why is redundant_lifetimes reported in code generated by an external macro in the first place?
    Typically generated impls do not report lints like that.
    The lint is probably checking the lifetime's span to determine whether we are in an expansion or not, but that span is copied from the derive input and is too small to see the "big picture".

    So the lint itself can be fixed, and it will allow us to postpone merging of #158943, which I think can paint us into a corner.

  19. removed
    I-lang-nominated
    P-lang-drag-1
    on Nov 11, 2026
  20. added a commit that references this issue on Nov 11, 2026
  21. added 2 commits that reference this issue on Nov 12, 2026
  22. added a commit that references this issue on Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugE-hardI-lang-radarT-lang

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions