]> git.lizzy.rs Git - rust.git/blob - tests/ui/default_trait_access.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[rust.git] / tests / ui / default_trait_access.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 #![warn(clippy::default_trait_access)]
11
12 use std::default;
13 use std::default::Default as D2;
14 use std::string;
15
16 fn main() {
17     let s1: String = Default::default();
18
19     let s2 = String::default();
20
21     let s3: String = D2::default();
22
23     let s4: String = std::default::Default::default();
24
25     let s5 = string::String::default();
26
27     let s6: String = default::Default::default();
28
29     let s7 = std::string::String::default();
30
31     let s8: String = DefaultFactory::make_t_badly();
32
33     let s9: String = DefaultFactory::make_t_nicely();
34
35     let s10 = DerivedDefault::default();
36
37     let s11: GenericDerivedDefault<String> = Default::default();
38
39     let s12 = GenericDerivedDefault::<String>::default();
40
41     let s13 = TupleDerivedDefault::default();
42
43     let s14: TupleDerivedDefault = Default::default();
44
45     let s15: ArrayDerivedDefault = Default::default();
46
47     let s16 = ArrayDerivedDefault::default();
48
49     let s17: TupleStructDerivedDefault = Default::default();
50
51     let s18 = TupleStructDerivedDefault::default();
52
53     let s19 = <DerivedDefault as Default>::default();
54
55     println!(
56         "[{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}], [{:?}]",
57         s1,
58         s2,
59         s3,
60         s4,
61         s5,
62         s6,
63         s7,
64         s8,
65         s9,
66         s10,
67         s11,
68         s12,
69         s13,
70         s14,
71         s15,
72         s16,
73         s17,
74         s18,
75         s19,
76     );
77 }
78
79 struct DefaultFactory;
80
81 impl DefaultFactory {
82     pub fn make_t_badly<T: Default>() -> T {
83         Default::default()
84     }
85
86     pub fn make_t_nicely<T: Default>() -> T {
87         T::default()
88     }
89 }
90
91 #[derive(Debug, Default)]
92 struct DerivedDefault {
93     pub s: String,
94 }
95
96 #[derive(Debug, Default)]
97 struct GenericDerivedDefault<T: Default + std::fmt::Debug> {
98     pub s: T,
99 }
100
101 #[derive(Debug, Default)]
102 struct TupleDerivedDefault {
103     pub s: (String, String),
104 }
105
106 #[derive(Debug, Default)]
107 struct ArrayDerivedDefault {
108     pub s: [String; 10],
109 }
110
111 #[derive(Debug, Default)]
112 struct TupleStructDerivedDefault(String);