]> git.lizzy.rs Git - rust.git/blob - tests/ui/let_return.rs
Merge commit '7ea7cd165ad6705603852771bf82cc2fd6560db5' into clippyup2
[rust.git] / tests / ui / let_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 fn main() {}