]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/lint-unconditional-recursion.rs
Add a lint to detect unconditional recursion.
[rust.git] / src / test / compile-fail / 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 #![allow(dead_code)]
13 fn foo() { //~ ERROR function cannot return without recurring
14     foo(); //~ NOTE recursive call site
15 }
16
17 fn bar() {
18     if true {
19         bar()
20     }
21 }
22
23 fn baz() { //~ ERROR function cannot return without recurring
24     if true {
25         baz() //~ NOTE recursive call site
26     } else {
27         baz() //~ NOTE recursive call site
28     }
29 }
30
31 fn qux() {
32     loop {}
33 }
34
35 fn quz() -> bool { //~ ERROR function cannot return without recurring
36     if true {
37         while quz() {} //~ NOTE recursive call site
38         true
39     } else {
40         loop { quz(); } //~ NOTE recursive call site
41     }
42 }
43
44 trait Foo {
45     fn bar(&self) { //~ ERROR function cannot return without recurring
46         self.bar() //~ NOTE recursive call site
47     }
48 }
49
50 impl Foo for Box<Foo+'static> {
51     fn bar(&self) { //~ ERROR function cannot return without recurring
52         loop {
53             self.bar() //~ NOTE recursive call site
54         }
55     }
56
57 }
58
59 struct Baz;
60 impl Baz {
61     fn qux(&self) { //~ ERROR function cannot return without recurring
62         self.qux(); //~ NOTE recursive call site
63     }
64 }
65
66 fn main() {}