Skip to content

Commit cd78473

Browse files
committed
Implement default_mismatches_new lint
If a type has an auto-derived `Default` trait and a `fn new() -> Self`, this lint checks if the `new()` method performs custom logic rather than simply calling the `default()` method. Users expect the `new()` method to be equivalent to `default()`, so if the `Default` trait is auto-derived, the `new()` method should not perform custom logic. Otherwise, there is a risk of different behavior between the two instantiation methods. ```rust struct MyStruct(i32); impl MyStruct { fn new() -> Self { Self(42) } } ``` Users are unlikely to notice that `MyStruct::new()` and `MyStruct::default()` would produce different results. The `new()` method should use auto-derived `default()` instead to be consistent: ```rust struct MyStruct(i32); impl MyStruct { fn new() -> Self { Self::default() } } ``` Alternatively, if the `new()` method requires a non-default initialization, implement a custom `Default`. This also allows you to mark the `new()` implementation as `const`: ```rust struct MyStruct(i32); impl MyStruct { const fn new() -> Self { Self(42) } } impl Default for MyStruct { fn default() -> Self { Self::new() } } ```
1 parent 582779b commit cd78473

13 files changed

Lines changed: 863 additions & 236 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6633,6 +6633,7 @@ Released 2018-09-13
66336633
[`declare_interior_mutable_const`]: https://rust-lang.github.io/rust-clippy/master/index.html#declare_interior_mutable_const
66346634
[`default_constructed_unit_structs`]: https://rust-lang.github.io/rust-clippy/master/index.html#default_constructed_unit_structs
66356635
[`default_instead_of_iter_empty`]: https://rust-lang.github.io/rust-clippy/master/index.html#default_instead_of_iter_empty
6636+
[`default_mismatches_new`]: https://rust-lang.github.io/rust-clippy/master/index.html#default_mismatches_new
66366637
[`default_numeric_fallback`]: https://rust-lang.github.io/rust-clippy/master/index.html#default_numeric_fallback
66376638
[`default_trait_access`]: https://rust-lang.github.io/rust-clippy/master/index.html#default_trait_access
66386639
[`default_union_representation`]: https://rust-lang.github.io/rust-clippy/master/index.html#default_union_representation

clippy_lints/src/declared_lints.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,8 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
567567
crate::needless_update::NEEDLESS_UPDATE_INFO,
568568
crate::neg_cmp_op_on_partial_ord::NEG_CMP_OP_ON_PARTIAL_ORD_INFO,
569569
crate::neg_multiply::NEG_MULTIPLY_INFO,
570-
crate::new_without_default::NEW_WITHOUT_DEFAULT_INFO,
570+
crate::new_vs_default::DEFAULT_MISMATCHES_NEW_INFO,
571+
crate::new_vs_default::NEW_WITHOUT_DEFAULT_INFO,
571572
crate::no_effect::NO_EFFECT_INFO,
572573
crate::no_effect::NO_EFFECT_UNDERSCORE_BINDING_INFO,
573574
crate::no_effect::UNNECESSARY_OPERATION_INFO,

clippy_lints/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ mod needless_question_mark;
266266
mod needless_update;
267267
mod neg_cmp_op_on_partial_ord;
268268
mod neg_multiply;
269-
mod new_without_default;
269+
mod new_vs_default;
270270
mod no_effect;
271271
mod no_mangle_with_rust_abi;
272272
mod non_canonical_impls;
@@ -616,7 +616,7 @@ pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Co
616616
},
617617
Box::new(|_| Box::new(swap::Swap)),
618618
Box::new(|_| Box::new(panicking_overflow_checks::PanickingOverflowChecks)),
619-
Box::new(|_| Box::<new_without_default::NewWithoutDefault>::default()),
619+
Box::new(|_| Box::<new_vs_default::NewVsDefault>::default()),
620620
Box::new(move |_| Box::new(disallowed_names::DisallowedNames::new(conf))),
621621
Box::new(move |tcx| Box::new(functions::Functions::new(tcx, conf))),
622622
Box::new(move |_| Box::new(doc::Documentation::new(conf))),

0 commit comments

Comments
 (0)