TheBevyFlock / bevy_cli

A Bevy CLI tool and linter.
https://thebevyflock.github.io/bevy_cli/
Apache License 2.0
52 stars 7 forks source link

Lint suggestion: Manual implementation of `Default` is unnecessary #97

Closed BenjaminBrienen closed 2 months ago

BenjaminBrienen commented 2 months ago

Every field of Self is initialized to its default value:

pub(crate) struct ResultSifter<T> {
    items: Vec<T>,
    errors: Option<syn::Error>,
}

impl<T> Default for ResultSifter<T> {
    fn default() -> Self {
        Self {
            items: Vec::new(),
            errors: None,
        }
    }
}

suggestion:

#[derive(Default)]
pub(crate) struct ResultSifter<T> {
    items: Vec<T>,
    errors: Option<syn::Error>,
}
BenjaminBrienen commented 2 months ago

Apparently my example is poor because:

error[E0277]: the trait bound `StructField<'_>: std::default::Default` is not satisfied
   --> crates\bevy_reflect\derive\src\derive_data.rs:428:17
    |
428 |                 ResultSifter::default(),
    |                 ^^^^^^^^^^^^ the trait `std::default::Default` is not implemented for `StructField<'_>`, which is required by `ResultSifter<_>: std::default::Default`
    |
    = help: the trait `std::default::Default` is implemented for `ResultSifter<T>`
note: required for `ResultSifter<StructField<'_>>` to implement `std::default::Default`
   --> crates\bevy_reflect\derive\src\result_sifter.rs:4:10
    |
4   | #[derive(Default)]
    |          ^^^^^^^ unsatisfied trait bound introduced in this `derive` macro
    = note: this error originates in the derive macro `Default` (in Nightly builds, run with -Z macro-backtrace for more info)

but I think the idea is still valid

BenjaminBrienen commented 2 months ago

If I find an actual good example of this I'll reopen it and give the better example

BenjaminBrienen commented 2 months ago

https://smallcultfollowing.com/babysteps/blog/2022/04/12/implied-bounds-and-perfect-derive/