]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_return.fixed
82d95cc041fb35941a47c4862625db40d34e40bd
[rust.git] / tests / ui / needless_return.fixed
1 // run-rustfix
2
3 #![allow(unused)]
4 #![allow(
5     clippy::if_same_then_else,
6     clippy::single_match,
7     clippy::branches_sharing_code,
8     clippy::needless_bool
9 )]
10 #![warn(clippy::needless_return)]
11
12 macro_rules! the_answer {
13     () => {
14         42
15     };
16 }
17
18 fn test_end_of_fn() -> bool {
19     if true {
20         // no error!
21         return true;
22     }
23     true
24 }
25
26 fn test_no_semicolon() -> bool {
27     true
28 }
29
30 fn test_if_block() -> bool {
31     if true {
32         true
33     } else {
34         false
35     }
36 }
37
38 fn test_match(x: bool) -> bool {
39     match x {
40         true => false,
41         false => {
42             true
43         },
44     }
45 }
46
47 fn test_closure() {
48     let _ = || {
49         true
50     };
51     let _ = || true;
52 }
53
54 fn test_macro_call() -> i32 {
55     return the_answer!();
56 }
57
58 fn test_void_fun() {
59     
60 }
61
62 fn test_void_if_fun(b: bool) {
63     if b {
64         
65     } else {
66         
67     }
68 }
69
70 fn test_void_match(x: u32) {
71     match x {
72         0 => (),
73         _ => {},
74     }
75 }
76
77 fn read_line() -> String {
78     use std::io::BufRead;
79     let stdin = ::std::io::stdin();
80     return stdin.lock().lines().next().unwrap().unwrap();
81 }
82
83 fn borrows_but_not_last(value: bool) -> String {
84     if value {
85         use std::io::BufRead;
86         let stdin = ::std::io::stdin();
87         let _a = stdin.lock().lines().next().unwrap().unwrap();
88         String::from("test")
89     } else {
90         String::new()
91     }
92 }
93
94 macro_rules! needed_return {
95     ($e:expr) => {
96         if $e > 3 {
97             return;
98         }
99     };
100 }
101
102 fn test_return_in_macro() {
103     // This will return and the macro below won't be executed. Removing the `return` from the macro
104     // will change semantics.
105     needed_return!(10);
106     needed_return!(0);
107 }
108
109 mod issue6501 {
110     fn foo(bar: Result<(), ()>) {
111         bar.unwrap_or_else(|_| {})
112     }
113
114     fn test_closure() {
115         let _ = || {
116             
117         };
118         let _ = || {};
119     }
120
121     struct Foo;
122     #[allow(clippy::unnecessary_lazy_evaluations)]
123     fn bar(res: Result<Foo, u8>) -> Foo {
124         res.unwrap_or_else(|_| Foo)
125     }
126 }
127
128 fn main() {
129     let _ = test_end_of_fn();
130     let _ = test_no_semicolon();
131     let _ = test_if_block();
132     let _ = test_match(true);
133     test_closure();
134 }