]> git.lizzy.rs Git - rust.git/blob - tests/ui/let_and_return.rs
e3561863c1e1ff7f00f1e52703e808cdb1c64e63
[rust.git] / tests / ui / let_and_return.rs
1 #![allow(unused)]
2 #![warn(clippy::let_and_return)]
3
4 fn test() -> i32 {
5     let _y = 0; // no warning
6     let x = 5;
7     x
8 }
9
10 fn test_inner() -> i32 {
11     if true {
12         let x = 5;
13         x
14     } else {
15         0
16     }
17 }
18
19 fn test_nowarn_1() -> i32 {
20     let mut x = 5;
21     x += 1;
22     x
23 }
24
25 fn test_nowarn_2() -> i32 {
26     let x = 5;
27     x + 1
28 }
29
30 fn test_nowarn_3() -> (i32, i32) {
31     // this should technically warn, but we do not compare complex patterns
32     let (x, y) = (5, 9);
33     (x, y)
34 }
35
36 fn test_nowarn_4() -> i32 {
37     // this should technically warn, but not b/c of clippy::let_and_return, but b/c of useless type
38     let x: i32 = 5;
39     x
40 }
41
42 fn test_nowarn_5(x: i16) -> u16 {
43     #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
44     let x = x as u16;
45     x
46 }
47
48 // False positive example
49 trait Decode {
50     fn decode<D: std::io::Read>(d: D) -> Result<Self, ()>
51     where
52         Self: Sized;
53 }
54
55 macro_rules! tuple_encode {
56     ($($x:ident),*) => (
57         impl<$($x: Decode),*> Decode for ($($x),*) {
58             #[inline]
59             #[allow(non_snake_case)]
60             fn decode<D: std::io::Read>(mut d: D) -> Result<Self, ()> {
61                 // Shouldn't trigger lint
62                 Ok(($({let $x = Decode::decode(&mut d)?; $x }),*))
63             }
64         }
65     );
66 }
67
68 tuple_encode!(T0, T1, T2, T3, T4, T5, T6, T7);
69
70 mod no_lint_if_stmt_borrows {
71     mod issue_3792 {
72         use std::io::{self, BufRead, Stdin};
73
74         fn read_line() -> String {
75             let stdin = io::stdin();
76             let line = stdin.lock().lines().next().unwrap().unwrap();
77             line
78         }
79     }
80
81     mod issue_3324 {
82         use std::cell::RefCell;
83         use std::rc::{Rc, Weak};
84
85         fn test(value: Weak<RefCell<Bar>>) -> u32 {
86             let value = value.upgrade().unwrap();
87             let ret = value.borrow().baz();
88             ret
89         }
90
91         struct Bar {}
92
93         impl Bar {
94             fn new() -> Self {
95                 Bar {}
96             }
97             fn baz(&self) -> u32 {
98                 0
99             }
100         }
101
102         fn main() {
103             let a = Rc::new(RefCell::new(Bar::new()));
104             let b = Rc::downgrade(&a);
105             test(b);
106         }
107     }
108
109     mod free_function {
110         struct Inner;
111
112         struct Foo<'a> {
113             inner: &'a Inner,
114         }
115
116         impl Drop for Foo<'_> {
117             fn drop(&mut self) {}
118         }
119
120         impl<'a> Foo<'a> {
121             fn new(inner: &'a Inner) -> Self {
122                 Self { inner }
123             }
124
125             fn value(&self) -> i32 {
126                 42
127             }
128         }
129
130         fn some_foo(inner: &Inner) -> Foo<'_> {
131             Foo { inner }
132         }
133
134         fn test() -> i32 {
135             let x = Inner {};
136             let value = some_foo(&x).value();
137             value
138         }
139
140         fn test2() -> i32 {
141             let x = Inner {};
142             let value = Foo::new(&x).value();
143             value
144         }
145     }
146 }
147
148 mod issue_5729 {
149     use std::sync::Arc;
150
151     trait Foo {}
152
153     trait FooStorage {
154         fn foo_cloned(&self) -> Arc<dyn Foo>;
155     }
156
157     struct FooStorageImpl<T: Foo> {
158         foo: Arc<T>,
159     }
160
161     impl<T: Foo + 'static> FooStorage for FooStorageImpl<T> {
162         fn foo_cloned(&self) -> Arc<dyn Foo> {
163             let clone = Arc::clone(&self.foo);
164             clone
165         }
166     }
167 }
168
169 fn main() {}