2023-02-10 07:01:19 -06:00
|
|
|
#![allow(
|
|
|
|
dead_code,
|
|
|
|
clippy::missing_safety_doc,
|
|
|
|
clippy::extra_unused_lifetimes,
|
|
|
|
clippy::extra_unused_type_parameters
|
|
|
|
)]
|
2018-12-17 17:25:49 -06:00
|
|
|
#![warn(clippy::new_without_default)]
|
2016-03-01 09:25:15 -06:00
|
|
|
|
2016-06-01 16:35:14 -05:00
|
|
|
pub struct Foo;
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2016-03-01 09:25:15 -06:00
|
|
|
impl Foo {
|
2018-12-09 16:26:16 -06:00
|
|
|
pub fn new() -> Foo {
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: you should consider adding a `Default` implementation for `Foo`
|
|
|
|
//~| NOTE: `-D clippy::new-without-default` implied by `-D warnings`
|
2018-12-09 16:26:16 -06:00
|
|
|
Foo
|
|
|
|
}
|
2016-03-01 09:25:15 -06:00
|
|
|
}
|
|
|
|
|
2016-06-01 16:35:14 -05:00
|
|
|
pub struct Bar;
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2016-03-01 09:25:15 -06:00
|
|
|
impl Bar {
|
2018-12-09 16:26:16 -06:00
|
|
|
pub fn new() -> Self {
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: you should consider adding a `Default` implementation for `Bar`
|
2018-12-09 16:26:16 -06:00
|
|
|
Bar
|
|
|
|
}
|
2016-03-01 09:25:15 -06:00
|
|
|
}
|
|
|
|
|
2016-06-01 16:35:14 -05:00
|
|
|
pub struct Ok;
|
2016-03-01 09:25:15 -06:00
|
|
|
|
|
|
|
impl Ok {
|
2018-12-09 16:26:16 -06:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Ok
|
|
|
|
}
|
2016-03-01 09:25:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Ok {
|
2018-12-09 16:26:16 -06:00
|
|
|
fn default() -> Self {
|
|
|
|
Ok
|
|
|
|
}
|
2016-03-01 09:25:15 -06:00
|
|
|
}
|
|
|
|
|
2016-06-01 16:35:14 -05:00
|
|
|
pub struct Params;
|
2016-03-01 09:25:15 -06:00
|
|
|
|
|
|
|
impl Params {
|
2018-12-09 16:26:16 -06:00
|
|
|
pub fn new(_: u32) -> Self {
|
|
|
|
Params
|
|
|
|
}
|
2016-03-01 09:25:15 -06:00
|
|
|
}
|
|
|
|
|
2016-06-01 16:35:14 -05:00
|
|
|
pub struct GenericsOk<T> {
|
2016-03-03 12:46:10 -06:00
|
|
|
bar: T,
|
|
|
|
}
|
|
|
|
|
2016-03-18 13:12:32 -05:00
|
|
|
impl<U> Default for GenericsOk<U> {
|
2018-12-09 16:26:16 -06:00
|
|
|
fn default() -> Self {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
2016-03-18 13:12:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'c, V> GenericsOk<V> {
|
2018-12-09 16:26:16 -06:00
|
|
|
pub fn new() -> GenericsOk<V> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2016-03-18 13:12:32 -05:00
|
|
|
}
|
|
|
|
|
2016-06-01 16:35:14 -05:00
|
|
|
pub struct LtOk<'a> {
|
2016-03-18 13:12:32 -05:00
|
|
|
foo: &'a bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'b> Default for LtOk<'b> {
|
2018-12-09 16:26:16 -06:00
|
|
|
fn default() -> Self {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
2016-03-18 13:12:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'c> LtOk<'c> {
|
2018-12-09 16:26:16 -06:00
|
|
|
pub fn new() -> LtOk<'c> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2016-03-18 13:12:32 -05:00
|
|
|
}
|
|
|
|
|
2016-06-01 16:35:14 -05:00
|
|
|
pub struct LtKo<'a> {
|
2016-03-18 13:12:32 -05:00
|
|
|
foo: &'a bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'c> LtKo<'c> {
|
2018-12-09 16:26:16 -06:00
|
|
|
pub fn new() -> LtKo<'c> {
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: you should consider adding a `Default` implementation for `LtKo<'c>`
|
2018-12-09 16:26:16 -06:00
|
|
|
unimplemented!()
|
|
|
|
}
|
2016-06-01 16:35:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Private;
|
|
|
|
|
|
|
|
impl Private {
|
2018-12-09 16:26:16 -06:00
|
|
|
fn new() -> Private {
|
|
|
|
unimplemented!()
|
|
|
|
} // We don't lint private items
|
2016-03-03 12:46:10 -06:00
|
|
|
}
|
|
|
|
|
2022-02-26 07:26:21 -06:00
|
|
|
struct PrivateStruct;
|
|
|
|
|
|
|
|
impl PrivateStruct {
|
|
|
|
pub fn new() -> PrivateStruct {
|
|
|
|
unimplemented!()
|
|
|
|
} // We don't lint public items on private structs
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct PrivateItem;
|
|
|
|
|
|
|
|
impl PrivateItem {
|
|
|
|
fn new() -> PrivateItem {
|
|
|
|
unimplemented!()
|
|
|
|
} // We don't lint private items on public structs
|
|
|
|
}
|
|
|
|
|
2016-06-03 09:45:07 -05:00
|
|
|
struct Const;
|
|
|
|
|
|
|
|
impl Const {
|
2018-12-09 16:26:16 -06:00
|
|
|
pub const fn new() -> Const {
|
|
|
|
Const
|
|
|
|
} // const fns can't be implemented via Default
|
2016-06-03 09:45:07 -05:00
|
|
|
}
|
2017-06-14 11:50:19 -05:00
|
|
|
|
|
|
|
pub struct IgnoreGenericNew;
|
|
|
|
|
|
|
|
impl IgnoreGenericNew {
|
2018-12-09 16:26:16 -06:00
|
|
|
pub fn new<T>() -> Self {
|
|
|
|
IgnoreGenericNew
|
|
|
|
} // the derived Default does not make sense here as the result depends on T
|
2017-06-14 11:50:19 -05:00
|
|
|
}
|
|
|
|
|
2017-11-29 10:06:27 -06:00
|
|
|
pub trait TraitWithNew: Sized {
|
|
|
|
fn new() -> Self {
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-07 05:39:54 -05:00
|
|
|
pub struct IgnoreUnsafeNew;
|
|
|
|
|
|
|
|
impl IgnoreUnsafeNew {
|
2018-12-09 16:26:16 -06:00
|
|
|
pub unsafe fn new() -> Self {
|
|
|
|
IgnoreUnsafeNew
|
|
|
|
}
|
2018-10-07 05:39:54 -05:00
|
|
|
}
|
|
|
|
|
2018-10-21 23:59:45 -05:00
|
|
|
#[derive(Default)]
|
2019-06-19 13:36:23 -05:00
|
|
|
pub struct OptionRefWrapper<'a, T>(Option<&'a T>);
|
2018-10-21 23:59:45 -05:00
|
|
|
|
2019-06-19 13:36:23 -05:00
|
|
|
impl<'a, T> OptionRefWrapper<'a, T> {
|
2018-10-21 23:59:45 -05:00
|
|
|
pub fn new() -> Self {
|
|
|
|
OptionRefWrapper(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 00:06:41 -06:00
|
|
|
pub struct Allow(Foo);
|
|
|
|
|
|
|
|
impl Allow {
|
|
|
|
#[allow(clippy::new_without_default)]
|
2018-12-27 09:17:45 -06:00
|
|
|
pub fn new() -> Self {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2018-12-11 00:06:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AllowDerive;
|
|
|
|
|
|
|
|
impl AllowDerive {
|
2018-12-17 17:25:49 -06:00
|
|
|
#[allow(clippy::new_without_default)]
|
2018-12-27 09:17:45 -06:00
|
|
|
pub fn new() -> Self {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2018-12-11 00:06:41 -06:00
|
|
|
}
|
|
|
|
|
2020-05-28 08:45:24 -05:00
|
|
|
pub struct NewNotEqualToDerive {
|
|
|
|
foo: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NewNotEqualToDerive {
|
|
|
|
// This `new` implementation is not equal to a derived `Default`, so do not suggest deriving.
|
|
|
|
pub fn new() -> Self {
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: you should consider adding a `Default` implementation for `NewNotEqualToDe
|
2020-05-28 08:45:24 -05:00
|
|
|
NewNotEqualToDerive { foo: 1 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:29:11 -05:00
|
|
|
// see #6933
|
|
|
|
pub struct FooGenerics<T>(std::marker::PhantomData<T>);
|
|
|
|
impl<T> FooGenerics<T> {
|
|
|
|
pub fn new() -> Self {
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: you should consider adding a `Default` implementation for `FooGenerics<T>`
|
2021-03-25 13:29:11 -05:00
|
|
|
Self(Default::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct BarGenerics<T>(std::marker::PhantomData<T>);
|
|
|
|
impl<T: Copy> BarGenerics<T> {
|
|
|
|
pub fn new() -> Self {
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: you should consider adding a `Default` implementation for `BarGenerics<T>`
|
2021-03-25 13:29:11 -05:00
|
|
|
Self(Default::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 05:16:06 -05:00
|
|
|
pub mod issue7220 {
|
|
|
|
pub struct Foo<T> {
|
|
|
|
_bar: *mut T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Foo<T> {
|
|
|
|
pub fn new() -> Self {
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: you should consider adding a `Default` implementation for `Foo<T>`
|
2021-07-29 05:16:06 -05:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-26 07:26:21 -06:00
|
|
|
// see issue #8152
|
|
|
|
// This should not create any lints
|
|
|
|
pub struct DocHidden;
|
|
|
|
impl DocHidden {
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn new() -> Self {
|
|
|
|
DocHidden
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-01 09:25:15 -06:00
|
|
|
fn main() {}
|
2022-07-18 02:39:37 -05:00
|
|
|
|
|
|
|
pub struct IgnoreConstGenericNew(usize);
|
|
|
|
impl IgnoreConstGenericNew {
|
|
|
|
pub fn new<const N: usize>() -> Self {
|
|
|
|
Self(N)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct IgnoreLifetimeNew;
|
|
|
|
impl IgnoreLifetimeNew {
|
|
|
|
pub fn new<'a>() -> Self {
|
|
|
|
Self
|
|
|
|
}
|
|
|
|
}
|
2023-07-31 14:57:31 -05:00
|
|
|
|
|
|
|
// From issue #11267
|
|
|
|
|
|
|
|
pub struct MyStruct<K, V>
|
|
|
|
where
|
|
|
|
K: std::hash::Hash + Eq + PartialEq,
|
|
|
|
{
|
|
|
|
_kv: Option<(K, V)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K, V> MyStruct<K, V>
|
|
|
|
where
|
|
|
|
K: std::hash::Hash + Eq + PartialEq,
|
|
|
|
{
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self { _kv: None }
|
|
|
|
}
|
|
|
|
}
|