]> git.lizzy.rs Git - rust.git/blob - tests/ui/implicit_return.fixed
Improve `implicit_return`
[rust.git] / tests / ui / implicit_return.fixed
1 // run-rustfix
2
3 #![warn(clippy::implicit_return)]
4 #![allow(clippy::needless_return, clippy::needless_bool, unused, clippy::never_loop)]
5
6 fn test_end_of_fn() -> bool {
7     if true {
8         // no error!
9         return true;
10     }
11
12     return true
13 }
14
15 fn test_if_block() -> bool {
16     if true { return true } else { return false }
17 }
18
19 #[rustfmt::skip]
20 fn test_match(x: bool) -> bool {
21     match x {
22         true => return false,
23         false => { return true },
24     }
25 }
26
27 fn test_match_with_unreachable(x: bool) -> bool {
28     match x {
29         true => return false,
30         false => unreachable!(),
31     }
32 }
33
34 fn test_loop() -> bool {
35     loop {
36         return true;
37     }
38 }
39
40 fn test_loop_with_block() -> bool {
41     loop {
42         {
43             return true;
44         }
45     }
46 }
47
48 fn test_loop_with_nests() -> bool {
49     loop {
50         if true {
51             return true;
52         } else {
53             let _ = true;
54         }
55     }
56 }
57
58 #[allow(clippy::redundant_pattern_matching)]
59 fn test_loop_with_if_let() -> bool {
60     loop {
61         if let Some(x) = Some(true) {
62             return x;
63         }
64     }
65 }
66
67 fn test_closure() {
68     #[rustfmt::skip]
69     let _ = || { return true };
70     let _ = || return true;
71 }
72
73 fn test_panic() -> bool {
74     panic!()
75 }
76
77 fn test_return_macro() -> String {
78     return format!("test {}", "test")
79 }
80
81 fn macro_branch_test() -> bool {
82     macro_rules! m {
83         ($t:expr, $f:expr) => {
84             if true { $t } else { $f }
85         };
86     }
87     return m!(true, false)
88 }
89
90 fn loop_test() -> bool {
91     'outer: loop {
92         if true {
93             return true;
94         }
95
96         let _ = loop {
97             if false {
98                 return false;
99             }
100             if true {
101                 break true;
102             }
103         };
104     }
105 }
106
107 fn loop_macro_test() -> bool {
108     macro_rules! m {
109         ($e:expr) => {
110             break $e
111         };
112     }
113     return loop {
114         m!(true);
115     }
116 }
117
118 fn divergent_test() -> bool {
119     fn diverge() -> ! {
120         panic!()
121     }
122     diverge()
123 }
124
125 fn main() {}