rust/tests/ui/new_without_default.rs

161 lines
2.6 KiB
Rust
Raw Normal View History

2018-10-06 11:18:06 -05:00
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(const_fn)]
#![allow(dead_code)]
#![warn(clippy::new_without_default)]
pub struct Foo;
2017-02-08 07:58:07 -06:00
impl Foo {
2018-12-09 16:26:16 -06:00
pub fn new() -> Foo {
Foo
}
}
pub struct Bar;
2017-02-08 07:58:07 -06:00
impl Bar {
2018-12-09 16:26:16 -06:00
pub fn new() -> Self {
Bar
}
}
pub struct Ok;
impl Ok {
2018-12-09 16:26:16 -06:00
pub fn new() -> Self {
Ok
}
}
impl Default for Ok {
2018-12-09 16:26:16 -06:00
fn default() -> Self {
Ok
}
}
pub struct Params;
impl Params {
2018-12-09 16:26:16 -06:00
pub fn new(_: u32) -> Self {
Params
}
}
pub struct GenericsOk<T> {
2016-03-03 12:46:10 -06:00
bar: T,
}
impl<U> Default for GenericsOk<U> {
2018-12-09 16:26:16 -06:00
fn default() -> Self {
unimplemented!();
}
}
impl<'c, V> GenericsOk<V> {
2018-12-09 16:26:16 -06:00
pub fn new() -> GenericsOk<V> {
unimplemented!()
}
}
pub struct LtOk<'a> {
foo: &'a bool,
}
impl<'b> Default for LtOk<'b> {
2018-12-09 16:26:16 -06:00
fn default() -> Self {
unimplemented!();
}
}
impl<'c> LtOk<'c> {
2018-12-09 16:26:16 -06:00
pub fn new() -> LtOk<'c> {
unimplemented!()
}
}
pub struct LtKo<'a> {
foo: &'a bool,
}
impl<'c> LtKo<'c> {
2018-12-09 16:26:16 -06:00
pub fn new() -> LtKo<'c> {
unimplemented!()
}
// FIXME: that suggestion is missing lifetimes
}
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
}
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
}
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-11-29 10:06:27 -06:00
pub trait TraitWithNew: Sized {
fn new() -> Self {
panic!()
}
}
pub struct IgnoreUnsafeNew;
impl IgnoreUnsafeNew {
2018-12-09 16:26:16 -06:00
pub unsafe fn new() -> Self {
IgnoreUnsafeNew
}
}
#[derive(Default)]
pub struct OptionRefWrapper<'a, T: 'a>(Option<&'a T>);
impl<'a, T: 'a> OptionRefWrapper<'a, T> {
pub fn new() -> Self {
OptionRefWrapper(None)
}
}
pub struct Allow(Foo);
impl Allow {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
unimplemented!()
}
}
pub struct AllowDerive;
impl AllowDerive {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
unimplemented!()
}
}
fn main() {}