Skip to content

Correct manual impl Default #15493

@leonardo-m

Description

@leonardo-m

Summary

Clippy warns a manually implenented Default, while I think it's correct on both non-Nightly and Nightly compilers (on Nightly there's a better solution).

Lint Name

derivable_impls

Reproducer

I tried this code:

#![feature(const_trait_impl)]
#![feature(const_default)]
#![allow(dead_code)]

#[derive(Copy, Clone)]
#[repr(transparent)]
struct Foo(u64);

impl const Default for Foo {
    fn default() -> Self {
        Self(0)
    }
}

fn main() {
    const X: Foo = Foo::default();
}

But Clippy complains:

warning: this `impl` can be derived
  --> src\main.rs:9:1
   |
 9 | / impl const Default for Foo {
10 | |     fn default() -> Self {
11 | |         Self(0)
12 | |     }
13 | | }
   | |_^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#derivable_impls
   = note: `#[warn(clippy::derivable_impls)]` on by default
help: replace the manual implementation with a derive attribute
   |
 7 + #[derive(Default)]
 8 ~ struct Foo(u64);
   |

If I do that, the code doesn't compile anymore:

error[E0277]: the trait bound `Foo: const Default` is not satisfied

To make it compilable again on Nightly I need to use a derive_const:

#![feature(const_trait_impl)]
#![feature(const_default)]
#![feature(derive_const)]
#![allow(dead_code)]

#[derive(Copy, Clone)]
#[repr(transparent)]
#[derive_const(Default)]
struct Foo(u64);

fn main() {
    const X: Foo = Foo::default();
}

Version

rustc 1.91.0-nightly (898aff704 2025-08-14)
binary: rustc
commit-hash: 898aff704d6f0d00343f21d31b8b9bfac8e43007
commit-date: 2025-08-14
host: x86_64-pc-windows-gnu
release: 1.91.0-nightly
LLVM version: 21.1.0

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