]> git.lizzy.rs Git - rust.git/blob - tests/ui/new_without_default.rs
Merge pull request #3280 from d-dorazio/fix-new_without_default-should-not-fire-unsaf...
[rust.git] / tests / ui / new_without_default.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10
11 #![feature(tool_lints)]
12
13 #![feature(const_fn)]
14
15
16 #![allow(dead_code)]
17 #![warn(clippy::new_without_default, clippy::new_without_default_derive)]
18
19 pub struct Foo;
20
21 impl Foo {
22     pub fn new() -> Foo { Foo }
23 }
24
25 pub struct Bar;
26
27 impl Bar {
28     pub fn new() -> Self { Bar }
29 }
30
31 pub struct Ok;
32
33 impl Ok {
34     pub fn new() -> Self { Ok }
35 }
36
37 impl Default for Ok {
38     fn default() -> Self { Ok }
39 }
40
41 pub struct Params;
42
43 impl Params {
44     pub fn new(_: u32) -> Self { Params }
45 }
46
47 pub struct GenericsOk<T> {
48     bar: T,
49 }
50
51 impl<U> Default for GenericsOk<U> {
52     fn default() -> Self { unimplemented!(); }
53 }
54
55 impl<'c, V> GenericsOk<V> {
56     pub fn new() -> GenericsOk<V> { unimplemented!() }
57 }
58
59 pub struct LtOk<'a> {
60     foo: &'a bool,
61 }
62
63 impl<'b> Default for LtOk<'b> {
64     fn default() -> Self { unimplemented!(); }
65 }
66
67 impl<'c> LtOk<'c> {
68     pub fn new() -> LtOk<'c> { unimplemented!() }
69 }
70
71 pub struct LtKo<'a> {
72     foo: &'a bool,
73 }
74
75 impl<'c> LtKo<'c> {
76     pub fn new() -> LtKo<'c> { unimplemented!() }
77     // FIXME: that suggestion is missing lifetimes
78 }
79
80 struct Private;
81
82 impl Private {
83     fn new() -> Private { unimplemented!() } // We don't lint private items
84 }
85
86 struct Const;
87
88 impl Const {
89     pub const fn new() -> Const { Const } // const fns can't be implemented via Default
90 }
91
92 pub struct IgnoreGenericNew;
93
94 impl IgnoreGenericNew {
95     pub fn new<T>() -> Self { IgnoreGenericNew } // the derived Default does not make sense here as the result depends on T
96 }
97
98 pub trait TraitWithNew: Sized {
99     fn new() -> Self {
100         panic!()
101     }
102 }
103
104 pub struct IgnoreUnsafeNew;
105
106 impl IgnoreUnsafeNew {
107     pub unsafe fn new() -> Self { IgnoreUnsafeNew }
108 }
109
110 fn main() {}