Skip to content

derivable_impl false positive when Default is implemented manually to structs with a dyn field #10158

Closed
@Y-Nak

Description

@Y-Nak

Summary

false positive with derivable_impl if

  1. Default is implemented manually to a struct having Box<dyn Trait> field, and
  2. A type being used in the Default implementation implements Default

Lint Name

derivable_impl

Reproducer

I tried this code:

pub trait T {}

#[derive(Default)]
pub struct S {}
impl T for S {}

pub struct Outer {
    pub inner: Box<dyn T>,
}

impl Default for Outer {
    fn default() -> Self {
        Outer {
            inner: Box::<S>::default(),
        }
    }
}

I saw this happen:

warning: this `impl` can be derived
  --> src/lib.rs:11:1
   |
11 | / impl Default for Outer {
12 | |     fn default() -> Self {
13 | |         Outer {
14 | |             inner: Box::<S>::default(),
15 | |         }
16 | |     }
17 | | }
   | |_^
   |
   = 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: remove the manual implementation...
help: ...and instead derive it
   |
7  | #[derive(Default)]
   |

I expected to see this happen: derivable_impl is not invoked.

Version

rustc 1.66.0 (69f9c33d7 2022-12-12)
binary: rustc
commit-hash: 69f9c33d71c871fc16ac445211281c6e7a340943
commit-date: 2022-12-12
host: aarch64-apple-darwin
release: 1.66.0
LLVM version: 15.0.2

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