Auto merge of #10954 - y21:issue10158, r=llogiq

[`derivable_impls`]: don't lint if `default()` call expr unsize-coerces to trait object

Fixes #10158.

This fixes a FP where the derive-generated Default impl would have different behavior because of unsize coercion from `Box<T>` to `Box<dyn Trait>`:
```rs
struct S {
  x: Box<dyn std::fmt::Debug>
}
impl Default for S {
  fn default() -> Self {
    Self {
      x: Box::<()>::default()
     // ^~ Box<()> coerces to Box<dyn Debug>
     // #[derive(Default)] would call Box::<dyn Debug>::default()
    }
  }
}
```
(this intentionally only looks for trait objects `dyn` specifically, and not any unsize coercion, e.g. `&[i32; 5]` to `&[i32]`, because that breaks existing tests and isn't actually problematic, as far as I can tell)

changelog: [`derivable_impls`]: don't lint if `default()` call expression unsize-coerces to trait object
This commit is contained in:
bors 2023-06-14 16:59:26 +00:00
commit ffe95252bd
3 changed files with 74 additions and 7 deletions

View file

@ -268,4 +268,25 @@ impl Default for OtherGenericType {
}
}
mod issue10158 {
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 {
// Box::<S>::default() adjusts to Box<dyn T>
inner: Box::<S>::default(),
}
}
}
}
fn main() {}

View file

@ -304,4 +304,25 @@ impl Default for OtherGenericType {
}
}
mod issue10158 {
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 {
// Box::<S>::default() adjusts to Box<dyn T>
inner: Box::<S>::default(),
}
}
}
}
fn main() {}