]> git.lizzy.rs Git - rust.git/blob - tests/ui/escape_analysis.rs
Merge pull request #2984 from flip1995/single_char_pattern
[rust.git] / tests / ui / escape_analysis.rs
1 #![feature(box_syntax)]
2
3 #![allow(warnings, clippy)]
4
5 #![warn(boxed_local)]
6
7 #[derive(Clone)]
8 struct A;
9
10 impl A {
11     fn foo(&self){}
12 }
13
14 trait Z {
15     fn bar(&self);
16 }
17
18 impl Z for A {
19     fn bar(&self) {
20         //nothing
21     }
22 }
23
24 fn main() {
25 }
26
27 fn ok_box_trait(boxed_trait: &Box<Z>) {
28     let boxed_local = boxed_trait;
29     // done
30 }
31
32 fn warn_call() {
33     let x = box A;
34     x.foo();
35 }
36
37 fn warn_arg(x: Box<A>) {
38     x.foo();
39 }
40
41 fn nowarn_closure_arg() {
42     let x = Some(box A);
43     x.map_or((), |x| take_ref(&x));
44 }
45
46 fn warn_rename_call() {
47     let x = box A;
48
49     let y = x;
50     y.foo(); // via autoderef
51 }
52
53 fn warn_notuse() {
54     let bz = box A;
55 }
56
57 fn warn_pass() {
58     let bz = box A;
59     take_ref(&bz); // via deref coercion
60 }
61
62 fn nowarn_return() -> Box<A> {
63     let fx = box A;
64     fx // moved out, "escapes"
65 }
66
67 fn nowarn_move() {
68     let bx = box A;
69     drop(bx) // moved in, "escapes"
70 }
71 fn nowarn_call() {
72     let bx = box A;
73     bx.clone(); // method only available to Box, not via autoderef
74 }
75
76 fn nowarn_pass() {
77     let bx = box A;
78     take_box(&bx); // fn needs &Box
79 }
80
81
82 fn take_box(x: &Box<A>) {}
83 fn take_ref(x: &A) {}
84
85
86 fn nowarn_ref_take() {
87     // false positive, should actually warn
88     let x = box A;
89     let y = &x;
90     take_box(y);
91 }
92
93 fn nowarn_match() {
94     let x = box A; // moved into a match
95     match x {
96         y => drop(y)
97     }
98 }
99
100 fn warn_match() {
101     let x = box A;
102     match &x { // not moved
103         ref y => ()
104     }
105 }
106
107 fn nowarn_large_array() {
108     // should not warn, is large array
109     // and should not be on stack
110     let x = box [1; 10000];
111     match &x { // not moved
112         ref y => ()
113     }
114 }
115
116
117 /// ICE regression test
118 pub trait Foo {
119     type Item;
120 }
121
122 impl<'a> Foo for &'a () {
123     type Item = ();
124 }
125
126 pub struct PeekableSeekable<I: Foo> {
127     _peeked: I::Item,
128 }
129
130 pub fn new(_needs_name: Box<PeekableSeekable<&()>>) -> () {
131 }