]> git.lizzy.rs Git - rust.git/blob - src/test/ui/lint/lint-unconditional-recursion.rs
Move parse-fail tests to UI
[rust.git] / src / test / ui / lint / lint-unconditional-recursion.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![deny(unconditional_recursion)]
12
13 #![allow(dead_code)]
14 fn foo() { //~ ERROR function cannot return without recursing
15     foo();
16 }
17
18 fn bar() {
19     if true {
20         bar()
21     }
22 }
23
24 fn baz() { //~ ERROR function cannot return without recursing
25     if true {
26         baz()
27     } else {
28         baz()
29     }
30 }
31
32 fn qux() {
33     loop {}
34 }
35
36 fn quz() -> bool { //~ ERROR function cannot return without recursing
37     if true {
38         while quz() {}
39         true
40     } else {
41         loop { quz(); }
42     }
43 }
44
45 // Trait method calls.
46 trait Foo {
47     fn bar(&self) { //~ ERROR function cannot return without recursing
48         self.bar()
49     }
50 }
51
52 impl Foo for Box<Foo+'static> {
53     fn bar(&self) { //~ ERROR function cannot return without recursing
54         loop {
55             self.bar()
56         }
57     }
58 }
59
60 // Trait method call with integer fallback after method resolution.
61 impl Foo for i32 {
62     fn bar(&self) { //~ ERROR function cannot return without recursing
63         0.bar()
64     }
65 }
66
67 impl Foo for u32 {
68     fn bar(&self) {
69         0.bar()
70     }
71 }
72
73 // Trait method calls via paths.
74 trait Foo2 {
75     fn bar(&self) { //~ ERROR function cannot return without recursing
76         Foo2::bar(self)
77     }
78 }
79
80 impl Foo2 for Box<Foo2+'static> {
81     fn bar(&self) { //~ ERROR function cannot return without recursing
82         loop {
83             Foo2::bar(self)
84         }
85     }
86 }
87
88 struct Baz;
89 impl Baz {
90     // Inherent method call.
91     fn qux(&self) { //~ ERROR function cannot return without recursing
92         self.qux();
93     }
94
95     // Inherent method call via path.
96     fn as_ref(&self) -> &Self { //~ ERROR function cannot return without recursing
97         Baz::as_ref(self)
98     }
99 }
100
101 // Trait method calls to impls via paths.
102 impl Default for Baz {
103     fn default() -> Baz { //~ ERROR function cannot return without recursing
104         let x = Default::default();
105         x
106     }
107 }
108
109 // Overloaded operators.
110 impl std::ops::Deref for Baz {
111     type Target = ();
112     fn deref(&self) -> &() { //~ ERROR function cannot return without recursing
113         &**self
114     }
115 }
116
117 impl std::ops::Index<usize> for Baz {
118     type Output = Baz;
119     fn index(&self, x: usize) -> &Baz { //~ ERROR function cannot return without recursing
120         &self[x]
121     }
122 }
123
124 // Overloaded autoderef.
125 struct Quux;
126 impl std::ops::Deref for Quux {
127     type Target = Baz;
128     fn deref(&self) -> &Baz { //~ ERROR function cannot return without recursing
129         self.as_ref()
130     }
131 }
132
133 fn all_fine() {
134     let _f = all_fine;
135 }
136
137 // issue 26333
138 trait Bar {
139     fn method<T: Bar>(&self, x: &T) {
140         x.method(x)
141     }
142 }
143
144 fn main() {}